8
8
use AutoMapper \Generator \UniqueVariableScope ;
9
9
use AutoMapper \MapperContext ;
10
10
use AutoMapper \Metadata \PropertyMetadata ;
11
+ use PhpParser \Comment \Doc ;
11
12
use PhpParser \Node \Expr ;
12
13
use PhpParser \Node \Name ;
13
14
use PhpParser \Node \Scalar ;
@@ -69,10 +70,15 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
69
70
);
70
71
71
72
if ($ propertyMapping ->target ->readAccessor !== null && $ this ->itemTransformer instanceof IdentifierHashInterface) {
73
+ $ targetHashVar = new Expr \Variable ($ uniqueVariableScope ->getUniqueName ('targetHash ' ));
74
+
72
75
$ loopExistingStatements [] = new Stmt \If_ ($ isDeepPopulateExpr , [
73
76
'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
+ ],
76
82
]);
77
83
}
78
84
@@ -96,17 +102,24 @@ public function transform(Expr $input, Expr $target, PropertyMetadata $propertyM
96
102
new Stmt \Expression ($ propertyMapping ->target ->writeMutator ->getExpression ($ target , $ mappedValueVar , $ assignByRef )),
97
103
],
98
104
]);
99
-
100
- // @TODO handle existingValue
101
105
} else {
102
106
$ loopExistingValueVar = new Expr \Variable ($ uniqueVariableScope ->getUniqueName ('existingValue ' ));
103
107
104
108
if ($ propertyMapping ->target ->readAccessor !== null && $ this ->itemTransformer instanceof IdentifierHashInterface) {
105
109
$ hashValueVariable = new Expr \Variable ($ uniqueVariableScope ->getUniqueName ('hashValue ' ));
106
- $ statements [] = new Stmt \If_ (new Expr \BinaryOp \Coalesce (
110
+
111
+ $ isDeepPopulateExpr = new Expr \BinaryOp \Coalesce (
107
112
new Expr \ArrayDimFetch (new Expr \Variable ('context ' ), new Scalar \String_ (MapperContext::DEEP_TARGET_TO_POPULATE )),
108
113
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 , [
110
123
'stmts ' => [
111
124
new Stmt \Foreach_ ($ propertyMapping ->target ->readAccessor ->getExpression ($ target ), $ loopExistingValueVar , [
112
125
'stmts ' => [
0 commit comments