@@ -148,15 +148,16 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
148148 );
149149 // call node methods. It can be useful when you want to change the outcome of the node data in the node
150150 // that does not have a certain setter but is used in other ways
151- $ nodeMethodVariables = $ variableCollection ->getPreNodeSetterVariables ();
152- foreach ($ nodeMethodVariables as $ objectSetterVariable ) {
153- $ variableId = $ objectSetterVariable ->getId ();
154- $ preNodeSetter = $ objectSetterVariable ->getPreNodeSetter ();
151+ $ preNodeSetterVariables = $ variableCollection ->getPreNodeSetterVariables ();
152+ foreach ($ preNodeSetterVariables as $ preNodeSetterVariable ) {
153+ $ variableId = $ preNodeSetterVariable ->getId ();
154+ $ variableIsset = isset ($ nodeDataItem [$ variableId ]);
155+ $ preNodeSetter = $ preNodeSetterVariable ->getPreNodeSetter ();
155156 if (isset ($ nodeDataItem [$ variableId ]) || $ preNodeSetter ->alwaysExecute ()) {
156157 $ setterOptions = array_merge ($ preNodeStaticOptions , array (
157158 NodeAccessor::VARIABLE_NODE_DATA => $ nodeDataItem ,
158159 NodeAccessor::VARIABLE_NAME => $ variableId ,
159- NodeAccessor::VARIABLE_VALUE => $ nodeDataItem [$ variableId ]
160+ NodeAccessor::VARIABLE_VALUE => $ variableIsset ? $ nodeDataItem [$ variableId ] : null
160161 ));
161162 if ($ newNode = $ preNodeSetter ->apply ($ setterOptions )) {
162163 $ nodeDataItem = $ newNode ;
@@ -197,7 +198,7 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
197198 throw new EncoderException (sprintf ('Variable "%s" for "%s" cannot process its value (%s). Presumably because the NodeType does not recognize the variable ' , $ processedVariable , $ nodeClassName , $ requiredValue ));
198199 }
199200
200- array_push ( $ requiredVariableValues, $ processedRequiredValue) ;
201+ $ requiredVariableValues[ $ processedVariable ] = $ processedRequiredValue ;
201202 unset($ nodeDataItem [$ processedVariable ]);
202203 }
203204
@@ -212,44 +213,42 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
212213 $ parentNode ->addChildrenToObject ($ nodeName , $ parentObject , array ($ nodeInstance ));
213214 }
214215
215- $ objectStaticOptions = array_merge ( $ preNodeStaticOptions , array (
216- ObjectAccessor:: VARIABLE_NODE_DATA => $ nodeDataItem ,
216+ // run the post setter variable types
217+ $ postNodeStaticOptions = array_merge ( $ preNodeStaticOptions , array (
217218 ObjectAccessor::VARIABLE_OBJECT => $ nodeInstance ,
218219 ));
220+ $ postNodeSetterVariables = $ variableCollection ->getPostNodeSetterVariables ();
221+ foreach ($ postNodeSetterVariables as $ postNodeSetterVariable ) {
222+ $ variableId = $ postNodeSetterVariable ->getId ();
223+ $ variableIsset = isset ($ nodeDataItem [$ variableId ]);
224+ $ postNodeSetter = $ postNodeSetterVariable ->getPostNodeSetter ();
225+ if ($ variableIsset || $ postNodeSetter ->alwaysExecute ()) {
226+ $ setterOptions = array_merge ($ postNodeStaticOptions , array (
227+ NodeAccessor::VARIABLE_NODE_DATA => $ nodeDataItem ,
228+ NodeAccessor::VARIABLE_NAME => $ variableId ,
229+ NodeAccessor::VARIABLE_VALUE => $ variableIsset ? $ nodeDataItem [$ variableId ] : null
230+ ));
231+ if ($ newNode = $ postNodeSetter ->apply ($ setterOptions )) {
232+ $ nodeDataItem = $ newNode ;
233+ }
234+ }
235+ }
219236
237+ // run the optional object setter variable types
220238 $ objectSetterVariables = $ variableCollection ->getObjectSetterVariables ();
221239 foreach ($ objectSetterVariables as $ objectSetterVariable ) {
222240 $ variableId = $ objectSetterVariable ->getId ();
241+ if (array_key_exists ($ variableId , $ requiredVariableValues )) {
242+ // if the variable was an required variable do not try to set it again
243+ continue ;
244+ }
223245 $ variableIsset = isset ($ nodeDataItem [$ variableId ]);
224246 $ objectSetter = $ objectSetterVariable ->getObjectSetter ();
225247 if ($ variableIsset || $ objectSetter ->alwaysExecute ()) {
226248 $ objectSetter ->apply ($ nodeInstance , $ variableIsset ? $ nodeDataItem [$ variableId ] : null );
227249 }
228250 }
229251
230-
231- /*$setterOptionsStatic = array(
232- ActionVariable::SETTER_NODE_DATA => $nodeDataItem,
233- ActionVariable::SETTER_OBJECT => $nodeInstance,
234- ActionVariable::SETTER_PARENT => $parentObject
235- );
236-
237- $nodeMethodVariables = $type->getVariables();
238- foreach ($nodeMethodVariables as $nodeMethodVariable) {
239- $variableId = $nodeMethodVariable->getId();
240- $variableIsset = isset($nodeDataItem[$variableId]);
241- if (($variableIsset || $nodeMethodVariable->alwaysExecute()) && $nodeMethodVariable->setterIsActive()) {
242- $setterOptions = array_merge($setterOptionsStatic, array(
243- ActionVariable::SETTER_NAME => $variableId,
244- ActionVariable::SETTER_VALUE => $variableIsset ? $nodeDataItem[$variableId] : null
245- ));
246- $type->applyToVariable($variableId, $setterOptions);
247- }
248- }*/
249-
250-
251-
252-
253252 if (!$ addAfterDecode && $ addAfterAttributes ) {
254253 $ parentNode ->addChildrenToObject ($ nodeName , $ parentObject , array ($ nodeInstance ));
255254 }
@@ -332,38 +331,47 @@ public function encode($object, EncoderOptions $options = null) {
332331 }
333332
334333 protected function _encode ($ object , EncoderNode $ node , EncoderOptions $ options , $ parent = null , $ nodeIterationIndex = null , $ childObjectIterationIndex = null ) {
335- $ variables = $ node ->getVariables ();
334+ $ variableCollection = $ node ->getVariableCollection ();
335+ $ objectGetterVariables = $ variableCollection ->getObjectGetterVariables ();
336336
337337 $ optionNodeIndex = $ node ->getNodeName () . '[ ' . $ childObjectIterationIndex . '] ' ;
338338
339+ // get all the variables values from the object
339340 $ attributesRaw = array ();
340- foreach ($ variables as $ variable ) {
341- if (! $ variable -> hasGetterAction () || $ variable -> alwaysExecute ()) {
341+ foreach ($ objectGetterVariables as $ objectGetterVariable ) {
342+ $ variableId = $ objectGetterVariable -> getId ();
342343
343- $ variableId = $ variable ->getId ();
344+ $ objectGetter = $ objectGetterVariable ->getObjectGetter ();
345+ $ attributesRaw [$ variableId ] = $ objectGetter ->apply ($ object );
346+ }
344347
345- $ attributeValue = null ;
346- $ getAttributeMethod = $ variable ->getGetterMethod ();
347- if (method_exists ($ object , $ getAttributeMethod )) {
348- $ attributeValue = $ object ->$ getAttributeMethod ();
349- } else {
350- throw new EncoderException (sprintf ('Getter method "%s" does not exist in object "%s" for node type "%s" (%s) and variable with id "%s". ' , $ getAttributeMethod , get_class ($ object ), $ node ->getNodeTypeName (), get_class ($ node ), $ variableId ));
348+ $ postNodeStaticOptions = array (
349+ NodeAccessor::VARIABLE_NODE => $ node ,
350+ NodeAccessor::VARIABLE_OBJECT => $ object ,
351+ NodeAccessor::VARIABLE_PARENT => $ parent ,
352+ NodeAccessor::VARIABLE_OPTIONS => $ options ,
353+ NodeAccessor::VARIABLE_NODE_ITERATION_INDEX => $ nodeIterationIndex ,
354+ NodeAccessor::VARIABLE_CHILD_OBJECT_ITERATION_INDEX => $ childObjectIterationIndex ,
355+ );
356+ $ postNodeGetterVariables = $ variableCollection ->getPostNodeGetterVariables ();
357+ foreach ($ postNodeGetterVariables as $ postNodeGetterVariable ) {
358+ $ variableId = $ postNodeGetterVariable ->getId ();
359+ $ hasVariable = array_key_exists ($ variableId , $ attributesRaw );
360+ $ postNodeGetter = $ postNodeGetterVariable ->getPostNodeGetter ();
361+ if ($ hasVariable || $ postNodeGetter ->alwaysExecute ()) {
362+ $ actionOptions = array_merge ($ postNodeStaticOptions , array (
363+ NodeAccessor::VARIABLE_NODE_DATA => $ attributesRaw ,
364+ NodeAccessor::VARIABLE_NAME => $ variableId ,
365+ NodeAccessor::VARIABLE_VALUE => $ hasVariable ? $ attributesRaw [$ variableId ] : null ,
366+ ));
367+ if ($ newAttributeData = $ postNodeGetter ->apply ($ actionOptions )) {
368+ if (is_array ($ newAttributeData )) {
369+ $ attributesRaw = $ newAttributeData ;
370+ }
351371 }
352-
353- $ attributesRaw [$ variableId ] = $ attributeValue ;
354372 }
355373 }
356374
357- $ actionVariables = array (
358- ActionVariable::GETTER_OBJECT => $ object ,
359- ActionVariable::GETTER_PARENT => $ parent ,
360- ActionVariable::GETTER_OPTIONS => $ options ,
361- ActionVariable::GETTER_NODE_ITERATION_INDEX => $ nodeIterationIndex ,
362- ActionVariable::GETTER_CHILD_OBJECT_ITERATION_INDEX => $ childObjectIterationIndex ,
363- );
364-
365- $ nodeMethodVariables = $ node ->getVariablesGetterActionByType (EncoderNodeVariable::ACTION_TYPE_NODE );
366- $ attributesRaw = $ this ->loopNodeVariables ($ node , $ nodeMethodVariables , $ attributesRaw , $ actionVariables );
367375
368376 $ optionNodeKey = $ options ->option ('key ' , $ node );
369377 $ optionNodeValue = $ options ->option ('value ' , $ node );
@@ -510,25 +518,4 @@ protected function encodeNodeChildren(EncoderNode $node, $nodeName, EncoderNodeC
510518 protected function encodeAttributes ($ attributes ) {
511519 return $ attributes ;
512520 }
513-
514-
515- protected function loopNodeVariables ($ node , $ variables , $ data , $ actionOptions ) {
516- $ temp = $ data ;
517- foreach ($ variables as $ variableId => $ nodeMethodVariable ) {
518- $ hasVariable = array_key_exists ($ variableId , $ data );
519- if ($ hasVariable || $ nodeMethodVariable ->alwaysExecute ()) {
520- $ actionOptions = array_merge (array (
521- ActionVariable::GETTER_NODE_DATA => $ data ,
522- ActionVariable::GETTER_NAME => $ variableId ,
523- ActionVariable::GETTER_VALUE => $ hasVariable ? $ data [$ variableId ] : null ,
524- ), $ actionOptions );
525- if ($ newAttributeData = $ nodeMethodVariable ->callNodeGetterAction ($ node , $ actionOptions )) {
526- if (is_array ($ newAttributeData )) {
527- $ temp = $ newAttributeData ;
528- }
529- }
530- }
531- }
532- return $ temp ;
533- }
534521}
0 commit comments