Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
58 / 58
100.00% covered (success)
100.00%
29 / 29
CRAP
100.00% covered (success)
100.00%
1 / 1
File
100.00% covered (success)
100.00%
58 / 58
100.00% covered (success)
100.00%
29 / 29
38
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
6
 getMimeTypes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFileMimeType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setBasename
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getBasename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasBasename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFilename
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setExtension
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getExtension
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasExtension
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setPath
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasPath
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setSize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setMimeType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getMimeType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasMimeType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setDefaultMimeType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultMimeType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDefaultMimeType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getAllMimeTypes
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 exists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getContents
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 toArray
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Utils;
15
16/**
17 * Pop utils file helper class
18 *
19 * @category   Pop
20 * @package    Pop\Utils
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    2.0.0
25 */
26class File
27{
28
29    /**
30     * Basename (filename.ext)
31     * @var ?string
32     */
33    protected ?string $basename = null;
34
35    /**
36     * Filename (filename)
37     * @var ?string
38     */
39    protected ?string $filename = null;
40
41    /**
42     * Extension (.ext)
43     * @var ?string
44     */
45    protected ?string $extension = null;
46
47    /**
48     * Path (/some/path)
49     * @var ?string
50     */
51    protected ?string $path = null;
52
53    /**
54     * Size
55     * @var int
56     */
57    protected int $size = 0;
58
59    /**
60     * Mime type
61     * @var ?string
62     */
63    protected ?string $mimeType = null;
64
65    /**
66     * Mime types
67     * @var array
68     */
69    protected static array $mimeTypes = [
70        'avi'   => 'video/x-msvideo',
71        'bin'   => 'application/octet-stream',
72        'bmp'   => 'image/bmp',
73        'bz'    => 'application/x-bzip',
74        'bz2'   => 'application/x-bzip2',
75        'css'   => 'text/css',
76        'csv'   => 'text/csv',
77        'doc'   => 'application/msword',
78        'docx'  => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
79        'gz'    => 'application/gzip',
80        'gif'   => 'image/gif',
81        'htm'   => 'text/html',
82        'html'  => 'text/html',
83        'ico'   => 'image/vnd.microsoft.icon',
84        'ics'   => 'text/calendar',
85        'jar'   => 'application/java-archive',
86        'jpe'   => 'image/jpeg',
87        'jpg'   => 'image/jpeg',
88        'jpeg'  => 'image/jpeg',
89        'js'    => 'text/javascript',
90        'json'  => 'application/json',
91        'jwt'   => 'application/jwt',
92        'log'   => 'text/plain',
93        'mid'   => 'audio/midi',
94        'midi'  => 'audio/midi',
95        'mp3'   => 'audio/mpeg',
96        'mp4'   => 'video/mp4',
97        'mpeg'  => 'video/mpeg',
98        'odp'   => 'application/vnd.oasis.opendocument.presentation',
99        'ods'   => 'application/vnd.oasis.opendocument.spreadsheet',
100        'odt'   => 'application/vnd.oasis.opendocument.text',
101        'oga'   => 'audio/ogg',
102        'ogv'   => 'video/ogg',
103        'ogx'   => 'application/ogg',
104        'otf'   => 'font/otf',
105        'png'   => 'image/png',
106        'pdf'   => 'application/pdf',
107        'php'   => 'application/x-httpd-php',
108        'ppt'   => 'application/vnd.ms-powerpoint',
109        'pptx'  => 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
110        'psd'   => 'image/vnd.adobe.photoshop',
111        'rar'   => 'application/vnd.rar',
112        'rtf'   => 'application/rtf',
113        'sgml'  => 'application/sgml',
114        'sql'   => 'application/sql',
115        'svg'   => 'image/svg+xml',
116        'tar'   => 'application/x-tar',
117        'tif'   => 'image/tiff',
118        'tiff'  => 'image/tiff',
119        'ttf'   => 'font/ttf',
120        'tsv'   => 'text/tsv',
121        'txt'   => 'text/plain',
122        'wav'   => 'audio/wav',
123        'xhtml' => 'application/xhtml+xml',
124        'xls'   => 'application/vnd.ms-excel',
125        'xlsx'  => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
126        'xml'   => 'application/xml',
127        'yaml'  => 'application/yaml',
128        'zip'   => 'application/zip',
129    ];
130
131    /**
132     * Default mime types
133     * @var string
134     */
135    protected static string $defaultMimeType = 'application/octet-stream';
136
137    /**
138     * Constructor
139     *
140     * Instantiate the file object
141     *
142     * @param  ?string $filename
143     */
144    public function __construct(?string $filename = null)
145    {
146        $info = pathinfo($filename);
147
148        if (!empty($info['basename'])) {
149            $this->setBasename($info['basename']);
150        }
151        if (!empty($info['filename'])) {
152            $this->setFilename($info['filename']);
153        }
154        if (!empty($info['dirname'])) {
155            $this->setPath($info['dirname']);
156        }
157        if (!empty($info['extension'])) {
158            $this->setExtension($info['extension']);
159        }
160
161        if (file_exists($filename)) {
162            $this->setSize(filesize($filename));
163        }
164    }
165
166    /**
167     * Get common mime types
168     *
169     * @return array
170     */
171    public static function getMimeTypes(): array
172    {
173        return (new self())->getAllMimeTypes();
174    }
175
176    /**
177     * Get file's mime type
178     *
179     * @param  string $filename
180     * @return ?string
181     */
182    public static function getFileMimeType(string $filename): ?string
183    {
184        return (new self($filename))->getMimeType();
185    }
186
187    /**
188     * Set the basename
189     *
190     * @param  string $basename
191     * @return File
192     */
193    public function setBasename(string $basename): File
194    {
195        $this->basename = $basename;
196        return $this;
197    }
198
199    /**
200     * Get the basename
201     *
202     * @return ?string
203     */
204    public function getBasename(): ?string
205    {
206        return $this->basename;
207    }
208
209    /**
210     * Has the basename
211     *
212     * @return bool
213     */
214    public function hasBasename(): bool
215    {
216        return ($this->basename !== null);
217    }
218
219    /**
220     * Set the filename
221     *
222     * @param  string $filename
223     * @return File
224     */
225    public function setFilename(string $filename): File
226    {
227        $this->filename = $filename;
228        return $this;
229    }
230
231    /**
232     * Get the filename
233     *
234     * @return ?string
235     */
236    public function getFilename(): ?string
237    {
238        return $this->filename;
239    }
240
241    /**
242     * Has the filename
243     *
244     * @return bool
245     */
246    public function hasFilename(): bool
247    {
248        return ($this->filename !== null);
249    }
250
251    /**
252     * Set the extension
253     *
254     * @param  string $extension
255     * @return File
256     */
257    public function setExtension(string $extension): File
258    {
259        $this->extension = $extension;
260        if (array_key_exists(strtolower($extension), self::$mimeTypes)) {
261            $this->setMimeType(self::$mimeTypes[strtolower($extension)]);
262        } else {
263            $this->setDefaultMimeType();
264        }
265        return $this;
266    }
267
268    /**
269     * Get the extension
270     *
271     * @return ?string
272     */
273    public function getExtension(): ?string
274    {
275        return $this->extension;
276    }
277
278    /**
279     * Has the extension
280     *
281     * @return bool
282     */
283    public function hasExtension(): bool
284    {
285        return ($this->extension !== null);
286    }
287
288    /**
289     * Set the path
290     *
291     * @param  string $path
292     * @return File
293     */
294    public function setPath(string $path): File
295    {
296        $this->path = $path;
297        return $this;
298    }
299
300    /**
301     * Get the path
302     *
303     * @return ?string
304     */
305    public function getPath(): ?string
306    {
307        return $this->path;
308    }
309
310    /**
311     * Has the path
312     *
313     * @return bool
314     */
315    public function hasPath(): bool
316    {
317        return ($this->path !== null);
318    }
319
320    /**
321     * Set the size
322     *
323     * @param  int $size
324     * @return File
325     */
326    public function setSize(int $size): File
327    {
328        $this->size = $size;
329        return $this;
330    }
331
332    /**
333     * Get the size
334     *
335     * @return int
336     */
337    public function getSize(): int
338    {
339        return $this->size;
340    }
341
342    /**
343     * Has the size
344     *
345     * @return bool
346     */
347    public function hasSize(): bool
348    {
349        return ($this->size > 0);
350    }
351
352    /**
353     * Set the mime type
354     *
355     * @param  string $mimeType
356     * @return File
357     */
358    public function setMimeType(string $mimeType): File
359    {
360        $this->mimeType = $mimeType;
361        return $this;
362    }
363
364    /**
365     * Get the mime type
366     *
367     * @return ?string
368     */
369    public function getMimeType(): ?string
370    {
371        return $this->mimeType;
372    }
373
374    /**
375     * Has the mime type
376     *
377     * @return bool
378     */
379    public function hasMimeType(): bool
380    {
381        return ($this->mimeType !== null);
382    }
383
384    /**
385     * Set the mime type to default mime type
386     *
387     * @return File
388     */
389    public function setDefaultMimeType(): File
390    {
391        $this->mimeType = self::$defaultMimeType;
392        return $this;
393    }
394
395    /**
396     * Get the default mime type
397     *
398     * @return string
399     */
400    public function getDefaultMimeType(): string
401    {
402        return self::$defaultMimeType;
403    }
404
405    /**
406     * Set the default mime type
407     *
408     * @return bool
409     */
410    public function isDefaultMimeType(): bool
411    {
412        return ($this->mimeType == self::$defaultMimeType);
413    }
414
415    /**
416     * Get the all common mime types
417     *
418     * @return array
419     */
420    public function getAllMimeTypes(): array
421    {
422        return self::$mimeTypes;
423    }
424
425    /**
426     * Does the file exist
427     *
428     * @return bool
429     */
430    public function exists(): bool
431    {
432        $fullPath = ($this->hasPath()) ? $this->path . DIRECTORY_SEPARATOR . $this->basename : $this->basename;
433        return file_exists($fullPath);
434    }
435
436    /**
437     * Get the file contents
438     *
439     * @return mixed
440     */
441    public function getContents(): mixed
442    {
443        $fullPath = ($this->hasPath()) ? $this->path . DIRECTORY_SEPARATOR . $this->basename : $this->basename;
444        return file_get_contents($fullPath);
445    }
446
447    /**
448     * Convert file to an array
449     *
450     * @return array
451     */
452    public function toArray(): array
453    {
454        return [
455            'basename'  => $this->basename,
456            'filename'  => $this->filename,
457            'extension' => $this->extension,
458            'path'      => $this->path,
459            'size'      => $this->size,
460            'mime_type' => $this->mimeType,
461        ];
462    }
463
464    /**
465     * To string
466     *
467     * @return string
468     */
469    public function __toString(): string
470    {
471        return ($this->hasPath()) ? $this->path . DIRECTORY_SEPARATOR . $this->basename : $this->basename;
472    }
473
474}