Skip to content

Commit

Permalink
Refactor ArrayPropertyMappingPopulator (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdreesen committed May 7, 2024
1 parent f70babb commit ea3f6d5
Showing 1 changed file with 38 additions and 26 deletions.
64 changes: 38 additions & 26 deletions src/Populator/ArrayPropertyMappingPopulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,64 @@
*/
final class ArrayPropertyMappingPopulator implements Populator
{
/** @var \Closure(mixed, TContext=):mixed */
private \Closure $mapper;
private ?string $sourceArrayItemProperty;
/** @var \Closure(mixed, TContext=):mixed|null */
private ?\Closure $mapper;
private PropertyAccessorInterface $arrayItemAccessor;
private PropertyAccessorInterface $accessor;
private PropertyMappingPopulator $populator;

/**
* @param \Closure(mixed, TContext=):mixed|null $mapper
*/
public function __construct(
private string $targetProperty,
private string $sourceArrayProperty,
private ?string $sourceArrayItemProperty = null,
string $targetProperty,
string $sourceArrayProperty,
?string $sourceArrayItemProperty = null,
?\Closure $mapper = null,
?PropertyAccessorInterface $arrayItemAccessor = null,
?PropertyAccessorInterface $accessor = null,
) {
$this->mapper = $mapper ?? static fn ($v) => $v;
$this->sourceArrayItemProperty = $sourceArrayItemProperty;
$this->mapper = $mapper;
$this->arrayItemAccessor = $arrayItemAccessor ?? PropertyAccess::createPropertyAccessor();
$this->accessor = $accessor ?? PropertyAccess::createPropertyAccessor();
$this->populator = new PropertyMappingPopulator(
$targetProperty,
$sourceArrayProperty,
null,
$this->unwrapAndMap(...),
$accessor,
);
}

/**
* @throws PopulationException
*/
public function populate(object $target, object $source, ?object $ctx = null): void
{
try {
$sourceArrayPropertyValues = $this->accessor->getValue($source, $this->sourceArrayProperty);
$this->populator->populate($target, $source, $ctx);
}

$unwrappedArray = [];
if (\is_array($sourceArrayPropertyValues) && [] !== $sourceArrayPropertyValues) {
$unwrappedArray = array_map(
fn ($item) => null !== $this->sourceArrayItemProperty
? $this->arrayItemAccessor->getValue($item, $this->sourceArrayItemProperty)
: $item,
$sourceArrayPropertyValues,
);
}
/**
* @param TContext $ctx
*
* @return array<mixed>
*/
private function unwrapAndMap(mixed $values, ?object $ctx = null): array
{
if (!\is_array($values) || [] === $values) {
return [];
}

$this->accessor->setValue(
$target,
$this->targetProperty,
array_map(fn ($item) => ($this->mapper)($item, $ctx), $unwrappedArray),
);
} catch (\Throwable $exception) {
throw new PopulationException($this->sourceArrayProperty, $this->targetProperty, $exception);
if (null === $this->sourceArrayItemProperty) {
return $this->mapper ? array_map(fn ($item) => ($this->mapper)($item, $ctx), $values) : $values;
}

$mapper = fn ($item) => $this->arrayItemAccessor->getValue($item, $this->sourceArrayItemProperty);

if ($this->mapper) {
$mapper = fn ($item) => ($this->mapper)($mapper($item), $ctx);
}

return array_map($mapper, $values);
}
}

0 comments on commit ea3f6d5

Please sign in to comment.