dimtrovich /
validation
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of Dimtrovich/Validation. |
||
| 5 | * |
||
| 6 | * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]> |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view |
||
| 9 | * the LICENSE file that was distributed with this source code. |
||
| 10 | */ |
||
| 11 | |||
| 12 | namespace Dimtrovich\Validation\Spec; |
||
| 13 | |||
| 14 | use BlitzPHP\Utilities\Helpers; |
||
| 15 | use LogicException; |
||
| 16 | |||
| 17 | class FileFactory |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Create a new fake file. |
||
| 21 | */ |
||
| 22 | public function create(string $name, int|string $kilobytes = 0, ?string $mimeType = null): File |
||
| 23 | { |
||
| 24 | if (is_string($kilobytes)) { |
||
| 25 | 2 | return $this->createWithContent($name, $kilobytes); |
|
| 26 | } |
||
| 27 | |||
| 28 | return Helpers::tap(new File($name, tmpfile()), function ($file) use ($kilobytes, $mimeType) { |
||
| 29 | 2 | $file->sizeToReport = $kilobytes * 1024; |
|
| 30 | 2 | $file->mimeTypeToReport = $mimeType; |
|
| 31 | 2 | }); |
|
| 32 | } |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Create a new fake file with content. |
||
| 36 | */ |
||
| 37 | public function createWithContent(string $name, string $content): File |
||
| 38 | { |
||
| 39 | 2 | $tmpfile = tmpfile(); |
|
| 40 | |||
| 41 | 2 | fwrite($tmpfile, $content); |
|
| 42 | |||
| 43 | return Helpers::tap(new File($name, $tmpfile), function ($file) use ($tmpfile) { |
||
| 44 | 2 | $file->sizeToReport = fstat($tmpfile)['size']; |
|
| 45 | 2 | }); |
|
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Create a new fake image. |
||
| 50 | * |
||
| 51 | * @throws LogicException |
||
| 52 | */ |
||
| 53 | public function image(string $name, int $width = 10, int $height = 10): File |
||
| 54 | { |
||
| 55 | return new File($name, $this->generateImage( |
||
| 56 | $width, |
||
| 57 | $height, |
||
| 58 | pathinfo($name, PATHINFO_EXTENSION) |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 59 | )); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Generate a dummy image of the given width and height. |
||
| 64 | * |
||
| 65 | * @return resource |
||
| 66 | * |
||
| 67 | * @throws LogicException |
||
| 68 | */ |
||
| 69 | protected function generateImage(int $width, int $height, string $extension) |
||
| 70 | { |
||
| 71 | if (! function_exists('imagecreatetruecolor')) { |
||
| 72 | throw new LogicException('GD extension is not installed.'); |
||
| 73 | } |
||
| 74 | |||
| 75 | return Helpers::tap(tmpfile(), function ($temp) use ($width, $height, $extension) { |
||
| 76 | ob_start(); |
||
| 77 | |||
| 78 | $extension = in_array($extension, ['jpeg', 'png', 'gif', 'webp', 'wbmp', 'bmp'], true) |
||
| 79 | ? strtolower($extension) |
||
| 80 | : 'jpeg'; |
||
| 81 | |||
| 82 | $image = imagecreatetruecolor($width, $height); |
||
| 83 | |||
| 84 | if (! function_exists($functionName = "image{$extension}")) { |
||
| 85 | ob_get_clean(); |
||
| 86 | |||
| 87 | throw new LogicException("{$functionName} function is not defined and image cannot be generated."); |
||
| 88 | } |
||
| 89 | |||
| 90 | $functionName($image); |
||
| 91 | |||
| 92 | fwrite($temp, ob_get_clean()); |
||
| 93 | }); |
||
| 94 | } |
||
| 95 | } |
||
| 96 |