|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Component\AutoMapper; |
| 4 | + |
| 5 | +use Psr\Container\ContainerInterface; |
| 6 | +use Symfony\Component\AutoMapper\Exception\RuntimeException; |
| 7 | +use Symfony\Component\AutoMapper\Attributes\MapIf; |
| 8 | +use Symfony\Component\AutoMapper\Attributes\MapTo; |
| 9 | +use Symfony\Component\AutoMapper\Attributes\MapWith; |
| 10 | +use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * Object to object mapper. |
| 14 | + * |
| 15 | + * @experimental |
| 16 | + * @author Antoine Bluchet <soyuka@gmail.com> |
| 17 | + */ |
| 18 | +final class AutoMapper implements AutoMapperInterface |
| 19 | +{ |
| 20 | + public function __construct(private ?PropertyAccessorInterface $propertyAccessor = null, private ?ContainerInterface $serviceLocator = null) |
| 21 | + { |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * {@inheritdoc} |
| 26 | + */ |
| 27 | + public function map(object $object, object|string|null $to = null): mixed |
| 28 | + { |
| 29 | + $refl = new \ReflectionClass($object); |
| 30 | + |
| 31 | + if (!$to) { |
| 32 | + $to = $this->getAttribute($refl, MapTo::class, true)->to; |
| 33 | + } |
| 34 | + |
| 35 | + $arguments = []; |
| 36 | + if (is_object($to)) { |
| 37 | + $toRefl = new \ReflectionClass(get_class($to)); |
| 38 | + $mapped = $to; |
| 39 | + } else { |
| 40 | + $toRefl = new \ReflectionClass($to); |
| 41 | + $mapped = $toRefl->newInstanceWithoutConstructor(); |
| 42 | + } |
| 43 | + |
| 44 | + $constructor = $toRefl->getConstructor(); |
| 45 | + foreach ($constructor?->getParameters() ?? [] as $parameter) { |
| 46 | + $arguments[$parameter->getName()] = $parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null; |
| 47 | + } |
| 48 | + |
| 49 | + foreach ($refl->getProperties() as $property) { |
| 50 | + $propertyName = $property->getName(); |
| 51 | + $mapTo = $this->getAttribute($property, MapTo::class)?->to ?? $propertyName; |
| 52 | + if (!$toRefl->hasProperty($mapTo)) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + $value = $this->propertyAccessor ? $this->propertyAccessor->getValue($object, $propertyName) : $object->{$propertyName}; |
| 57 | + $mapIf = $this->getCallable($this->getAttribute($property, MapIf::class)?->if); |
| 58 | + if (is_callable($mapIf) && false === $this->call($mapIf, $value, $object)) { |
| 59 | + continue; |
| 60 | + } |
| 61 | + |
| 62 | + $mapWith = $this->getCallable($this->getAttribute($property, MapWith::class)?->with); |
| 63 | + if (is_callable($mapWith)) { |
| 64 | + $value = $this->call($mapWith, $value, $object); |
| 65 | + } |
| 66 | + |
| 67 | + if (is_object($value) && $to = $this->getAttribute(new \ReflectionClass(get_class($value)), MapTo::class)?->to) { |
| 68 | + $value = $this->map($value, $to); |
| 69 | + } |
| 70 | + |
| 71 | + if (array_key_exists($mapTo, $arguments)) { |
| 72 | + $arguments[$mapTo] = $value; |
| 73 | + } else { |
| 74 | + $this->propertyAccessor ? $this->propertyAccessor->setValue($mapped, $mapTo, $value) : ($mapped->{$mapTo} = $value); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + $constructor->invokeArgs($mapped, $arguments); |
| 79 | + |
| 80 | + return $mapped; |
| 81 | + } |
| 82 | + |
| 83 | + private function call(callable $fn, mixed $value, object $object): mixed |
| 84 | + { |
| 85 | + $refl = new \ReflectionFunction(\Closure::fromCallable($fn)); |
| 86 | + $withParameters = $refl->getParameters(); |
| 87 | + $withArgs = [$value]; |
| 88 | + |
| 89 | + // Let's not send object if we don't need to, gives the ability to call native functions |
| 90 | + foreach ($withParameters as $parameter) { |
| 91 | + if ($parameter->getName() === 'object') { |
| 92 | + $withArgs['object'] = $object; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return call_user_func_array($fn, $withArgs); |
| 98 | + } |
| 99 | + |
| 100 | + private function getCallable(string|callable|null $fn): ?callable |
| 101 | + { |
| 102 | + if ($this->serviceLocator && is_string($fn) && $this->serviceLocator->has($fn)) { |
| 103 | + return $this->serviceLocator->get($fn); |
| 104 | + } |
| 105 | + |
| 106 | + return $fn; |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param class-string $name |
| 111 | + */ |
| 112 | + private function getAttribute(mixed $refl, string $name, bool $throw = false): mixed |
| 113 | + { |
| 114 | + $a = $refl->getAttributes($name)[0] ?? null; |
| 115 | + |
| 116 | + if ($throw && !$a) { |
| 117 | + throw new RuntimeException(sprintf('Attribute of type "%s" expected on "%s.', $name, $refl->getName())); |
| 118 | + } |
| 119 | + |
| 120 | + return $a ? $a->newInstance() : $a; |
| 121 | + } |
| 122 | +} |
0 commit comments