Skip to content

Commit 68a9c11

Browse files
committed
Object setters now have a require flag
1 parent ead1f76 commit 68a9c11

File tree

7 files changed

+80
-1
lines changed

7 files changed

+80
-1
lines changed

src/PE/Encoder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
249249
}
250250
$variableIsset = isset($nodeDataItem[$variableId]);
251251
$objectSetter = $objectSetterVariable->getObjectSetter();
252+
if ($objectSetter->required() && !$variableIsset) {
253+
throw new EncoderException(sprintf('Decoding failed because variable "%s" for node "%s" is required but isn\'t present in the node data.', $variableId, $nodeName));
254+
}
252255
if ($variableIsset || $objectSetter->alwaysExecute()) {
253256
$objectSetter->apply($nodeInstance, $variableIsset ? $nodeDataItem[$variableId] : null);
254257
}

src/PE/Variables/Types/ObjectSetter.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,23 @@
1414
*/
1515
final class ObjectSetter extends ObjectAccessor {
1616

17-
function __construct($method = null) {
17+
private $required;
18+
19+
function __construct($method = null, $required = false) {
1820
parent::__construct($method);
21+
22+
$this->required($required);
23+
}
24+
25+
/**
26+
* @param null|bool $bool
27+
* @return bool
28+
*/
29+
public function required($bool = null) {
30+
if ($bool !== null && is_bool($bool)) {
31+
$this->required = $bool;
32+
}
33+
return (bool) $this->required;
1934
}
2035

2136
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace PE\Nodes\Specials;
4+
5+
use PE\Nodes\EncoderNode;
6+
use PE\Nodes\EncoderNodeVariable;
7+
8+
class RequiredVariableNode extends EncoderNode {
9+
10+
function __construct() {
11+
parent::__construct('required-variables', 'required-variable', '\\PE\\Samples\\Specials');
12+
13+
$thing = $this->addVariable(new EncoderNodeVariable('required'));
14+
$thing->getObjectSetter()->required(true);
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace PE\Samples\Specials;
4+
5+
class RequiredVariable {
6+
7+
protected $required;
8+
9+
function setRequired($value) {
10+
$this->required = $value;
11+
}
12+
function getRequired() {
13+
return $this->required;
14+
}
15+
}

tests/PE/Tests/EncoderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ public function testEncodeDecodedSingleChild() {
103103
}
104104

105105

106+
public function testRequiredVariable() {
107+
$this->setExpectedException('\\PE\\Exceptions\\EncoderException', 'Decoding failed because variable "required" for node "required-variable" is required but isn\'t present in the node data.');
108+
109+
$this->addRequireVariableNode();
110+
111+
$this->encoder()->decode(array(
112+
'required-variable' => array()
113+
));
114+
}
115+
106116

107117
public function testDecode() {
108118
$this->addFarmNodes();

tests/PE/Tests/Samples.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use PE\Nodes\Specials\NonArrayGetterMethodOnPurposeNode;
3131
use PE\Nodes\Specials\OptionalVariablesNode;
3232
use PE\Nodes\Specials\RequiredConstructorVariablesNode;
33+
use PE\Nodes\Specials\RequiredVariableNode;
3334
use PE\Nodes\Specials\SingleChildNode;
3435
use PE\Nodes\Specials\VariableTypesNode;
3536
use PE\Samples\Erroneous\EncoderNodeLoader;
@@ -52,6 +53,7 @@
5253
use PE\Samples\Specials\NonArrayGetterMethodOnPurpose;
5354
use PE\Samples\Specials\AccessorMethodActionTypeNode;
5455
use PE\Samples\Specials\OptionalVariables;
56+
use PE\Samples\Specials\RequiredVariable;
5557
use PE\Samples\Specials\SingleChild;
5658
use PE\Samples\Specials\VariableTypes;
5759

@@ -252,6 +254,13 @@ public function addEncoderNodeLoaderNode($overrideObjectFileName = true) {
252254
}
253255

254256

257+
public function getRequiredVariable() {
258+
return new RequiredVariable();
259+
}
260+
public function addRequireVariableNode() {
261+
return EncoderNode::addNode(new RequiredVariableNode());
262+
}
263+
255264
public function getSingleChild() {
256265
return new SingleChild();
257266
}

tests/PE/Tests/Variables/Types/ObjectSetterTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public function testGetMethod() {
4545
$this->assertEquals('setVariableId', $objectSetter->getMethod());
4646
}
4747

48+
public function testRequired() {
49+
$objectSetter = $this->objectSetter();
50+
51+
$this->assertFalse($objectSetter->required());
52+
53+
$this->assertFalse($objectSetter->required(false));
54+
$this->assertFalse($objectSetter->required());
55+
$this->assertTrue($objectSetter->required(true));
56+
$this->assertTrue($objectSetter->required());
57+
}
58+
4859
public function testEncodeWithoutVariableGetterMethod() {
4960
$this->setExpectedException('\\PE\\Exceptions\\VariableTypeException', 'Method "setNonExistent" does not exist for class "PE\Tests\Variables\Types\ObjectSetterTestObject');
5061

0 commit comments

Comments
 (0)