Skip to content

Commit 10ed3e6

Browse files
committed
Enhanced api for registering node types
While registering a node type, you no longer have to supply a node type name to the function. Instead you'll have to supply from within the node type object itself. This will cause less problems when working with the library
1 parent b88080a commit 10ed3e6

File tree

16 files changed

+63
-40
lines changed

16 files changed

+63
-40
lines changed

src/PE/Encoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ protected function _encode($object, EncoderNode $node, EncoderOptions $options,
308308
if (method_exists($object, $getAttributeMethod)) {
309309
$attributeValue = $object->$getAttributeMethod();
310310
} else {
311-
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->getTypeName(), get_class($node), $variableId));
311+
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));
312312
}
313313

314314
$attributesRaw[$variableId] = $attributeValue;

src/PE/nodes/EncoderNode.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class EncoderNode {
4141
private $nodeName;
4242
private $nodeNameSingle;
4343
private $isolatedNodeName;
44-
public $typeName;
44+
private $typeName;
4545

4646
private $needsClass;
4747
private $classPrepend;
@@ -50,7 +50,7 @@ class EncoderNode {
5050

5151
const NODE_EXTENSION = 'Node';
5252

53-
function __construct($nodeName, $nodeNameSingle, $classPrepend) {
53+
function __construct($nodeName, $nodeNameSingle, $classPrepend, $nodeTypeName = null) {
5454

5555
$this->nodeClasses = array();
5656
$this->_nodeIsObjectCache = array();
@@ -63,6 +63,7 @@ function __construct($nodeName, $nodeNameSingle, $classPrepend) {
6363

6464
$this->needsObject(true);
6565
$this->classPrepend = $classPrepend;
66+
$this->typeName = $nodeTypeName;
6667
}
6768

6869
/**
@@ -79,6 +80,9 @@ public static function addNode(EncoderNode $node) {
7980
if ($nodeName === null || empty($nodeName) || !is_string($nodeName)) {
8081
throw new EncoderNodeException('Node without a name has been added. It must be a string and it cannot be empty.');
8182
}
83+
if ($node->getNodeTypeName() !== null) {
84+
throw new EncoderNodeException('The node you\'re trying to add seems to be a node type because it has a type name');
85+
}
8286
if (self::nodeExists($nodeName)) {
8387
throw new EncoderNodeException(sprintf('Node with name "%s" already exists', $nodeName));
8488
}
@@ -92,7 +96,9 @@ public static function addNode(EncoderNode $node) {
9296

9397
// make this node the default one if no type has yet been specified
9498
if (count(self::getNodeTypes($nodeName)) == 0) {
95-
self::addNodeType($node, self::DEFAULT_TYPE);
99+
// set the default type name so it can be registered as a type
100+
$node->typeName = $node->getDefaultType();
101+
self::addNodeType($node);
96102
}
97103

98104
self::softCleanNodeCache();
@@ -161,11 +167,14 @@ public static function nodeExists($nodeName) {
161167
* node. You can compare it with inheriting functions from a parent class.
162168
*
163169
* @param EncoderNode $nodeType
164-
* @param string $nodeTypeName
165-
* @return bool Returns true if the node type was successfully added
170+
* @return true Returns true if the node type was successfully added
166171
*/
167-
public static function addNodeType(EncoderNode $nodeType, $nodeTypeName) {
172+
public static function addNodeType(EncoderNode $nodeType) {
173+
if ($nodeType->getNodeTypeName() === null) {
174+
throw new EncoderNodeException('The node type you\'re trying to add seems to be a regular node because it has a no type name. Make sure you try to add an EncoderNode with a type name');
175+
}
168176
$nodeName = $nodeType->getNodeName();
177+
$nodeTypeName = $nodeType->getNodeTypeName();
169178
if (self::nodeTypeExists($nodeName, $nodeTypeName)) {
170179
throw new EncoderNodeException(sprintf('Node type with name "%s" and node type name "%s" already exists', $nodeName, $nodeTypeName));
171180
}
@@ -335,7 +344,7 @@ public function getNodeNameSingle() {
335344
return $this->nodeNameSingle;
336345
}
337346

338-
public function getTypeName() {
347+
public function getNodeTypeName() {
339348
return $this->typeName;
340349
}
341350

@@ -385,10 +394,6 @@ public function loadPlugin($pluginName) {
385394
throw new EncoderNodeException('Must be overwritten by subclasses');
386395
}
387396

388-
public function type() {
389-
return $this->typeName;
390-
}
391-
392397
/**
393398
* @param string $type
394399
* @return EncoderNode

tests/PE/Tests/Samples.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,13 @@ public function addBuildingNode() {
121121
EncoderNode::addNode(new BuildingNode());
122122
}
123123
public function addBuildingHouseNode() {
124-
EncoderNode::addNodeType(new HouseNode(), 'house');
124+
EncoderNode::addNodeType(new HouseNode());
125125
}
126126
public function addBuildingGreenhouseNode() {
127-
EncoderNode::addNodeType(new GreenhouseNode(), 'greenhouse');
127+
EncoderNode::addNodeType(new GreenhouseNode());
128128
}
129129
public function addBuildingBarnNode() {
130-
EncoderNode::addNodeType(new BarnNode(), 'barn');
130+
EncoderNode::addNodeType(new BarnNode());
131131
}
132132

133133
public function addAnimalNodes($children = true) {
@@ -141,16 +141,16 @@ public function addAnimalNodes($children = true) {
141141
}
142142

143143
public function addAnimalCatNode() {
144-
EncoderNode::addNodeType(new CatNode(), 'cat');
144+
EncoderNode::addNodeType(new CatNode());
145145
}
146146
public function addAnimalChickenNode() {
147-
EncoderNode::addNodeType(new ChickenNode(), 'chicken');
147+
EncoderNode::addNodeType(new ChickenNode());
148148
}
149149
public function addAnimalCowNode() {
150-
EncoderNode::addNodeType(new CowNode(), 'cow');
150+
EncoderNode::addNodeType(new CowNode());
151151
}
152152
public function addAnimalSheepNode() {
153-
EncoderNode::addNodeType(new SheepNode(), 'sheep');
153+
EncoderNode::addNodeType(new SheepNode());
154154
}
155155

156156

tests/PE/Tests/nodes/EncodeNodeTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class EncoderNodeTest extends Samples
1212

1313
const DEFAULT_NODE_NAME = 'nodes';
1414
const DEFAULT_NODE_NAME_SINGLE = 'node';
15+
const DEFAULT_NODE_TYPE_NAME = null;
1516
const DEFAULT_CLASS_PREPEND = null;
1617

1718
const DEFAULT_NODE_TYPE = 'type';
@@ -24,11 +25,20 @@ protected function setUp() {
2425
/**
2526
* @param string $nodeName
2627
* @param string $nodeNameSingle
27-
* @param null $classPrepend
28+
* @param string $classPrepend
29+
* @param string $nodeTypeName
2830
* @return EncoderNode
2931
*/
30-
protected function node($nodeName = self::DEFAULT_NODE_NAME, $nodeNameSingle = self::DEFAULT_NODE_NAME_SINGLE, $classPrepend = self::DEFAULT_CLASS_PREPEND) {
31-
return new EncoderNode($nodeName, $nodeNameSingle, $classPrepend);
32+
protected function node($nodeName = self::DEFAULT_NODE_NAME, $nodeNameSingle = self::DEFAULT_NODE_NAME_SINGLE, $classPrepend = self::DEFAULT_CLASS_PREPEND, $nodeTypeName = self::DEFAULT_NODE_TYPE_NAME) {
33+
return new EncoderNode($nodeName, $nodeNameSingle, $classPrepend, $nodeTypeName);
34+
}
35+
36+
/**
37+
* @param string $nodeTypeName
38+
* @return EncoderNode
39+
*/
40+
protected function nodeType($nodeTypeName = self::DEFAULT_NODE_TYPE_NAME) {
41+
return new EncoderNode(self::DEFAULT_NODE_NAME, self::DEFAULT_NODE_NAME_SINGLE, self::DEFAULT_CLASS_PREPEND, $nodeTypeName);
3242
}
3343

3444
public function testConstructor() {
@@ -52,6 +62,10 @@ public function testStaticAddNodeWithNonStringNodeName() {
5262
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeException', 'Node without a name has been added. It must be a string and it cannot be empty.');
5363
EncoderNode::addNode($this->node(null));
5464
}
65+
public function testStaticAddNodeWithNodeType() {
66+
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeException', 'The node you\'re trying to add seems to be a node type because it has a type name');
67+
EncoderNode::addNode($this->nodeType('cannot-be-a-type'));
68+
}
5569
public function testStaticAddNodeWithEmptyNodeName() {
5670
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeException', 'Node without a name has been added. It must be a string and it cannot be empty.');
5771
EncoderNode::addNode($this->node(''));
@@ -103,10 +117,14 @@ public function testStaticNodeExists() {
103117
$this->assertTrue(EncoderNode::nodeExists(self::DEFAULT_NODE_NAME_SINGLE));
104118
$this->assertFalse(EncoderNode::nodeExists('unknown'));
105119
}
106-
public function testStaticAddNodeType() {
120+
public function testStaticAddNodeTypeTwice() {
107121
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeException', 'Node type with name "buildings" and node type name "house" already exists');
108122
$this->addBuildingNode();
109123
$this->addBuildingHouseNode();
110124
$this->addBuildingHouseNode();
111125
}
126+
public function testStaticAddNodeTypeWithoutNodeType() {
127+
$this->setExpectedException('\\PE\\Exceptions\\EncoderNodeException', 'The node type you\'re trying to add seems to be a regular node because it has a no type name. Make sure you try to add an EncoderNode with a type name');
128+
EncoderNode::addNodeType($this->nodeType());
129+
}
112130
}

tests/PE/nodes/farm/AnimalNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77

88
class AnimalNode extends EncoderNode {
99

10-
function __construct($classPrepend = null) {
11-
parent::__construct('animals', 'animal', $classPrepend !== null ? $classPrepend : '\\PE\\Samples\\Farm');
10+
function __construct($classPrepend = null, $nodeTypeName = null) {
11+
parent::__construct('animals', 'animal', $classPrepend !== null ? $classPrepend : '\\PE\\Samples\\Farm', $nodeTypeName);
1212

1313
$this->addVariable(new EncoderNodeVariable('type'));
1414
$this->addVariable(new EncoderNodeVariable('name'));

tests/PE/nodes/farm/BuildingNode.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
class BuildingNode extends EncoderNode {
1111

12-
function __construct($classPrepend = null) {
12+
function __construct($classPrepend = null, $nodeTypeName = null) {
1313

14-
parent::__construct('buildings', 'building', $classPrepend !== null ? $classPrepend : '\\PE\\Samples\\Farm');
14+
parent::__construct('buildings', 'building', $classPrepend !== null ? $classPrepend : '\\PE\\Samples\\Farm', $nodeTypeName);
1515

1616
$this->addVariable(new EncoderNodeVariable('type', array(
1717
'getterAction' => array(

tests/PE/nodes/farm/animals/CatNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class CatNode extends CustomAnimal {
66

77
function __construct() {
8-
parent::__construct();
8+
parent::__construct('cat');
99
}
1010

1111
}

tests/PE/nodes/farm/animals/ChickenNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class ChickenNode extends CustomAnimal {
66

77
function __construct() {
8-
parent::__construct();
8+
parent::__construct('chicken');
99
}
1010

1111
}

tests/PE/nodes/farm/animals/CowNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class CowNode extends CustomAnimal {
66

77
function __construct() {
8-
parent::__construct();
8+
parent::__construct('cow');
99
}
1010

1111
}

tests/PE/nodes/farm/animals/CustomAnimal.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class CustomAnimal extends AnimalNode {
88

9-
function __construct() {
10-
parent::__construct('\\PE\\Samples\\Farm\\Animals');
9+
function __construct($nodeTypeName) {
10+
parent::__construct('\\PE\\Samples\\Farm\\Animals', $nodeTypeName);
1111
}
1212
}

0 commit comments

Comments
 (0)