These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * A loader that converts JSONx into JsonDOM |
||
4 | * |
||
5 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License |
||
6 | * @copyright Copyright (c) 2009-2014 Bastian Feder, Thomas Weinert |
||
7 | */ |
||
8 | |||
9 | namespace FluentDOM\Loader { |
||
10 | |||
11 | use FluentDOM\Document; |
||
12 | use FluentDOM\DocumentFragment; |
||
13 | use FluentDOM\Element; |
||
14 | use FluentDOM\Loadable; |
||
15 | use FluentDOM\QualifiedName; |
||
16 | |||
17 | /** |
||
18 | * A lazy load group for php class loaders |
||
19 | * |
||
20 | * This defines loaders for PHP classes like SimpleXML |
||
21 | */ |
||
22 | class JSONx implements Loadable { |
||
23 | |||
24 | use Supports\Libxml; |
||
25 | |||
26 | const XMLNS_JSONX = 'http://www.ibm.com/xmlns/prod/2009/jsonx'; |
||
27 | const XMLNS_JSONDOM = 'urn:carica-json-dom.2013'; |
||
28 | const DEFAULT_QNAME = '_'; |
||
29 | |||
30 | /** |
||
31 | * @return string[] |
||
32 | */ |
||
33 | 14 | public function getSupported() { |
|
34 | 14 | return array('jsonx', 'application/xml+jsonx'); |
|
35 | } |
||
36 | |||
37 | /** |
||
38 | * @see Loadable::load |
||
39 | * @param string $source |
||
40 | * @param string $contentType |
||
41 | * @param array|\Traversable|Options $options |
||
42 | * @return Document|Result|NULL |
||
43 | */ |
||
44 | 12 | public function load($source, $contentType, $options = []) { |
|
45 | 12 | if ($this->supports($contentType) && !empty($source)) { |
|
46 | 11 | $document = (new Libxml\Errors())->capture( |
|
47 | 11 | View Code Duplication | function() use ($source, $contentType, $options) { |
1 ignored issue
–
show
|
|||
48 | 11 | $document = new Document(); |
|
49 | 11 | $document->preserveWhiteSpace = FALSE; |
|
50 | 11 | $document->registerNamespace('jx', self::XMLNS_JSONX); |
|
51 | 11 | $settings = $this->getOptions($options); |
|
52 | 11 | $settings->isAllowed($sourceType = $settings->getSourceType($source)); |
|
53 | switch ($sourceType) { |
||
54 | 11 | case Options::IS_FILE : |
|
55 | 1 | $document->load($source, $settings[Options::LIBXML_OPTIONS]); |
|
56 | 1 | break; |
|
57 | 10 | case Options::IS_STRING : |
|
58 | 10 | default : |
|
59 | 10 | $document->loadXML($source, $settings[Options::LIBXML_OPTIONS]); |
|
60 | 10 | } |
|
61 | 11 | return $document; |
|
62 | } |
||
63 | 11 | ); |
|
64 | 11 | $target = new Document(); |
|
65 | 11 | $target->registerNamespace('json', self::XMLNS_JSONDOM); |
|
66 | 11 | if (isset($document->documentElement)) { |
|
67 | 11 | $this->transferNode($document->documentElement, $target); |
|
68 | 11 | } |
|
69 | 11 | return $target; |
|
70 | } |
||
71 | 1 | return NULL; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @see Loadable::loadFragment |
||
76 | * |
||
77 | * @param string $source |
||
78 | * @param string $contentType |
||
79 | * @param array|\Traversable|Options $options |
||
80 | * @return DocumentFragment|NULL |
||
81 | */ |
||
82 | 2 | public function loadFragment($source, $contentType, $options = []) { |
|
83 | 2 | if ($this->supports($contentType) && !empty($source)) { |
|
84 | 1 | $document = new Document(); |
|
85 | 1 | $document->preserveWhiteSpace = FALSE; |
|
86 | 1 | $document->registerNamespace('jx', self::XMLNS_JSONX); |
|
87 | 1 | $sourceFragment = $document->createDocumentFragment(); |
|
88 | 1 | $sourceFragment->appendXml($source); |
|
89 | 1 | $target = new Document(); |
|
90 | 1 | $target->registerNamespace('json', self::XMLNS_JSONDOM); |
|
91 | 1 | $targetFragment = $target->createDocumentFragment(); |
|
92 | 1 | foreach ($sourceFragment->childNodes as $node) { |
|
93 | 1 | $this->transferNode($node, $targetFragment); |
|
94 | 1 | } |
|
95 | 1 | return $targetFragment; |
|
96 | } |
||
97 | 1 | return NULL; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param \DOMNode|Element $node |
||
102 | * @param \DOMNode|Document|Element $target |
||
103 | */ |
||
104 | 12 | private function transferNode(\DOMNode $node, \DOMNode $target) { |
|
105 | 12 | if ($node->namespaceURI === self::XMLNS_JSONX) { |
|
106 | 12 | if ($target instanceOf Document) { |
|
107 | 11 | $normalizedName = $name = 'json:json'; |
|
108 | 11 | } else { |
|
109 | 12 | $name = $node->getAttribute('name'); |
|
110 | 12 | $normalizedName = QualifiedName::normalizeString($name, self::DEFAULT_QNAME); |
|
111 | } |
||
112 | 12 | $type = $node->localName; |
|
113 | 12 | $newNode = $target->appendElement($normalizedName); |
|
114 | 12 | if ($name !== $normalizedName && $name !== '') { |
|
115 | 1 | $newNode->setAttribute('json:name', $name); |
|
116 | 1 | } |
|
117 | switch ($type) { |
||
118 | 12 | case 'object' : |
|
119 | 11 | if ($node('count(*) > 0')) { |
|
120 | 11 | foreach ($node('*') as $childNode) { |
|
121 | 11 | $this->transferNode($childNode, $newNode); |
|
122 | 11 | } |
|
123 | 11 | return; |
|
124 | } |
||
125 | 1 | break; |
|
126 | 11 | case 'array' : |
|
127 | 3 | foreach ($node('*') as $childNode) { |
|
128 | 3 | $this->transferNode($childNode, $newNode); |
|
129 | 3 | } |
|
130 | 3 | break; |
|
131 | 11 | case 'string' : |
|
132 | 7 | $newNode->append((string)$node); |
|
133 | 7 | return; |
|
134 | 5 | default : |
|
135 | 5 | $newNode->append((string)$node); |
|
136 | 5 | break; |
|
137 | 5 | } |
|
138 | 8 | $newNode->setAttribute('json:type', $type); |
|
139 | 11 | } |
|
140 | 8 | } |
|
141 | } |
||
142 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.