-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle DoctrineCollections within AutoMapper (#223)
- Loading branch information
Showing
8 changed files
with
176 additions
and
0 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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Transformer; | ||
|
||
use AutoMapper\Generator\UniqueVariableScope; | ||
use AutoMapper\Metadata\PropertyMetadata; | ||
use Doctrine\Common\Collections\ArrayCollection; | ||
use PhpParser\Node\Arg; | ||
use PhpParser\Node\Expr; | ||
use PhpParser\Node\Name; | ||
use PhpParser\Node\Stmt; | ||
|
||
/** | ||
* Transform an array to Money\Money object. | ||
* | ||
* @author Baptiste Leduc <baptiste.leduc@gmail.com> | ||
*/ | ||
final class ArrayToDoctrineCollectionTransformer implements TransformerInterface, DependentTransformerInterface, PrioritizedTransformerFactoryInterface | ||
{ | ||
public function __construct( | ||
protected TransformerInterface $itemTransformer, | ||
) { | ||
} | ||
|
||
public function transform(Expr $input, Expr $target, PropertyMetadata $propertyMapping, UniqueVariableScope $uniqueVariableScope, Expr\Variable $source): array | ||
{ | ||
/** | ||
* $collection = new ArrayCollection();. | ||
*/ | ||
$collectionVar = new Expr\Variable($uniqueVariableScope->getUniqueName('collection')); | ||
$statements = [ | ||
new Stmt\Expression(new Expr\Assign($collectionVar, new Expr\New_(new Name(ArrayCollection::class)))), | ||
]; | ||
|
||
$loopValueVar = new Expr\Variable($uniqueVariableScope->getUniqueName('value')); | ||
[$output, $itemStatements] = $this->itemTransformer->transform($loopValueVar, $target, $propertyMapping, $uniqueVariableScope, $source); | ||
$itemStatements[] = new Stmt\Expression(new Expr\MethodCall($collectionVar, 'add', [new Arg($output)])); | ||
|
||
$statements[] = new Stmt\Foreach_(new Expr\BinaryOp\Coalesce($input, new Expr\Array_()), $loopValueVar, [ | ||
'stmts' => $itemStatements, | ||
]); | ||
|
||
return [$collectionVar, $statements]; | ||
} | ||
|
||
public function getDependencies(): array | ||
{ | ||
if (!$this->itemTransformer instanceof DependentTransformerInterface) { | ||
return []; | ||
} | ||
|
||
return $this->itemTransformer->getDependencies(); | ||
} | ||
|
||
public function getPriority(): int | ||
{ | ||
return 0; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Transformer; | ||
|
||
use AutoMapper\Metadata\MapperMetadata; | ||
use AutoMapper\Metadata\SourcePropertyMetadata; | ||
use AutoMapper\Metadata\TargetPropertyMetadata; | ||
use AutoMapper\Metadata\TypesMatching; | ||
use Doctrine\Common\Collections\Collection; | ||
use Symfony\Component\PropertyInfo\Type; | ||
|
||
/** | ||
* @author Baptiste Leduc <baptiste.leduc@gmail.com> | ||
*/ | ||
final class DoctrineCollectionTransformerFactory extends AbstractUniqueTypeTransformerFactory implements ChainTransformerFactoryAwareInterface | ||
{ | ||
use ChainTransformerFactoryAwareTrait; | ||
|
||
protected function createTransformer(Type $sourceType, Type $targetType, SourcePropertyMetadata $source, TargetPropertyMetadata $target, MapperMetadata $mapperMetadata): ?TransformerInterface | ||
{ | ||
if (!interface_exists(Collection::class)) { | ||
return null; | ||
} | ||
|
||
if (Type::BUILTIN_TYPE_OBJECT !== $targetType->getBuiltinType() || !\is_string($targetType->getClassName())) { | ||
return null; | ||
} | ||
|
||
if (Collection::class === $targetType->getClassName() || (false !== ($classImplements = class_implements($targetType->getClassName())) && \in_array(Collection::class, $classImplements))) { | ||
$types = TypesMatching::fromSourceAndTargetTypes($sourceType->getCollectionValueTypes(), $targetType->getCollectionValueTypes()); | ||
|
||
$subItemTransformer = $this->chainTransformerFactory->getTransformer($types, $source, $target, $mapperMetadata); | ||
if (null === $subItemTransformer) { | ||
return null; | ||
} | ||
|
||
return new ArrayToDoctrineCollectionTransformer($subItemTransformer); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures\DoctrineCollections; | ||
|
||
class Book | ||
{ | ||
public function __construct( | ||
public string $name | ||
) { | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\Tests\Fixtures\DoctrineCollections; | ||
|
||
use Doctrine\Common\Collections\Collection; | ||
|
||
class Library | ||
{ | ||
/** @var Collection<Book> */ | ||
public Collection $books; | ||
} |