Skip to content

Commit ea1318e

Browse files
committed
A lot of changes to the unit tests to make the new variables work
1 parent 28277de commit ea1318e

23 files changed

+353
-483
lines changed

src/PE/Encoder.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ protected function decodeRawToArray($nodeName, $node, $array) {
6161
foreach ($decodedData as $decodedName => $data) {
6262
// if data belongs to a child
6363
if ($nodeType->childNodeExists($decodedName)) {
64+
$childNodeSetter = $nodeType->getChild($decodedName)->getSetter();
6465
// decode it, unset it and add it back into the array at the correct place
6566
$childArrDecoded = $this->decodeRawToArray($decodedName, $nodeChildData, $array);
6667
unset($decodedData[$decodedName]);
67-
$decodedData = $nodeType->getChild($decodedName)->setAfterChildren() === false ? array($decodedName => $childArrDecoded) + $decodedData : array_merge($decodedData, array($decodedName => $childArrDecoded));
68+
$decodedData = $childNodeSetter->setAfterChildren() === false ? array($decodedName => $childArrDecoded) + $decodedData : array_merge($decodedData, array($decodedName => $childArrDecoded));
6869
}
6970
// if data belongs to an attribute simply
7071
else if (is_array($data)) {
@@ -118,8 +119,10 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
118119
$addAfterAttributes = true;
119120
if ($parentNode) {
120121
$childNode = $parentNode->getChild($nodeName);
121-
$addAfterDecode = $childNode->setAfterChildren();
122-
$addAfterAttributes = $childNode->setAfterAttributes();
122+
123+
$childNodeSetter = $childNode->getSetter();
124+
$addAfterDecode = $childNodeSetter->setAfterChildren();
125+
$addAfterAttributes = $childNodeSetter->setAfterAttributes();
123126
}
124127

125128
$objects = array();
@@ -192,7 +195,12 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
192195
throw new EncoderException(sprintf('Variable "%s" for "%s" does not exist but is required to create an object for node "%s" (Node type: "%s") at index "%s"', $processedVariable, $nodeClassName, $nodeName, $type->getNodeName(), $nodeIndex));
193196
}
194197
$requiredValue = $nodeDataItem[$processedVariable];
195-
$objectSetter = $variableCollection->getVariableById($processedVariable)->getObjectSetter();
198+
$objectSetterVariable = $variableCollection->getVariableById($processedVariable);
199+
if (!$objectSetterVariable) {
200+
throw new EncoderException(sprintf('Variable "%s" for "%s" is required but there is no EncoderNodeVariable available to retrieve the value for node "%s" (Node type: "%s") at index "%s".', $processedVariable, $nodeClassName, $nodeName, $type->getNodeName(), $nodeIndex));
201+
}
202+
203+
$objectSetter = $objectSetterVariable->getObjectSetter();
196204
$processedRequiredValue = $objectSetter->processValue($requiredValue);
197205
if ($processedRequiredValue === null) {
198206
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));
@@ -258,7 +266,7 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
258266
if ($type->childNodeExists($childName)) {
259267
$children = $this->_decodeNode($childName, $nodeDataItem, $options, $type, $nodeInstance, $nodeDataItem);
260268

261-
if ($type->getChild($childName)->setAfterChildren()) {
269+
if ($type->getChild($childName)->getSetter()->setAfterChildren()) {
262270
$isSingleChildNode = $type->isSingleNode($childName);
263271
$type->addChildrenToObject($childName, $nodeInstance, $isSingleChildNode ? array($children) : $children);
264272
}

src/PE/Nodes/Children/NodeChildSetter.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,27 @@
44

55
class NodeChildSetter extends NodeChildAccessor {
66

7+
private $setAfterChildren = true;
8+
private $setAfterAttributes = true;
9+
710
function __construct($method) {
811
parent::__construct($method);
912
}
1013

14+
public function setAfterChildren($bool = null) {
15+
if ($bool !== null) {
16+
$this->setAfterChildren = $bool;
17+
}
18+
return $this->setAfterChildren;
19+
}
20+
21+
public function setAfterAttributes($bool = null) {
22+
if ($bool !== null) {
23+
$this->setAfterAttributes = $bool;
24+
}
25+
return $this->setAfterAttributes;
26+
}
27+
1128
}
1229

1330
?>

src/PE/Nodes/EncoderNodeChild.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ class EncoderNodeChild extends EncoderNodeVariable {
2424
private $getter;
2525

2626
private $isArray = true;
27-
private $setAfterChildren = true;
28-
private $setAfterAttributes = true;
2927

30-
function __construct($nodeName, $options = null) {
28+
function __construct($nodeName, NodeChildSetter $setter, NodeChildGetter $getter, $options = null) {
3129
parent::__construct('', $options);
3230
$this->setChildNodeName($nodeName);
31+
32+
$this->setter($setter);
33+
$this->getter($getter);
3334
}
3435

3536
public function setChildNodeName($childNodeName) {
@@ -45,7 +46,7 @@ public function getChildNodeName() {
4546
/**
4647
* @param NodeChildSetter $setter
4748
*/
48-
public function setter(NodeChildSetter $setter) {
49+
protected function setter(NodeChildSetter $setter) {
4950
$this->setter = $setter;
5051
}
5152
/**
@@ -58,7 +59,7 @@ public function getSetter() {
5859
/**
5960
* @param NodeChildGetter $getter
6061
*/
61-
public function getter(NodeChildGetter $getter) {
62+
protected function getter(NodeChildGetter $getter) {
6263
$this->getter = $getter;
6364
}
6465
/**
@@ -76,20 +77,6 @@ public function isArray($bool = null) {
7677
return $this->isArray;
7778
}
7879

79-
public function setAfterChildren($bool = null) {
80-
if ($bool !== null) {
81-
$this->setAfterChildren = $bool;
82-
}
83-
return $this->setAfterChildren;
84-
}
85-
86-
public function setAfterAttributes($bool = null) {
87-
if ($bool !== null) {
88-
$this->setAfterAttributes = $bool;
89-
}
90-
return $this->setAfterAttributes;
91-
}
92-
9380
/**
9481
* Adds values to an object
9582
*

src/PE/Nodes/EncoderNodeVariable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function __construct($id, $enableObjectAccessors = true) {
8282
* @param string $id
8383
* @return null|string
8484
*/
85-
public function setId($id) {
85+
protected function setId($id) {
8686
$this->id = $id;
8787
return $id;
8888
}
@@ -112,6 +112,8 @@ public function getType() {
112112

113113
/**
114114
* Set the order the variable (when it is a collection)
115+
*
116+
* A lower number means it will end up near the beginning of the array
115117
* @param int $index
116118
*/
117119
public function setOrder($index) {

src/PE/Nodes/EncoderNodeVariableCollection.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class EncoderNodeVariableCollection {
1717
private $variables;
1818

1919
function __construct() {
20+
$this->_cache = array();
2021
$this->variables = array();
2122
}
2223

@@ -50,6 +51,13 @@ public function getVariable($variable) {
5051
return null;
5152
}
5253

54+
/**
55+
* @return EncoderNodeVariable[]
56+
*/
57+
public function getVariables() {
58+
return $this->variables;
59+
}
60+
5361
/**
5462
* Get a variable based on its id
5563
* @param $id
@@ -172,6 +180,9 @@ protected function _getVariables($methodNameHas, $ordered = true) {
172180
array_push($unorderedVariables, $variable);
173181
}
174182
}
183+
// sort the ordered variables array
184+
ksort($orderedVariables);
185+
// merge all arrays and return the merged variables
175186
return $ordered ? array_merge($orderedVariables, $unorderedVariables) : $unorderedVariables;
176187
}
177188

@@ -191,7 +202,7 @@ protected function _cache($method, $parameters = null, $value = false) {
191202
$this->_cache[$method][$parameters] = $value;
192203
return $value;
193204
}
194-
if (!array_key_exists($method, $this->_cache) || !array_key_exists($parameters, $this->_cache[$method])) {
205+
if (!(array_key_exists($method, $this->_cache) && array_key_exists($parameters, $this->_cache[$method]))) {
195206
return false;
196207
}
197208
return $this->_cache[$method][$parameters];

src/PE/Variables/Types/ObjectAccessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ protected function camelCased($str) {
6161
protected function _apply($object, $parameters) {
6262
$methodName = $this->getMethod();
6363
if (!method_exists($object, $methodName)) {
64-
throw new VariableTypeException(sprintf('Method "%s" does not exist for class %s does not exist', $methodName, get_class($object)));
64+
throw new VariableTypeException(sprintf('Method "%s" does not exist for class "%s" does not exist', $methodName, get_class($object)));
6565
}
6666
else {
6767
return call_user_func_array(array($object, $methodName), $parameters);

tests/PE/Nodes/Erroneous/NoGetterMethodNode.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PE\Nodes\Erroneous;
44

5+
use PE\Nodes\Children\NodeChildGetter;
6+
use PE\Nodes\Children\NodeChildSetter;
57
use PE\Nodes\EncoderNode;
68
use PE\Nodes\EncoderNodeChild;
79

@@ -10,10 +12,7 @@ class NoGetterMethodNode extends EncoderNode {
1012
function __construct() {
1113
parent::__construct('no-getter-methods', 'no-getter-method', '\\PE\\Samples\\Erroneous');
1214

13-
$this->addChildNode(new EncoderNodeChild('things', array(
14-
'setter' => 'addThing',
15-
'getter' => 'getThings'
16-
)));
15+
$this->addChildNode(new EncoderNodeChild('things', new NodeChildSetter('addThing'), new NodeChildGetter('getThings')));
1716
}
1817

1918
}

tests/PE/Nodes/Erroneous/NonArrayGetterMethodNode.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PE\Nodes\Erroneous;
44

5+
use PE\Nodes\Children\NodeChildGetter;
6+
use PE\Nodes\Children\NodeChildSetter;
57
use PE\Nodes\EncoderNode;
68
use PE\Nodes\EncoderNodeChild;
79

@@ -10,9 +12,6 @@ class NonArrayGetterMethodNode extends EncoderNode {
1012
function __construct() {
1113
parent::__construct('non-array-getter-methods', 'non-array-getter-method', '\\PE\\Samples\\Erroneous');
1214

13-
$this->addChildNode(new EncoderNodeChild('things', array(
14-
'setter' => 'addThing',
15-
'getter' => 'getThings'
16-
)));
15+
$this->addChildNode(new EncoderNodeChild('things', new NodeChildSetter('addThing'), new NodeChildGetter('getThings')));
1716
}
1817
}

tests/PE/Nodes/Farm/Buildings/AnimalsBuildingNode.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ class AnimalsBuildingNode extends CustomBuilding {
1111
function __construct($nodeTypeName) {
1212
parent::__construct($nodeTypeName);
1313

14-
$animals = $this->addChildNode(new EncoderNodeChild('animals'));
15-
$animals->setter(new NodeChildSetter('addAnimal'));
16-
$animals->getter(new NodeChildGetter('getAnimals'));
14+
$this->addChildNode(new EncoderNodeChild('animals', new NodeChildSetter('addAnimal'), new NodeChildGetter('getAnimals')));
1715
}
1816

1917
}

tests/PE/Nodes/Farm/FarmNode.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ class FarmNode extends EncoderNode {
1212
function __construct() {
1313
parent::__construct('farms', 'farm', '\\PE\\Samples\\Farm');
1414

15-
$buildings = $this->addChildNode(new EncoderNodeChild('buildings'));
16-
$buildings->setter(new NodeChildSetter('addBuilding'));
17-
$buildings->getter(new NodeChildGetter('getBuildings'));
15+
$this->addChildNode(new EncoderNodeChild('buildings', new NodeChildSetter('addBuilding'), new NodeChildGetter('getBuildings')));
1816
}
1917

2018
}

0 commit comments

Comments
 (0)