-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from teamneusta/feature/ArrayConvertingPopulator
New Feature: ArrayConvertingPopulator
- Loading branch information
Showing
17 changed files
with
762 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neusta\ConverterBundle\Populator; | ||
|
||
use Neusta\ConverterBundle\Converter; | ||
use Neusta\ConverterBundle\Exception\PopulationException; | ||
use Neusta\ConverterBundle\Populator; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
|
||
/** | ||
* A populator that uses a converter to convert a field of type array<TInnerSource> from TSource | ||
* into an object of type array<TInnerTarget> for a field of TTarget. | ||
* | ||
* @template TSource of object | ||
* @template TTarget of object | ||
* @template TContext of object|null | ||
* | ||
* @implements Populator<TSource, TTarget, TContext> | ||
*/ | ||
final class ArrayConvertingPopulator implements Populator | ||
{ | ||
private ArrayPropertyMappingPopulator $populator; | ||
|
||
/** | ||
* @template TInnerSource of object | ||
* @template TInnerTarget of object | ||
* | ||
* @param Converter<TInnerSource, TInnerTarget, TContext> $converter | ||
*/ | ||
public function __construct( | ||
Converter $converter, | ||
string $sourceArrayPropertyName, | ||
string $targetPropertyName, | ||
?string $sourceArrayItemPropertyName = null, | ||
PropertyAccessorInterface $itemAccessor = null, | ||
PropertyAccessorInterface $accessor = null, | ||
) | ||
{ | ||
$this->populator = new ArrayPropertyMappingPopulator( | ||
$targetPropertyName, | ||
$sourceArrayPropertyName, | ||
$sourceArrayItemPropertyName, | ||
\Closure::fromCallable([$converter, 'convert']), | ||
$itemAccessor, | ||
$accessor, | ||
); | ||
} | ||
|
||
/** | ||
* @throws PopulationException | ||
*/ | ||
public function populate(object $target, object $source, ?object $ctx = null): void | ||
{ | ||
$this->populator->populate($target, $source, $ctx); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Neusta\ConverterBundle\Populator; | ||
|
||
use Neusta\ConverterBundle\Exception\PopulationException; | ||
use Neusta\ConverterBundle\Populator; | ||
use Symfony\Component\PropertyAccess\PropertyAccess; | ||
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; | ||
|
||
/** | ||
* @template TSource of object | ||
* @template TTarget of object | ||
* @template TContext of object|null | ||
* | ||
* @implements Populator<TSource, TTarget, TContext> | ||
*/ | ||
final class ArrayPropertyMappingPopulator implements Populator | ||
{ | ||
/** @var \Closure(mixed, TContext=):mixed */ | ||
private \Closure $mapper; | ||
private PropertyAccessorInterface $itemAccessor; | ||
private PropertyAccessorInterface $accessor; | ||
|
||
/** | ||
* @param \Closure(mixed, TContext=):mixed|null $mapper | ||
*/ | ||
public function __construct( | ||
private string $targetProperty, | ||
private string $sourceArrayProperty, | ||
private ?string $sourceArrayItemProperty = null, | ||
?\Closure $mapper = null, | ||
PropertyAccessorInterface $itemAccessor = null, | ||
PropertyAccessorInterface $accessor = null, | ||
) | ||
{ | ||
$this->mapper = $mapper ?? static fn($v) => $v; | ||
$this->itemAccessor = $itemAccessor ?? PropertyAccess::createPropertyAccessor(); | ||
$this->accessor = $accessor ?? PropertyAccess::createPropertyAccessor(); | ||
} | ||
|
||
/** | ||
* @throws PopulationException | ||
*/ | ||
public function populate(object $target, object $source, ?object $ctx = null): void | ||
{ | ||
try { | ||
$unwrappedArray = array_map( | ||
function ($arrayItem) { | ||
if (!empty($this->sourceArrayItemProperty)) { | ||
return $this->itemAccessor->getValue($arrayItem, $this->sourceArrayItemProperty); | ||
} | ||
return $arrayItem; | ||
}, | ||
$this->accessor->getValue($source, $this->sourceArrayProperty) | ||
); | ||
|
||
|
||
$this->accessor->setValue( | ||
$target, | ||
$this->targetProperty, | ||
array_map( | ||
function ($item) use ($ctx) { | ||
return ($this->mapper)($item, $ctx); | ||
}, | ||
$unwrappedArray | ||
) | ||
); | ||
} catch (\Throwable $exception) { | ||
throw new PopulationException($this->sourceArrayProperty, $this->targetProperty, $exception); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.