Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
33 / 33 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| SvgCompanyLogoFileFaker | |
100.00% |
33 / 33 |
|
100.00% |
6 / 6 |
11 | |
100.00% |
1 / 1 |
| createOriginalFilename | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createMimeType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createResource | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createSvg | |
100.00% |
23 / 23 |
|
100.00% |
1 / 1 |
2 | |||
| shape | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
5 | |||
| 1 | <?php |
| 2 | namespace Apie\Faker\FileFakers; |
| 3 | |
| 4 | use Apie\Faker\Interfaces\ApieImageFileFaker; |
| 5 | use Faker\Generator; |
| 6 | use Nyholm\Psr7\Stream; |
| 7 | |
| 8 | /** |
| 9 | * Generates a fictional company logo from a random arrangement of geometric shapes. |
| 10 | */ |
| 11 | final class SvgCompanyLogoFileFaker implements ApieImageFileFaker |
| 12 | { |
| 13 | private const COLORS = ['#264653', '#2A9D8F', '#E9C46A', '#F4A261', '#E76F51', '#3A506B', '#5BC0BE', '#9B5DE5', '#F15BB5', '#00BBF9', '#00F5D4', '#FF6B6B']; |
| 14 | |
| 15 | public function createOriginalFilename(Generator $faker): string |
| 16 | { |
| 17 | return $faker->word() . '.svg'; |
| 18 | } |
| 19 | |
| 20 | public function createMimeType(): string |
| 21 | { |
| 22 | return 'image/svg+xml'; |
| 23 | } |
| 24 | |
| 25 | public static function isSupported(): bool |
| 26 | { |
| 27 | return true; |
| 28 | } |
| 29 | |
| 30 | /** @return resource */ |
| 31 | public function createResource(Generator $faker, string $originalFilename, string $mimeType): mixed |
| 32 | { |
| 33 | return Stream::create($this->createSvg($faker))->detach(); |
| 34 | } |
| 35 | |
| 36 | private function createSvg(Generator $faker): string |
| 37 | { |
| 38 | $colors = $faker->randomElements(self::COLORS, 3); |
| 39 | $elements = []; |
| 40 | $count = $faker->numberBetween(3, 5); |
| 41 | for ($i = 0; $i < $count; $i++) { |
| 42 | $angle = 360 * $i / $count + $faker->numberBetween(-12, 12); |
| 43 | $distance = $faker->numberBetween(28, 38); |
| 44 | $x = 128 + (int) round(cos(deg2rad($angle)) * $distance); |
| 45 | $y = 128 + (int) round(sin(deg2rad($angle)) * $distance); |
| 46 | $size = $faker->numberBetween(13, 23); |
| 47 | $color = $faker->randomElement($colors); |
| 48 | $shape = $faker->randomElement(['circle', 'diamond', 'square', 'triangle']); |
| 49 | |
| 50 | $elements[] = '<g transform="translate(' . $x . ' ' . $y . ') rotate(' . $faker->numberBetween(0, 359) . ')">' |
| 51 | . $this->shape($shape, $size, $color) |
| 52 | . '</g>'; |
| 53 | } |
| 54 | |
| 55 | $centerColor = $faker->randomElement($colors); |
| 56 | $centerShape = $faker->randomElement(['circle', 'diamond', 'square']); |
| 57 | $elements[] = '<g transform="translate(128 128) rotate(' . $faker->numberBetween(0, 359) . ')">' |
| 58 | . $this->shape($centerShape, $faker->numberBetween(12, 20), $centerColor) |
| 59 | . '</g>'; |
| 60 | |
| 61 | return '<?xml version="1.0" encoding="UTF-8"?>' |
| 62 | . '<svg xmlns="http://www.w3.org/2000/svg" width="256" height="256" viewBox="0 0 256 256">' |
| 63 | . implode('', $elements) |
| 64 | . '</svg>'; |
| 65 | } |
| 66 | |
| 67 | private function shape(string $shape, int $size, string $color): string |
| 68 | { |
| 69 | return match ($shape) { |
| 70 | 'diamond' => '<path d="M0 ' . -$size . ' L' . $size . ' 0 L0 ' . $size . ' L' . -$size . ' 0 Z" fill="' . $color . '"/>', |
| 71 | 'square' => '<rect x="' . -$size . '" y="' . -$size . '" width="' . ($size * 2) . '" height="' . ($size * 2) . '" rx="' . (int) round($size / 3) . '" fill="' . $color . '"/>', |
| 72 | 'triangle' => '<path d="M0 ' . -$size . ' L' . $size . ' ' . $size . ' L' . -$size . ' ' . $size . ' Z" fill="' . $color . '"/>', |
| 73 | default => '<circle r="' . $size . '" fill="' . $color . '"/>', |
| 74 | }; |
| 75 | } |
| 76 | } |