@@ -69,10 +69,15 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
6969 );
7070
7171 if ($ propertyMapping ->target ->readAccessor !== null && $ this ->itemTransformer instanceof IdentifierHashInterface) {
72+ $ targetHashVar = new Expr \Variable ($ uniqueVariableScope ->getUniqueName ('targetHash ' ));
73+
7274 $ loopExistingStatements [] = new Stmt \If_ ($ isDeepPopulateExpr , [
7375 'stmts ' => [
74- new Stmt \Expression (new Expr \Assign (new Expr \ArrayDimFetch ($ exisingValuesIndexed , $ this ->itemTransformer ->getTargetHashExpression ($ loopRemoveValueVar )), $ loopRemoveValueVar )),
75- ],
76+ new Stmt \Expression (new Expr \Assign ($ targetHashVar , $ this ->itemTransformer ->getTargetHashExpression ($ loopRemoveValueVar ))),
77+ new Stmt \If_ (new Expr \BinaryOp \NotIdentical (new Expr \ConstFetch (new Name ('null ' )), $ targetHashVar ), [
78+ 'stmts ' => [new Stmt \Expression (new Expr \Assign (new Expr \ArrayDimFetch ($ exisingValuesIndexed , $ targetHashVar ), $ loopRemoveValueVar ))],
79+ ]),
80+ ],
7681 ]);
7782 }
7883
@@ -96,17 +101,24 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
96101 new Stmt \Expression ($ propertyMapping ->target ->writeMutator ->getExpression ($ target , $ mappedValueVar , $ assignByRef )),
97102 ],
98103 ]);
99-
100- // @TODO handle existingValue
101104 } else {
102105 $ loopExistingValueVar = new Expr \Variable ($ uniqueVariableScope ->getUniqueName ('existingValue ' ));
103106
104107 if ($ propertyMapping ->target ->readAccessor !== null && $ this ->itemTransformer instanceof IdentifierHashInterface) {
105108 $ hashValueVariable = new Expr \Variable ($ uniqueVariableScope ->getUniqueName ('hashValue ' ));
106- $ statements [] = new Stmt \If_ (new Expr \BinaryOp \Coalesce (
109+
110+ $ isDeepPopulateExpr = new Expr \BinaryOp \Coalesce (
107111 new Expr \ArrayDimFetch (new Expr \Variable ('context ' ), new Scalar \String_ (MapperContext::DEEP_TARGET_TO_POPULATE )),
108112 new Expr \ConstFetch (new Name ('false ' ))
109- ), [
113+ );
114+
115+ $ isDefinedExpr = $ propertyMapping ->target ->readAccessor ->getIsDefinedExpression (new Expr \Variable ('result ' ));
116+
117+ if ($ isDefinedExpr !== null ) {
118+ $ isDeepPopulateExpr = new Expr \BinaryOp \BooleanAnd ($ isDeepPopulateExpr , $ isDefinedExpr );
119+ }
120+
121+ $ statements [] = new Stmt \If_ ($ isDeepPopulateExpr , [
110122 'stmts ' => [
111123 new Stmt \Foreach_ ($ propertyMapping ->target ->readAccessor ->getExpression ($ target ), $ loopExistingValueVar , [
112124 'stmts ' => [
0 commit comments