Skip to content

Commit 28277de

Browse files
committed
The node children partly work
1 parent 2564aa5 commit 28277de

File tree

6 files changed

+72
-11
lines changed

6 files changed

+72
-11
lines changed

src/PE/Encoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ protected function _encode($object, EncoderNode $node, EncoderOptions $options,
412412
$isIterated = $optionChildIteration !== null;
413413
$childIteration = $optionChildIteration === null ? 1 : $optionChildIteration;
414414

415-
$getChildObjectsMethod = $child->getGetterMethod();
415+
$getChildObjectsMethod = $child->getGetter()->getMethod();
416416
if (!method_exists($object, $getChildObjectsMethod)) {
417417
throw new EncoderException(sprintf('Getter method "%s" for node "%s" does not exist in class "%s"', $getChildObjectsMethod, $childNodeName, get_class($object)));
418418
}

src/PE/Nodes/Children/NodeChildAccessor.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ function __construct($method) {
1010
$this->method = $method;
1111
}
1212

13+
public function getMethod() {
14+
return $this->method;
15+
}
16+
1317
}
1418

1519
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace PE\Nodes\Children;
4+
5+
class NodeChildGetter extends NodeChildAccessor {
6+
7+
function __construct($method) {
8+
parent::__construct($method);
9+
}
10+
11+
}
12+
13+
?>

src/PE/Nodes/EncoderNodeChild.php

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33
namespace PE\Nodes;
44

55
use PE\Exceptions\EncoderNodeChildException;
6+
use PE\Nodes\Children\NodeChildGetter;
7+
use PE\Nodes\Children\NodeChildSetter;
68

79
class EncoderNodeChild extends EncoderNodeVariable {
810

11+
/**
12+
* @var string
13+
*/
914
private $childNodeName;
15+
16+
/**
17+
* @var NodeChildSetter Setter of the child objects
18+
*/
19+
private $setter;
20+
21+
/**
22+
* @var NodeChildGetter Setter of the child objects
23+
*/
24+
private $getter;
25+
1026
private $isArray = true;
1127
private $setAfterChildren = true;
1228
private $setAfterAttributes = true;
@@ -26,6 +42,32 @@ public function getChildNodeName() {
2642
return $this->childNodeName;
2743
}
2844

45+
/**
46+
* @param NodeChildSetter $setter
47+
*/
48+
public function setter(NodeChildSetter $setter) {
49+
$this->setter = $setter;
50+
}
51+
/**
52+
* @return NodeChildSetter
53+
*/
54+
public function getSetter() {
55+
return $this->setter;
56+
}
57+
58+
/**
59+
* @param NodeChildGetter $getter
60+
*/
61+
public function getter(NodeChildGetter $getter) {
62+
$this->getter = $getter;
63+
}
64+
/**
65+
* @return NodeChildGetter
66+
*/
67+
public function getGetter() {
68+
return $this->getter;
69+
}
70+
2971
// @todo Figure out if this feature is still necessary. Because if you use a single node, can't we simply assume it isn't an array?
3072
public function isArray($bool = null) {
3173
if ($bool !== null) {
@@ -56,13 +98,13 @@ public function setAfterAttributes($bool = null) {
5698
* @return bool Returns true if the action succeeded and false when it couldn't find the child
5799
*/
58100
public function addChildrenToObject($target, $values) {
59-
$methodName = $this->getSetterMethod();
101+
$methodName = $this->getSetter()->getMethod();
60102
if ($methodName === null) {
61103
throw new EncoderNodeChildException(sprintf('Setter method (%s) for class "%s" does not exist', $this->getChildNodeName(), get_class($target)));
62104
}
63105
else if (method_exists($target, $methodName)) {
64106
foreach ($values as $value) {
65-
$target->$methodName($this->processValue($value));
107+
$target->$methodName($value);
66108
}
67109
}
68110
else {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22

33
namespace PE\Nodes\Farm\Buildings;
44

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

79
class AnimalsBuildingNode extends CustomBuilding {
810

911
function __construct($nodeTypeName) {
1012
parent::__construct($nodeTypeName);
1113

12-
$this->addChildNode(new EncoderNodeChild('animals', array(
13-
'setter' => 'addAnimal',
14-
'getter' => 'getAnimals'
15-
)));
14+
$animals = $this->addChildNode(new EncoderNodeChild('animals'));
15+
$animals->setter(new NodeChildSetter('addAnimal'));
16+
$animals->getter(new NodeChildGetter('getAnimals'));
1617
}
1718

1819
}

tests/PE/Nodes/Farm/FarmNode.php

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

33
namespace PE\Nodes\Farm;
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,9 @@ class FarmNode extends EncoderNode {
1012
function __construct() {
1113
parent::__construct('farms', 'farm', '\\PE\\Samples\\Farm');
1214

13-
$this->addChildNode(new EncoderNodeChild('buildings', array(
14-
'setter' => 'addBuilding',
15-
'getter' => 'getBuildings'
16-
)));
15+
$buildings = $this->addChildNode(new EncoderNodeChild('buildings'));
16+
$buildings->setter(new NodeChildSetter('addBuilding'));
17+
$buildings->getter(new NodeChildGetter('getBuildings'));
1718
}
1819

1920
}

0 commit comments

Comments
 (0)