Skip to content

Commit 873e082

Browse files
committed
Fixed the "variablesAreValidWithData" method
1 parent 04fbe60 commit 873e082

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

src/PE/Encoder.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,9 @@ protected function decodeChildNames($structure) {
9797
* @param array $nodeData
9898
* @param EncoderOptions $options
9999
* @param EncoderNode|null $parentNode
100-
* @param array $parentObject
100+
* @param object $parentObject
101101
* @param array $parentNodeData
102102
* @return array[]
103-
*
104-
* @todo setAfterChildren doesn't work when it has the wrong order, so perhaps I need to build a sorting mechanism. Perhaps in "decodeRawToArray"?
105103
*/
106104
protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, EncoderNode $parentNode = null, $parentObject = null, $parentNodeData = null) {
107105

@@ -234,8 +232,7 @@ protected function _decodeNode($nodeName, $nodeData, EncoderOptions $options, En
234232

235233
$nodeIndex++;
236234
}
237-
// @todo this broke, perhaps this can be fixed some time
238-
//$proxyNode->variablesAreValid($decodedChildren);
235+
$proxyNode->variablesAreValid($decodedChildren, true);
239236

240237
if ($isSingleNode) {
241238
return $objects[0];

src/PE/nodes/EncoderNode.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,12 +549,13 @@ public function variableExists($id) {
549549

550550
/**
551551
* @param array $nodeDataArray
552-
* @return bool
552+
* @param bool $throwErrorIfFails Set to true if you want it to throw an error if it fails
553+
* @return bool Returns true if all requirements are met
553554
*
554555
* @see EncoderNodeVariable::variablesAreValidWithData()
555556
*/
556-
public function variablesAreValid($nodeDataArray) {
557-
return $this->variables->variablesAreValidWithData($nodeDataArray);
557+
public function variablesAreValid($nodeDataArray, $throwErrorIfFails = false) {
558+
return $this->variables->variablesAreValidWithData($nodeDataArray, $throwErrorIfFails);
558559
}
559560

560561
/**
@@ -622,7 +623,7 @@ public function loadObject($object = null) {
622623
throw new EncoderNodeException('Object for loading cannot be null');
623624
}
624625
}
625-
return $this->_loadObject($object);
626+
$this->_loadObject($object);
626627
}
627628

628629
/**

src/PE/nodes/EncoderNodeVariableCollection.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@ public function addVariable(Variable $variable) {
5555

5656
/**
5757
* @param $dataArray
58+
* @param bool $throwErrorIfFails Set to true if you want it to throw an error if it fails
5859
* @return bool Returns true if all requirements are met
5960
*/
60-
public function variablesAreValidWithData($dataArray) {
61+
public function variablesAreValidWithData($dataArray, $throwErrorIfFails = false) {
6162
$variables = $this->getVariables();
6263
$unique = array();
6364
foreach ($dataArray as $data) {
@@ -70,7 +71,12 @@ public function variablesAreValidWithData($dataArray) {
7071
}
7172
$variableValue = $data[$variableId];
7273
if (array_search($variableValue, $unique[$variableId]) !== false) {
73-
throw new EncoderNodeVariableException(sprintf('Variable "%s" must be unique but value "%s" is given at least twice', $variableId, $variableValue));
74+
if ($throwErrorIfFails) {
75+
throw new EncoderNodeVariableException(sprintf('Variable "%s" must be unique but value "%s" is given at least twice', $variableId, $variableValue));
76+
}
77+
else {
78+
return false;
79+
}
7480
}
7581
$unique[$variableId][] = $variableValue;
7682
}

tests/PE/Tests/nodes/EncoderNodeVariableCollectionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public function testVariablesAreValidWithData() {
7171
$variable->mustBeUnique(true);
7272

7373
$this->assertTrue($collection->variablesAreValidWithData(array(array('var' => 'Test'))));
74+
$this->assertFalse($collection->variablesAreValidWithData(array(array('var' => 'Test'), array('var' => 'Test'))));
7475
}
7576

7677
public function testVariablesAreNotValidWithData() {
@@ -80,7 +81,7 @@ public function testVariablesAreNotValidWithData() {
8081
$variable = $this->collectionAddVariable(new EncoderNodeVariable('var'));
8182
$variable->mustBeUnique(true);
8283

83-
$collection->variablesAreValidWithData(array(array('var' => 'Test'), array('var' => 'Test')));
84+
$collection->variablesAreValidWithData(array(array('var' => 'Test'), array('var' => 'Test')), true);
8485
}
8586

8687
public function testGetAlwaysExecutedVariables() {

0 commit comments

Comments
 (0)