Skip to content

Commit

Permalink
refactor(TraversableToArrayAccessTransformer): Refactor for clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 31, 2024
1 parent 812435a commit 77ea752
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 24 additions & 3 deletions src/Transformer/TraversableToArrayAccessTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed,mixed> $source
* @param \ArrayAccess<mixed,mixed>|array<array-key,mixed> $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) {
Expand All @@ -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,
Expand Down

0 comments on commit 77ea752

Please sign in to comment.