|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace GoodPhp\LaravelIntegration\Routing; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use GoodPhp\Reflection\Reflector\Reflection\FunctionParameterReflection; |
| 7 | +use GoodPhp\Reflection\Reflector\Reflection\MethodReflection; |
| 8 | +use GoodPhp\Serialization\Serializer; |
| 9 | +use GoodPhp\Serialization\TypeAdapter\Exception\CollectionItemMappingException; |
| 10 | +use GoodPhp\Serialization\TypeAdapter\Exception\MultipleMappingException; |
| 11 | +use GoodPhp\Serialization\TypeAdapter\Primitive\ClassProperties\PropertyMappingException; |
| 12 | +use GoodPhp\Serialization\TypeAdapter\Primitive\PrimitiveTypeAdapter; |
| 13 | +use Illuminate\Container\Container; |
| 14 | +use Illuminate\Http\Request; |
| 15 | +use Illuminate\Routing\ControllerDispatcher; |
| 16 | +use Illuminate\Support\Arr; |
| 17 | +use Illuminate\Validation\ValidationException; |
| 18 | +use ReflectionParameter; |
| 19 | + |
| 20 | +class SerializationInjectingControllerDispatcher extends ControllerDispatcher |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + Container $container, |
| 24 | + private readonly Serializer $serializer, |
| 25 | + private readonly \GoodPhp\Reflection\Reflector\Reflector $reflector, |
| 26 | + ) { |
| 27 | + parent::__construct($container); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Attempt to transform the given parameter into a class instance. |
| 32 | + * |
| 33 | + * @param array $parameters |
| 34 | + * @param object $skippableValue |
| 35 | + * |
| 36 | + * @return mixed |
| 37 | + */ |
| 38 | + protected function transformDependency(ReflectionParameter $parameter, $parameters, $skippableValue) |
| 39 | + { |
| 40 | + if ($parameter->getAttributes(Input::class)) { |
| 41 | + $type = $this->reflector |
| 42 | + ->forType($parameter->getDeclaringClass()->getName()) |
| 43 | + ->methods() |
| 44 | + ->first(fn (MethodReflection $method) => $method->name() === $parameter->getDeclaringFunction()->getShortName()) |
| 45 | + ->parameters() |
| 46 | + ->first(fn (FunctionParameterReflection $goodParameter) => $parameter->name === $goodParameter->name()) |
| 47 | + ->type(); |
| 48 | + |
| 49 | + try { |
| 50 | + return $this->serializer->adapter(PrimitiveTypeAdapter::class, $type)->deserialize( |
| 51 | + $this->container->make(Request::class)->input(), |
| 52 | + ); |
| 53 | + } catch (PropertyMappingException|CollectionItemMappingException|MultipleMappingException $e) { |
| 54 | + throw ValidationException::withMessages(Arr::dot($this->extractErrors($e))); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return parent::transformDependency($parameter, $parameters, $skippableValue); |
| 59 | + } |
| 60 | + |
| 61 | + private function extractErrors(Exception $e): array|string |
| 62 | + { |
| 63 | + if ($e instanceof PropertyMappingException) { |
| 64 | + return [ |
| 65 | + $e->path => $this->extractErrors($e->getPrevious()), |
| 66 | + ]; |
| 67 | + } |
| 68 | + |
| 69 | + if ($e instanceof CollectionItemMappingException) { |
| 70 | + return [ |
| 71 | + $e->key => $this->extractErrors($e->getPrevious()), |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + if ($e instanceof MultipleMappingException) { |
| 76 | + return collect($e->exceptions)->mapWithKeys(function (PropertyMappingException|CollectionItemMappingException $e) { |
| 77 | + return [ |
| 78 | + $e instanceof PropertyMappingException ? $e->path : $e->key => $this->extractErrors($e->getPrevious()), |
| 79 | + ]; |
| 80 | + })->all(); |
| 81 | + } |
| 82 | + |
| 83 | + return $e->getMessage(); |
| 84 | + } |
| 85 | +} |
0 commit comments