|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Constructo\Testing\Faker; |
| 6 | + |
| 7 | +use Constructo\Contract\Formatter; |
| 8 | +use Constructo\Contract\Testing\Faker as Contract; |
| 9 | +use Constructo\Support\Reflective\Engine; |
| 10 | +use Constructo\Support\Reflective\Factory\Target; |
| 11 | +use Constructo\Support\Reflective\Notation; |
| 12 | +use Constructo\Support\Set; |
| 13 | +use Constructo\Testing\Faker\Resolver\FromCollection; |
| 14 | +use Constructo\Testing\Faker\Resolver\FromDefaultValue; |
| 15 | +use Constructo\Testing\Faker\Resolver\FromDependency; |
| 16 | +use Constructo\Testing\Faker\Resolver\FromEnum; |
| 17 | +use Constructo\Testing\Faker\Resolver\FromPreset; |
| 18 | +use Constructo\Testing\Faker\Resolver\FromTypeAttributes; |
| 19 | +use Constructo\Testing\Faker\Resolver\FromTypeBuiltin; |
| 20 | +use Constructo\Testing\Faker\Resolver\FromTypeDate; |
| 21 | +use Faker\Factory; |
| 22 | +use Faker\Generator; |
| 23 | +use ReflectionException; |
| 24 | +use ReflectionParameter; |
| 25 | + |
| 26 | +use function Constructo\Cast\stringify; |
| 27 | +use function getenv; |
| 28 | + |
| 29 | + |
| 30 | +class Faker extends Engine implements Contract |
| 31 | +{ |
| 32 | + protected readonly Generator $generator; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param array<callable|Formatter> $formatters |
| 36 | + * @SuppressWarnings(StaticAccess) |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + Notation $case = Notation::SNAKE, |
| 40 | + array $formatters = [], |
| 41 | + ?string $locale = null, |
| 42 | + ) { |
| 43 | + parent::__construct($case, $formatters); |
| 44 | + |
| 45 | + $this->generator = Factory::create($this->locale($locale)); |
| 46 | + } |
| 47 | + |
| 48 | + public function __call(string $name, array $arguments): mixed |
| 49 | + { |
| 50 | + return $this->generate($name, $arguments); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @template U of object |
| 55 | + * @param class-string<U> $class |
| 56 | + * @throws ReflectionException |
| 57 | + */ |
| 58 | + public function fake(string $class, array $presets = []): Set |
| 59 | + { |
| 60 | + $target = Target::createFrom($class); |
| 61 | + $parameters = $target->getReflectionParameters(); |
| 62 | + if (empty($parameters)) { |
| 63 | + return Set::createFrom([]); |
| 64 | + } |
| 65 | + |
| 66 | + return $this->resolveParameters($parameters, new Set($presets)); |
| 67 | + } |
| 68 | + |
| 69 | + public function generate(string $name, array $arguments = []): mixed |
| 70 | + { |
| 71 | + return $this->generator->__call($name, $arguments); |
| 72 | + } |
| 73 | + |
| 74 | + public function generator(): Generator |
| 75 | + { |
| 76 | + return $this->generator; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param array<ReflectionParameter> $parameters |
| 81 | + */ |
| 82 | + private function resolveParameters(array $parameters, Set $presets): Set |
| 83 | + { |
| 84 | + $values = []; |
| 85 | + foreach ($parameters as $parameter) { |
| 86 | + $field = $this->casedField($parameter); |
| 87 | + $generated = (new FromDependency($this->notation)) |
| 88 | + ->then(new FromTypeDate($this->notation)) |
| 89 | + ->then(new FromCollection($this->notation)) |
| 90 | + ->then(new FromTypeBuiltin($this->notation)) |
| 91 | + ->then(new FromTypeAttributes($this->notation)) |
| 92 | + ->then(new FromEnum($this->notation)) |
| 93 | + ->then(new FromDefaultValue($this->notation)) |
| 94 | + ->then(new FromPreset($this->notation)) |
| 95 | + ->resolve($parameter, $presets); |
| 96 | + |
| 97 | + if ($generated === null) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + $values[$field] = $generated->content; |
| 101 | + } |
| 102 | + return Set::createFrom($values); |
| 103 | + } |
| 104 | + |
| 105 | + private function locale(?string $locale): string |
| 106 | + { |
| 107 | + $fallback = static function (string $default = 'en_US'): string { |
| 108 | + $locale = stringify(getenv('FAKER_LOCALE'), $default); |
| 109 | + return empty($locale) ? $default : $locale; |
| 110 | + }; |
| 111 | + return $locale ?? $fallback(); |
| 112 | + } |
| 113 | +} |
0 commit comments