dansmaculotte /
laravel-prestashop-webservice
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | namespace DansMaCulotte\PrestashopWebService; |
||
| 4 | |||
| 5 | use SimpleXMLElement; |
||
| 6 | |||
| 7 | class PrestashopWebService extends PrestashopWebServiceLibrary |
||
| 8 | { |
||
| 9 | |||
| 10 | /** |
||
| 11 | * Retrieve the resource schema |
||
| 12 | * |
||
| 13 | * @param $resource |
||
| 14 | * @param string $schema |
||
| 15 | * @return SimpleXMLElement |
||
| 16 | * @throws Exceptions\PrestashopWebServiceException |
||
| 17 | */ |
||
| 18 | public function getSchema($resource, $schema = 'blank') |
||
| 19 | { |
||
| 20 | return $this->get(['resource' => $resource . "?schema=$schema"]); |
||
| 21 | } |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Fill the provided schema with an associative array data, also remove the useless XML nodes if |
||
| 25 | * the corresponding flag is true |
||
| 26 | * |
||
| 27 | * @param SimpleXMLElement $xmlSchema |
||
| 28 | * @param array $data |
||
| 29 | * @param bool $removeUselessNodes set true if you want to remove nodes that are not present in the data array |
||
| 30 | * @param array $removeSpecificNodes If $removeUselessNodes is false you may add here the first level nodes that |
||
| 31 | * you want to remove |
||
| 32 | * @return SimpleXMLElement |
||
| 33 | */ |
||
| 34 | public function fillSchema( |
||
| 35 | SimpleXMLElement $xmlSchema, |
||
| 36 | $data, |
||
| 37 | $removeUselessNodes = true, |
||
| 38 | $removeSpecificNodes = [] |
||
| 39 | ) { |
||
| 40 | $resource = $xmlSchema->children()->children(); |
||
| 41 | foreach ($data as $key => $value) { |
||
| 42 | $this->processNode($resource, $key, $value); |
||
| 43 | } |
||
| 44 | if ($removeUselessNodes) { |
||
| 45 | $this->checkForUselessNodes($resource, $data); |
||
| 46 | } else { |
||
| 47 | $this->removeSpecificNodes($resource, $removeSpecificNodes); |
||
| 48 | } |
||
| 49 | return $xmlSchema; |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param string|array $data |
||
| 54 | * @param $languageId |
||
| 55 | * @return string |
||
| 56 | */ |
||
| 57 | private function getLanguageValue($data, $languageId) |
||
| 58 | { |
||
| 59 | if (is_string($data)) { |
||
| 60 | return $data; |
||
| 61 | } |
||
| 62 | |||
| 63 | if (array_key_exists($languageId, $data)) { |
||
| 64 | return $data[$languageId]; |
||
| 65 | } else { |
||
| 66 | return $data[1]; |
||
| 67 | } |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param $node |
||
| 72 | * @param $data |
||
| 73 | */ |
||
| 74 | private function fillLanguageNode($node, $data) |
||
| 75 | { |
||
| 76 | for ($i = 0; $i < count($node->language); $i++) { |
||
|
0 ignored issues
–
show
|
|||
| 77 | $node->language[$i] = $this->getLanguageValue($data, (int) $node->language[$i]['id']->__toString()); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @param SimpleXMLElement $node |
||
| 83 | * @param $dataKey |
||
| 84 | * @param $dataValue |
||
| 85 | */ |
||
| 86 | private function processNode(SimpleXMLElement $node, $dataKey, $dataValue) |
||
| 87 | { |
||
| 88 | if (is_int($dataKey)) { |
||
| 89 | if ($dataKey === 0) { |
||
| 90 | $this->emptyNode($node); |
||
| 91 | } |
||
| 92 | $this->createNode($node, $dataValue); |
||
| 93 | } elseif (property_exists($node->$dataKey, 'language')) { |
||
| 94 | $this->fillLanguageNode($node->$dataKey, $dataValue); |
||
| 95 | } elseif (is_array($dataValue)) { |
||
| 96 | foreach ($dataValue as $key => $value) { |
||
| 97 | $this->processNode($node->$dataKey, $key, $value); |
||
| 98 | } |
||
| 99 | } else { |
||
| 100 | $node->$dataKey = $dataValue; |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Remove XML first level nodes that are not present int the data array |
||
| 106 | * @param SimpleXMLElement $resource |
||
| 107 | * @param $data |
||
| 108 | */ |
||
| 109 | private function checkForUselessNodes(SimpleXMLElement $resource, $data) |
||
| 110 | { |
||
| 111 | $uselessNodes = []; |
||
| 112 | foreach ($resource as $key => $value) { |
||
| 113 | if (!array_key_exists($key, $data)) { |
||
| 114 | $uselessNodes[] = $key; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | |||
| 118 | foreach ($uselessNodes as $key) { |
||
| 119 | unset($resource->$key); |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Remove the given nodes from the resource |
||
| 125 | * @param $resource |
||
| 126 | * @param $removeSpecificNodes |
||
| 127 | */ |
||
| 128 | private function removeSpecificNodes($resource, $removeSpecificNodes) |
||
| 129 | { |
||
| 130 | foreach ($removeSpecificNodes as $node) { |
||
| 131 | unset($resource->$node); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @param SimpleXMLElement $node |
||
| 137 | * @param array $dataValue |
||
| 138 | */ |
||
| 139 | private function createNode(SimpleXMLElement $node, $dataValue) |
||
| 140 | { |
||
| 141 | foreach ($dataValue as $key => $value) { |
||
| 142 | if (is_array($value)) { |
||
| 143 | if (is_int($key)) { |
||
| 144 | $this->createNode($node, $value); |
||
| 145 | } else { |
||
| 146 | $childNode=$node->addChild($key); |
||
| 147 | $this->createNode($childNode, $value); |
||
| 148 | } |
||
| 149 | } else { |
||
| 150 | $node->addChild($key, $value); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param SimpleXMLElement $node |
||
| 157 | */ |
||
| 158 | private function emptyNode(SimpleXMLElement $node) |
||
| 159 | { |
||
| 160 | $nodeNames = []; |
||
| 161 | foreach ($node->children() as $key => $value) { |
||
| 162 | $nodeNames[]=$key; |
||
| 163 | } |
||
| 164 | |||
| 165 | foreach ($nodeNames as $nodeName) { |
||
| 166 | unset($node->$nodeName); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | } |
||
| 170 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: