Skip to content

Commit bda44b1

Browse files
fillSchema() now removes all nodes that are not present in the data array ($removeUselessNodes flag is true as default)
1 parent d8c315f commit bda44b1

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/PrestashopWebService.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,22 @@ public function getSchema($resource)
2121
}
2222

2323
/**
24-
* Fill the provided schema with an associative array data
24+
* Fill the provided schema with an associative array data, also remove the useless XML nodes if the corresponding flag is true
2525
*
2626
* @param SimpleXMLElement $xmlSchema
2727
* @param array $data
28+
* @param bool $removeUselessNodes set true if you want to remove nodes that are not present in the data array
2829
* @return SimpleXMLElement
2930
*/
30-
public function fillSchema(SimpleXMLElement $xmlSchema, $data)
31+
public function fillSchema(SimpleXMLElement $xmlSchema, $data, $removeUselessNodes = true)
3132
{
3233
$resource = $xmlSchema->children()->children();
3334
foreach ($data as $key => $value) {
3435
$this->processNode($resource, $key, $value);
3536
}
37+
if ($removeUselessNodes) {
38+
$this->checkForUselessNode($resource, $data);
39+
}
3640
return $xmlSchema;
3741
}
3842

@@ -82,4 +86,22 @@ private function processNode(SimpleXMLElement $node, $dataKey, $dataValue)
8286
$node->$dataKey = $dataValue;
8387
}
8488
}
89+
90+
/**
91+
* Remove XML first level nodes that are not present int the data array
92+
* @param SimpleXMLElement $resource
93+
* @param $data
94+
*/
95+
private function checkForUselessNode(SimpleXMLElement $resource, $data)
96+
{
97+
$uselessNodes = [];
98+
foreach ($resource as $key => $value) {
99+
if (!array_key_exists($key, $data)) {
100+
$uselessNodes[] = $key;
101+
}
102+
}
103+
foreach ($uselessNodes as $key) {
104+
unset($resource->$key);
105+
}
106+
}
85107
}

0 commit comments

Comments
 (0)