|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace TinyBlocks\Mapper\Internal\Builders; |
| 6 | + |
| 7 | +use ReflectionClass; |
| 8 | +use ReflectionException; |
| 9 | +use ReflectionMethod; |
| 10 | +use ReflectionParameter; |
| 11 | +use TinyBlocks\Mapper\Internal\Extractors\ReflectionExtractor; |
| 12 | +use TinyBlocks\Mapper\Internal\Mappers\Object\Casters\CasterHandler; |
| 13 | + |
| 14 | +final readonly class ObjectBuilder |
| 15 | +{ |
| 16 | + public function __construct(private ReflectionExtractor $extractor) |
| 17 | + { |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * @template T of object |
| 22 | + * @param class-string<T> $class |
| 23 | + * @return T |
| 24 | + * @throws ReflectionException |
| 25 | + */ |
| 26 | + public function build(iterable $iterable, string $class): object |
| 27 | + { |
| 28 | + $reflection = new ReflectionClass(objectOrClass: $class); |
| 29 | + $parameters = $this->extractor->extractConstructorParameters(class: $class); |
| 30 | + $inputProperties = iterator_to_array(iterator: $iterable); |
| 31 | + |
| 32 | + $arguments = $this->buildArguments( |
| 33 | + parameters: $parameters, |
| 34 | + inputProperties: $inputProperties |
| 35 | + ); |
| 36 | + |
| 37 | + return $this->instantiate(reflection: $reflection, arguments: $arguments); |
| 38 | + } |
| 39 | + |
| 40 | + protected function buildArguments(array $parameters, array $inputProperties): array |
| 41 | + { |
| 42 | + $arguments = []; |
| 43 | + |
| 44 | + /** @var ReflectionParameter $parameter */ |
| 45 | + foreach ($parameters as $parameter) { |
| 46 | + $name = $parameter->getName(); |
| 47 | + $value = $inputProperties[$name] ?? null; |
| 48 | + |
| 49 | + $arguments[] = $value !== null |
| 50 | + ? $this->castValue(parameter: $parameter, value: $value) |
| 51 | + : $this->getDefaultValue(parameter: $parameter); |
| 52 | + } |
| 53 | + |
| 54 | + return $arguments; |
| 55 | + } |
| 56 | + |
| 57 | + protected function castValue(ReflectionParameter $parameter, mixed $value): mixed |
| 58 | + { |
| 59 | + $caster = new CasterHandler(parameter: $parameter); |
| 60 | + return $caster->castValue(value: $value); |
| 61 | + } |
| 62 | + |
| 63 | + protected function getDefaultValue(ReflectionParameter $parameter): mixed |
| 64 | + { |
| 65 | + return $parameter->isDefaultValueAvailable() |
| 66 | + ? $parameter->getDefaultValue() |
| 67 | + : null; |
| 68 | + } |
| 69 | + |
| 70 | + protected function instantiate(ReflectionClass $reflection, array $arguments): object |
| 71 | + { |
| 72 | + $constructor = $reflection->getConstructor(); |
| 73 | + |
| 74 | + if ($constructor === null) { |
| 75 | + return $reflection->newInstance(); |
| 76 | + } |
| 77 | + |
| 78 | + if ($constructor->isPrivate()) { |
| 79 | + return $this->instantiateWithPrivateConstructor( |
| 80 | + reflection: $reflection, |
| 81 | + constructor: $constructor, |
| 82 | + arguments: $arguments |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + return $reflection->newInstanceArgs(args: $arguments); |
| 87 | + } |
| 88 | + |
| 89 | + protected function instantiateWithPrivateConstructor( |
| 90 | + ReflectionClass $reflection, |
| 91 | + ReflectionMethod $constructor, |
| 92 | + array $arguments |
| 93 | + ): object { |
| 94 | + $instance = $reflection->newInstanceWithoutConstructor(); |
| 95 | + $constructor->invokeArgs(object: $instance, args: $arguments); |
| 96 | + return $instance; |
| 97 | + } |
| 98 | +} |
0 commit comments