From 77ea752bd292c430c916ee93fa1f94e23d1227e4 Mon Sep 17 00:00:00 2001 From: Priyadi Iman Nurcahyo <1102197+priyadi@users.noreply.github.com> Date: Wed, 31 Jan 2024 23:56:51 +0700 Subject: [PATCH] refactor(`TraversableToArrayAccessTransformer`): Refactor for clarity. --- CHANGELOG.md | 2 ++ .../TraversableToArrayAccessTransformer.php | 27 ++++++++++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1710e5c1..542d2b25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ * refactor(`TraversableTransformerTrait`): Refactor for clarity. * refactor(`ArrayLikeTransformerTrait`): Rename for clarity. * refactor(`ArrayLikeMetadata`): Add `$type`. +* refactor(`PropertyMapping`): Add `$sourceTypes`. +* refactor(`TraversableToArrayAccessTransformer`): Refactor for clarity. ## 0.5.22 diff --git a/src/Transformer/TraversableToArrayAccessTransformer.php b/src/Transformer/TraversableToArrayAccessTransformer.php index 3ecdffde..6b6424f8 100644 --- a/src/Transformer/TraversableToArrayAccessTransformer.php +++ b/src/Transformer/TraversableToArrayAccessTransformer.php @@ -69,6 +69,28 @@ public function transform( $targetMetadata = $this->arrayLikeMetadataFactory ->createArrayLikeMetadata($targetType); + // Transform source + + $target = $this->doTransform( + source: $source, + target: $target, + targetMetadata: $targetMetadata, + context: $context, + ); + + return $target; + } + + /** + * @param iterable $source + * @param \ArrayAccess|array $target + */ + private function doTransform( + iterable $source, + \ArrayAccess|array|null $target, + ArrayLikeMetadata $targetMetadata, + Context $context + ): mixed { // If the target is not provided, instantiate it if ($target === null) { @@ -78,11 +100,10 @@ public function transform( // Add the target to cache $context(ObjectCache::class) - ->saveTarget($source, $targetType, $target, $context); + ->saveTarget($source, $targetMetadata->getType(), $target, $context); - // Transform source + // Transform the source - /** @psalm-suppress MixedArgumentTypeCoercion */ $transformed = $this->transformTraversableSource( source: $source, target: $target,