Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
59 / 59 |
|
100.00% |
29 / 29 |
CRAP | |
100.00% |
1 / 1 |
File | |
100.00% |
59 / 59 |
|
100.00% |
29 / 29 |
38 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
6 | |||
getMimeTypes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getFileMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setBasename | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getBasename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasBasename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setFilename | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setExtension | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getExtension | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasExtension | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setPath | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setSize | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasSize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setMimeType | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
hasMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setDefaultMimeType | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDefaultMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isDefaultMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getAllMimeTypes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
exists | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
getContents | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
toArray | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
1 | |||
__toString | |
100.00% |
1 / 1 |
|
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 | */ |
14 | namespace 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.1.0 |
25 | */ |
26 | class 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 = []; |
147 | |
148 | if (!empty($filename)) { |
149 | $info = pathinfo($filename); |
150 | $this->setSize(filesize($filename)); |
151 | } |
152 | |
153 | if (!empty($info['basename'])) { |
154 | $this->setBasename($info['basename']); |
155 | } |
156 | if (!empty($info['filename'])) { |
157 | $this->setFilename($info['filename']); |
158 | } |
159 | if (!empty($info['dirname'])) { |
160 | $this->setPath($info['dirname']); |
161 | } |
162 | if (!empty($info['extension'])) { |
163 | $this->setExtension($info['extension']); |
164 | } |
165 | } |
166 | |
167 | /** |
168 | * Get common mime types |
169 | * |
170 | * @return array |
171 | */ |
172 | public static function getMimeTypes(): array |
173 | { |
174 | return (new self())->getAllMimeTypes(); |
175 | } |
176 | |
177 | /** |
178 | * Get file's mime type |
179 | * |
180 | * @param string $filename |
181 | * @return ?string |
182 | */ |
183 | public static function getFileMimeType(string $filename): ?string |
184 | { |
185 | return (new self($filename))->getMimeType(); |
186 | } |
187 | |
188 | /** |
189 | * Set the basename |
190 | * |
191 | * @param string $basename |
192 | * @return File |
193 | */ |
194 | public function setBasename(string $basename): File |
195 | { |
196 | $this->basename = $basename; |
197 | return $this; |
198 | } |
199 | |
200 | /** |
201 | * Get the basename |
202 | * |
203 | * @return ?string |
204 | */ |
205 | public function getBasename(): ?string |
206 | { |
207 | return $this->basename; |
208 | } |
209 | |
210 | /** |
211 | * Has the basename |
212 | * |
213 | * @return bool |
214 | */ |
215 | public function hasBasename(): bool |
216 | { |
217 | return ($this->basename !== null); |
218 | } |
219 | |
220 | /** |
221 | * Set the filename |
222 | * |
223 | * @param string $filename |
224 | * @return File |
225 | */ |
226 | public function setFilename(string $filename): File |
227 | { |
228 | $this->filename = $filename; |
229 | return $this; |
230 | } |
231 | |
232 | /** |
233 | * Get the filename |
234 | * |
235 | * @return ?string |
236 | */ |
237 | public function getFilename(): ?string |
238 | { |
239 | return $this->filename; |
240 | } |
241 | |
242 | /** |
243 | * Has the filename |
244 | * |
245 | * @return bool |
246 | */ |
247 | public function hasFilename(): bool |
248 | { |
249 | return ($this->filename !== null); |
250 | } |
251 | |
252 | /** |
253 | * Set the extension |
254 | * |
255 | * @param string $extension |
256 | * @return File |
257 | */ |
258 | public function setExtension(string $extension): File |
259 | { |
260 | $this->extension = $extension; |
261 | if (array_key_exists(strtolower($extension), self::$mimeTypes)) { |
262 | $this->setMimeType(self::$mimeTypes[strtolower($extension)]); |
263 | } else { |
264 | $this->setDefaultMimeType(); |
265 | } |
266 | return $this; |
267 | } |
268 | |
269 | /** |
270 | * Get the extension |
271 | * |
272 | * @return ?string |
273 | */ |
274 | public function getExtension(): ?string |
275 | { |
276 | return $this->extension; |
277 | } |
278 | |
279 | /** |
280 | * Has the extension |
281 | * |
282 | * @return bool |
283 | */ |
284 | public function hasExtension(): bool |
285 | { |
286 | return ($this->extension !== null); |
287 | } |
288 | |
289 | /** |
290 | * Set the path |
291 | * |
292 | * @param string $path |
293 | * @return File |
294 | */ |
295 | public function setPath(string $path): File |
296 | { |
297 | $this->path = $path; |
298 | return $this; |
299 | } |
300 | |
301 | /** |
302 | * Get the path |
303 | * |
304 | * @return ?string |
305 | */ |
306 | public function getPath(): ?string |
307 | { |
308 | return $this->path; |
309 | } |
310 | |
311 | /** |
312 | * Has the path |
313 | * |
314 | * @return bool |
315 | */ |
316 | public function hasPath(): bool |
317 | { |
318 | return ($this->path !== null); |
319 | } |
320 | |
321 | /** |
322 | * Set the size |
323 | * |
324 | * @param int $size |
325 | * @return File |
326 | */ |
327 | public function setSize(int $size): File |
328 | { |
329 | $this->size = $size; |
330 | return $this; |
331 | } |
332 | |
333 | /** |
334 | * Get the size |
335 | * |
336 | * @return int |
337 | */ |
338 | public function getSize(): int |
339 | { |
340 | return $this->size; |
341 | } |
342 | |
343 | /** |
344 | * Has the size |
345 | * |
346 | * @return bool |
347 | */ |
348 | public function hasSize(): bool |
349 | { |
350 | return ($this->size > 0); |
351 | } |
352 | |
353 | /** |
354 | * Set the mime type |
355 | * |
356 | * @param string $mimeType |
357 | * @return File |
358 | */ |
359 | public function setMimeType(string $mimeType): File |
360 | { |
361 | $this->mimeType = $mimeType; |
362 | return $this; |
363 | } |
364 | |
365 | /** |
366 | * Get the mime type |
367 | * |
368 | * @return ?string |
369 | */ |
370 | public function getMimeType(): ?string |
371 | { |
372 | return $this->mimeType; |
373 | } |
374 | |
375 | /** |
376 | * Has the mime type |
377 | * |
378 | * @return bool |
379 | */ |
380 | public function hasMimeType(): bool |
381 | { |
382 | return ($this->mimeType !== null); |
383 | } |
384 | |
385 | /** |
386 | * Set the mime type to default mime type |
387 | * |
388 | * @return File |
389 | */ |
390 | public function setDefaultMimeType(): File |
391 | { |
392 | $this->mimeType = self::$defaultMimeType; |
393 | return $this; |
394 | } |
395 | |
396 | /** |
397 | * Get the default mime type |
398 | * |
399 | * @return string |
400 | */ |
401 | public function getDefaultMimeType(): string |
402 | { |
403 | return self::$defaultMimeType; |
404 | } |
405 | |
406 | /** |
407 | * Set the default mime type |
408 | * |
409 | * @return bool |
410 | */ |
411 | public function isDefaultMimeType(): bool |
412 | { |
413 | return ($this->mimeType == self::$defaultMimeType); |
414 | } |
415 | |
416 | /** |
417 | * Get the all common mime types |
418 | * |
419 | * @return array |
420 | */ |
421 | public function getAllMimeTypes(): array |
422 | { |
423 | return self::$mimeTypes; |
424 | } |
425 | |
426 | /** |
427 | * Does the file exist |
428 | * |
429 | * @return bool |
430 | */ |
431 | public function exists(): bool |
432 | { |
433 | $fullPath = ($this->hasPath()) ? $this->path . DIRECTORY_SEPARATOR . $this->basename : $this->basename; |
434 | return file_exists($fullPath); |
435 | } |
436 | |
437 | /** |
438 | * Get the file contents |
439 | * |
440 | * @return mixed |
441 | */ |
442 | public function getContents(): mixed |
443 | { |
444 | $fullPath = ($this->hasPath()) ? $this->path . DIRECTORY_SEPARATOR . $this->basename : $this->basename; |
445 | return file_get_contents($fullPath); |
446 | } |
447 | |
448 | /** |
449 | * Convert file to an array |
450 | * |
451 | * @return array |
452 | */ |
453 | public function toArray(): array |
454 | { |
455 | return [ |
456 | 'basename' => $this->basename, |
457 | 'filename' => $this->filename, |
458 | 'extension' => $this->extension, |
459 | 'path' => $this->path, |
460 | 'size' => $this->size, |
461 | 'mime_type' => $this->mimeType, |
462 | ]; |
463 | } |
464 | |
465 | /** |
466 | * To string |
467 | * |
468 | * @return string |
469 | */ |
470 | public function __toString(): string |
471 | { |
472 | return ($this->hasPath()) ? $this->path . DIRECTORY_SEPARATOR . $this->basename : $this->basename; |
473 | } |
474 | |
475 | } |