/** * {@inheritdoc} */ public function mapToObject(callable $callable, array $arguments) { if (count($arguments) > 0 && $this->isIndexedArray($arguments)) { throw new InvalidCallableArgumentsException('Can not map indexed arrays'); } $reflectionFunction = ReflectionFunctionFactory::createFromCallable($callable); if ($reflectionFunction->getNumberOfRequiredParameters() > 1) { throw new InvalidMethodParametersException('Could not map to more than one parameter'); } $reflectionParameters = $reflectionFunction->getParameters(); /** @var \ReflectionParameter $targetReflectionParameter */ $targetReflectionParameter = reset($reflectionParameters); if (null === $targetReflectionParameter->getClass()) { throw new InvalidMethodParametersException('Method parameter should have type definition'); } $mapped = $this->normalizer->denormalize($targetReflectionParameter->getClass()->name, $arguments); return $mapped; }
public function it_should_maps_arguments_to_object(NormalizerInterface $normalizer) { $normalizer->denormalize(SomeRequestObj::class, ['a' => 1, 'b' => 2])->willReturn(new SomeRequestObj(1, 2))->shouldBeCalled(); $this->mapToObject([new SomeGoodService(), 'someMethod'], ['a' => 1, 'b' => 2])->shouldBeLike(new SomeRequestObj(1, 2)); }