diff --git a/src/Exception/UnableToFindSuitableTransformerException.php b/src/Exception/UnableToFindSuitableTransformerException.php index e62b452..80e1d9f 100644 --- a/src/Exception/UnableToFindSuitableTransformerException.php +++ b/src/Exception/UnableToFindSuitableTransformerException.php @@ -20,8 +20,8 @@ class UnableToFindSuitableTransformerException extends NotMappableValueException { /** - * @param array $sourceTypes - * @param array $targetTypes + * @param array $sourceTypes + * @param array $targetTypes */ public function __construct(array $sourceTypes, array $targetTypes) { diff --git a/src/MainTransformer/MainTransformer.php b/src/MainTransformer/MainTransformer.php index 31901cc..fc6797b 100644 --- a/src/MainTransformer/MainTransformer.php +++ b/src/MainTransformer/MainTransformer.php @@ -22,6 +22,7 @@ use Rekalogika\Mapper\ObjectCache\ObjectCacheFactoryInterface; use Rekalogika\Mapper\TransformerRegistry\TransformerRegistryInterface; use Rekalogika\Mapper\TypeResolver\TypeResolverInterface; +use Symfony\Component\PropertyInfo\Type; class MainTransformer implements MainTransformerInterface { @@ -70,6 +71,24 @@ public static function getObjectCache( return $objectCache; } + /** + * @param array $types + * @return array + */ + private function getSimpleTypes( + array $types + ): array { + $simpleTypes = []; + + foreach ($types as $type) { + foreach ($this->typeResolver->getSimpleTypes($type) as $simpleType) { + $simpleTypes[] = $simpleType; + } + } + + return $simpleTypes; + } + public function transform( mixed $source, mixed $target, @@ -96,34 +115,31 @@ public function transform( // gets simple target types from the provided target type - $simpleTargetTypes = []; - - foreach ($targetTypes as $targetType) { - foreach ($this->typeResolver->getSimpleTypes($targetType) as $simpleType) { - $simpleTargetTypes[] = $simpleType; - } - } + $targetTypes = $this->getSimpleTypes($targetTypes); // guess the source type - $sourceType = $this->typeResolver->guessTypeFromVariable($source); + $sourceTypes = [$this->typeResolver->guessTypeFromVariable($source)]; - // iterate simple target types and find the suitable transformer + // search for the matching transformers according to the source and + // target types $searchResult = $this->transformerRegistry ->findBySourceAndTargetTypes( - [$sourceType], - $simpleTargetTypes + $sourceTypes, + $targetTypes ); + // loop over the result and transform the source to the target + foreach ($searchResult as $searchEntry) { $transformer = $this->processTransformer($searchEntry->getTransformer()); $sourceType = $searchEntry->getSourceType(); $sourceTypeForTransformer = $sourceType instanceof MixedType ? null : $sourceType; - $targetTypes = $searchEntry->getTargetType(); - $targetTypeForTransformer = $targetTypes instanceof MixedType ? null : $targetTypes; + $targetType = $searchEntry->getTargetType(); + $targetTypeForTransformer = $targetType instanceof MixedType ? null : $targetType; /** @var mixed */ $result = $transformer->transform( @@ -137,6 +153,6 @@ public function transform( return $result; } - throw new UnableToFindSuitableTransformerException([$sourceType], $simpleTargetTypes); + throw new UnableToFindSuitableTransformerException($sourceTypes, $targetTypes); } }