Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ArrayPropertyMappingPopulator #74

Merged
merged 2 commits into from
May 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
jdreesen marked this conversation as resolved.
Show resolved Hide resolved
?\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);
jdreesen marked this conversation as resolved.
Show resolved Hide resolved
}

$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);
}
}