Skip to content

Commit 25bd7bd

Browse files
committed
The test works for decoding as well as encoding
Next thing on the to-do list is to tidy up the library and make every unit test pass again
1 parent 2251508 commit 25bd7bd

File tree

4 files changed

+105
-87
lines changed

4 files changed

+105
-87
lines changed

src/PE/Encoder.php

Lines changed: 61 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/PE/Variables/Types/NodeAccessor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ abstract class NodeAccessor extends VariableType {
2222
const VARIABLE_VALUE = 'node_value';
2323
const VARIABLE_OBJECT = 'node_object';
2424
const VARIABLE_PARENT = 'node_parent';
25+
const VARIABLE_OPTIONS = 'node_options';
26+
const VARIABLE_NODE_ITERATION_INDEX = 'node_iteration_index';
27+
const VARIABLE_CHILD_OBJECT_ITERATION_INDEX = 'node_child_object_iteration_index';
2528

2629
const ACCESSOR_SETTER = 'setter';
2730
const ACCESSOR_GETTER = 'getter';

tests/PE/Nodes/Specials/VariableTypesNode.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use PE\Nodes\EncoderNodeVariable;
77
use PE\Samples\Specials\VariableTypes;
88
use PE\Variables\Types\NodeAccessor;
9+
use PE\Variables\Types\PostNodeGetter;
910
use PE\Variables\Types\PostNodeSetter;
1011
use PE\Variables\Types\PreNodeSetter;
1112

@@ -29,15 +30,29 @@ function __construct() {
2930
NodeAccessor::VARIABLE_OBJECT,
3031
NodeAccessor::VARIABLE_PARENT
3132
)));
33+
$required->postNodeGetter(new PostNodeGetter('postNodeRequiredGetter', array(
34+
NodeAccessor::VARIABLE_NODE,
35+
NodeAccessor::VARIABLE_NAME,
36+
NodeAccessor::VARIABLE_VALUE,
37+
NodeAccessor::VARIABLE_OBJECT,
38+
NodeAccessor::VARIABLE_PARENT
39+
)));
3240

33-
$required = $this->addVariable(new EncoderNodeVariable('optional'));
34-
$required->preNodeSetter(new PreNodeSetter('preNodeOptionalSetter', array(
41+
$optional = $this->addVariable(new EncoderNodeVariable('optional'));
42+
$optional->preNodeSetter(new PreNodeSetter('preNodeOptionalSetter', array(
3543
NodeAccessor::VARIABLE_NODE,
3644
NodeAccessor::VARIABLE_NAME,
3745
NodeAccessor::VARIABLE_VALUE,
3846
NodeAccessor::VARIABLE_PARENT
3947
)));
40-
$required->postNodeSetter(new PostNodeSetter('postNodeOptionsSetter', array(
48+
$optional->postNodeSetter(new PostNodeSetter('postNodeOptionalSetter', array(
49+
NodeAccessor::VARIABLE_NODE,
50+
NodeAccessor::VARIABLE_NAME,
51+
NodeAccessor::VARIABLE_VALUE,
52+
NodeAccessor::VARIABLE_OBJECT,
53+
NodeAccessor::VARIABLE_PARENT
54+
)));
55+
$optional->postNodeGetter(new PostNodeGetter('postNodeOptionalGetter', array(
4156
NodeAccessor::VARIABLE_NODE,
4257
NodeAccessor::VARIABLE_NAME,
4358
NodeAccessor::VARIABLE_VALUE,
@@ -47,22 +62,31 @@ function __construct() {
4762
}
4863

4964
public function preNodeRequiredSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, $parent) {
50-
print_r('pre required');
51-
print_r($nodeData);
65+
$nodeData['required'] = $nodeData['required'] . ' | setter pre';
5266
return $nodeData;
5367
}
5468
public function postNodeRequiredSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
55-
print_r('post required');
69+
$nodeData['required'] = $nodeData['required'] . ' | setter post';
70+
return $nodeData;
71+
}
72+
public function postNodeRequiredGetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
73+
print_r('post getter required');
74+
$nodeData['required'] = $nodeData['required'] . ' | getter post';
5675
return $nodeData;
5776
}
5877

78+
5979
public function preNodeOptionalSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, $parent) {
60-
print_r('pre optional');
61-
print_r($nodeData);
80+
$nodeData['optional'] = $nodeData['optional'] . ' | setter pre';
81+
return $nodeData;
82+
}
83+
public function postNodeOptionalSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
84+
$nodeData['optional'] = $nodeData['optional'] . ' | setter post';
6285
return $nodeData;
6386
}
64-
public function postNodeOptionsSetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
65-
print_r('post optional');
87+
public function postNodeOptionalGetter($nodeData, VariableTypesNode $variableTypesNode, $name, $value, VariableTypes $variableTypes, $parent) {
88+
print_r('post getter optional');
89+
$nodeData['optional'] = $nodeData['optional'] . ' | getter post';
6690
return $nodeData;
6791
}
6892
}

tests/PE/Tests/EncoderTest.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,15 @@ public function testDecodeObjectAllVariableTypes() {
248248
)
249249
));
250250

251-
print_r($decoded);
252251
/** @var VariableTypes $obj */
253252
$obj = $decoded['variable-type'];
254-
$this->assertEquals('Hello world', $obj->getRequired());
255-
$this->assertEquals('Hello other world', $obj->getOptional());
253+
$this->assertEquals('Hello world | setter pre', $obj->getRequired());
254+
$this->assertEquals('Hello other world | setter pre | setter post', $obj->getOptional());
255+
256+
$encoded = $this->encoder()->encode($obj);
257+
$encodedProcessed = $encoded['processed'];
258+
$this->assertEquals('Hello world | setter pre | getter post', $encodedProcessed['variable-type']['required']);
259+
$this->assertEquals('Hello other world | setter pre | setter post | getter post', $encodedProcessed['variable-type']['optional']);
256260
}
257261

258262
public function testDecodeWithSetAfterChildrenFalse() {

0 commit comments

Comments
 (0)