Skip to content

Commit 98598c6

Browse files
committed
feat(identifier): check existence on target field, check target hash is not null
1 parent b9ffcab commit 98598c6

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

src/Transformer/AbstractArrayTransformer.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use AutoMapper\Generator\UniqueVariableScope;
99
use AutoMapper\MapperContext;
1010
use AutoMapper\Metadata\PropertyMetadata;
11+
use PhpParser\Comment\Doc;
1112
use PhpParser\Node\Expr;
1213
use PhpParser\Node\Name;
1314
use PhpParser\Node\Scalar;
@@ -69,10 +70,15 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
6970
);
7071

7172
if ($propertyMapping->target->readAccessor !== null && $this->itemTransformer instanceof IdentifierHashInterface) {
73+
$targetHashVar = new Expr\Variable($uniqueVariableScope->getUniqueName('targetHash'));
74+
7275
$loopExistingStatements[] = new Stmt\If_($isDeepPopulateExpr, [
7376
'stmts' => [
74-
new Stmt\Expression(new Expr\Assign(new Expr\ArrayDimFetch($exisingValuesIndexed, $this->itemTransformer->getTargetHashExpression($loopRemoveValueVar)), $loopRemoveValueVar)),
75-
],
77+
new Stmt\Expression(new Expr\Assign($targetHashVar, $this->itemTransformer->getTargetHashExpression($loopRemoveValueVar))),
78+
new Stmt\If_(new Expr\BinaryOp\NotIdentical(new Expr\ConstFetch(new Name('null')), $targetHashVar), [
79+
'stmts' => [new Stmt\Expression(new Expr\Assign(new Expr\ArrayDimFetch($exisingValuesIndexed, $targetHashVar), $loopRemoveValueVar))],
80+
])
81+
],
7682
]);
7783
}
7884

@@ -96,17 +102,24 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
96102
new Stmt\Expression($propertyMapping->target->writeMutator->getExpression($target, $mappedValueVar, $assignByRef)),
97103
],
98104
]);
99-
100-
// @TODO handle existingValue
101105
} else {
102106
$loopExistingValueVar = new Expr\Variable($uniqueVariableScope->getUniqueName('existingValue'));
103107

104108
if ($propertyMapping->target->readAccessor !== null && $this->itemTransformer instanceof IdentifierHashInterface) {
105109
$hashValueVariable = new Expr\Variable($uniqueVariableScope->getUniqueName('hashValue'));
106-
$statements[] = new Stmt\If_(new Expr\BinaryOp\Coalesce(
110+
111+
$isDeepPopulateExpr = new Expr\BinaryOp\Coalesce(
107112
new Expr\ArrayDimFetch(new Expr\Variable('context'), new Scalar\String_(MapperContext::DEEP_TARGET_TO_POPULATE)),
108113
new Expr\ConstFetch(new Name('false'))
109-
), [
114+
);
115+
116+
$isDefinedExpr = $propertyMapping->target->readAccessor->getIsDefinedExpression(new Expr\Variable('result'));
117+
118+
if ($isDefinedExpr !== null) {
119+
$isDeepPopulateExpr = new Expr\BinaryOp\BooleanAnd($isDeepPopulateExpr, $isDefinedExpr);
120+
}
121+
122+
$statements[] = new Stmt\If_($isDeepPopulateExpr, [
110123
'stmts' => [
111124
new Stmt\Foreach_($propertyMapping->target->readAccessor->getExpression($target), $loopExistingValueVar, [
112125
'stmts' => [

0 commit comments

Comments
 (0)