These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * Load a DOM document from a string or file that was the result of a |
||
4 | * SimpleXMLElement encoded as JSON. |
||
5 | * |
||
6 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
||
7 | * @copyright Copyright (c) 2009-2014 Bastian Feder, Thomas Weinert |
||
8 | */ |
||
9 | |||
10 | namespace FluentDOM\Loader\Json { |
||
11 | |||
12 | use FluentDOM\Document; |
||
13 | use FluentDOM\Element; |
||
14 | use FluentDOM\Loadable; |
||
15 | use FluentDOM\Loader\Result; |
||
16 | use FluentDOM\Loader\Supports; |
||
17 | |||
18 | /** |
||
19 | * Load a DOM document from a string or file that was the result of a |
||
20 | * SimpleXMLElement encoded as JSON. |
||
21 | */ |
||
22 | class SimpleXML implements Loadable { |
||
23 | |||
24 | use Supports\Json; |
||
25 | |||
26 | const XMLNS = 'urn:carica-json-dom.2013'; |
||
27 | |||
28 | /** |
||
29 | * Maximum recursions |
||
30 | * |
||
31 | * @var int |
||
32 | */ |
||
33 | private $_recursions = 100; |
||
34 | |||
35 | /** |
||
36 | * @return string[] |
||
37 | */ |
||
38 | 4 | public function getSupported() { |
|
39 | 4 | return ['text/simplexml', 'text/simplexml+json', 'application/simplexml+json']; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Load the json string into an DOMDocument |
||
44 | * |
||
45 | * @param mixed $source |
||
46 | * @param string $contentType |
||
47 | * @param array|\Traversable|Options $options |
||
48 | * @return Document|Result|NULL |
||
49 | */ |
||
50 | 4 | View Code Duplication | public function load($source, $contentType, $options = []) { |
51 | 4 | if (FALSE !== ($json = $this->getJson($source, $contentType, $options))) { |
|
0 ignored issues
–
show
|
|||
52 | 3 | $document = new Document('1.0', 'UTF-8'); |
|
53 | 3 | $document->appendChild( |
|
54 | 3 | $root = $document->createElementNS(self::XMLNS, 'json:json') |
|
55 | 3 | ); |
|
56 | 3 | $this->transferTo($root, $json); |
|
57 | 3 | return $document; |
|
58 | } |
||
59 | 1 | return NULL; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param \DOMNode|\DOMElement $node |
||
64 | * @param mixed $json |
||
65 | */ |
||
66 | 3 | protected function transferTo(\DOMNode $node, $json) { |
|
67 | /** @var Document $dom */ |
||
68 | 3 | $dom = $node->ownerDocument ?: $node; |
|
69 | 3 | if ($json instanceof \stdClass) { |
|
70 | 3 | foreach ($json as $name => $data) { |
|
71 | 3 | if ($name == '@attributes') { |
|
72 | 1 | if ($data instanceof \stdClass) { |
|
73 | 1 | foreach ($data as $attributeName => $attributeValue) { |
|
74 | 1 | $node->setAttribute($attributeName, $this->getValueAsString($attributeValue)); |
|
75 | 1 | } |
|
76 | 1 | } |
|
77 | 1 | } else { |
|
78 | 3 | $this->transferChildTo($node, $name, $data); |
|
79 | } |
||
80 | 3 | } |
|
81 | 3 | } elseif (is_scalar($json)) { |
|
82 | 2 | $node->appendChild( |
|
83 | 2 | $dom->createTextNode($this->getValueAsString($json)) |
|
84 | 2 | ); |
|
85 | 2 | } |
|
86 | 3 | } |
|
87 | |||
88 | /** |
||
89 | * @param \DOMNode $node |
||
90 | * @param string $name |
||
91 | * @param mixed $data |
||
92 | * @return array |
||
93 | */ |
||
94 | 3 | protected function transferChildTo(\DOMNode $node, $name, $data) { |
|
95 | /** @var Document $dom */ |
||
96 | 3 | $dom = $node->ownerDocument ?: $node; |
|
97 | 3 | if (!is_array($data)) { |
|
98 | 2 | $data = [$data]; |
|
99 | 2 | } |
|
100 | 3 | foreach ($data as $dataChild) { |
|
101 | 3 | $child = $node->appendChild($dom->createElement($name)); |
|
102 | 3 | $this->transferTo($child, $dataChild); |
|
103 | 3 | } |
|
104 | 3 | return $data; |
|
105 | } |
||
106 | } |
||
107 | } |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.