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 FluentDOM\Loader\Supports { |
||
4 | |||
5 | use FluentDOM\Document; |
||
6 | use FluentDOM\DocumentFragment; |
||
7 | use FluentDOM\Exceptions\LoadingError; |
||
8 | use FluentDOM\Loader\Options; |
||
9 | use FluentDOM\Loader\Supports; |
||
10 | use FluentDOM\Loader\Result; |
||
11 | |||
12 | trait Json { |
||
13 | |||
14 | use Supports; |
||
15 | |||
16 | /** |
||
17 | * Load the json string into an DOMDocument |
||
18 | * |
||
19 | * @param mixed $source |
||
20 | * @param string $contentType |
||
21 | * @param array|\Traversable|Options $options |
||
22 | * @return Document|Result|NULL |
||
23 | */ |
||
24 | 2 | public function load($source, $contentType, $options = []) { |
|
25 | 2 | if (FALSE !== ($json = $this->getJson($source, $contentType, $options))) { |
|
26 | 1 | $dom = new Document('1.0', 'UTF-8'); |
|
27 | 1 | $this->transferTo($dom, $json); |
|
28 | 1 | return $dom; |
|
29 | } |
||
30 | 1 | return NULL; |
|
31 | } |
||
32 | |||
33 | /** |
||
34 | * @see Loadable::loadFragment |
||
35 | * |
||
36 | * @param string $source |
||
37 | * @param string $contentType |
||
38 | * @param array|\Traversable|Options $options |
||
39 | * @return DocumentFragment|NULL |
||
40 | */ |
||
41 | 2 | View Code Duplication | public function loadFragment($source, $contentType, $options = []) { |
1 ignored issue
–
show
|
|||
42 | 2 | if (FALSE !== ($json = $this->getJson($source, $contentType, $options))) { |
|
43 | 1 | $document = new Document('1.0', 'UTF-8'); |
|
44 | 1 | $fragment = $document->createDocumentFragment(); |
|
45 | 1 | $this->transferTo($fragment, $json); |
|
46 | 1 | return $fragment; |
|
47 | } |
||
48 | 1 | return NULL; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param mixed $source |
||
53 | * @param string $contentType |
||
54 | * @param array|\Traversable|Options $options |
||
55 | * @return mixed |
||
56 | */ |
||
57 | 10 | private function getJson($source, $contentType, $options) { |
|
58 | 10 | if ($this->supports($contentType)) { |
|
59 | 9 | if (is_string($source)) { |
|
60 | 4 | $json = FALSE; |
|
61 | 4 | $settings = $this->getOptions($options); |
|
62 | 4 | if ($settings->isAllowed($sourceType = $settings->getSourceType($source))) { |
|
63 | switch ($sourceType) { |
||
64 | /** @noinspection PhpMissingBreakStatementInspection */ |
||
65 | 4 | case Options::IS_FILE : |
|
66 | 1 | $source = file_get_contents($source); |
|
67 | 4 | case Options::IS_STRING : |
|
68 | 4 | $json = json_decode($source); |
|
69 | 4 | if (!($json || is_array($json))) { |
|
70 | 1 | throw new LoadingError\Json( |
|
71 | 1 | is_callable('json_last_error') ? json_last_error() : -1 |
|
72 | 1 | ); |
|
73 | } |
||
74 | 3 | } |
|
75 | 3 | } |
|
76 | 3 | } else { |
|
77 | 5 | $json = $source; |
|
78 | } |
||
79 | 8 | return ($json || is_array($json)) ? $json : FALSE; |
|
80 | } |
||
81 | 1 | return FALSE; |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param array|\Traversable|Options $options |
||
86 | * @return Options |
||
87 | */ |
||
88 | 4 | public function getOptions($options) { |
|
89 | 4 | $result = new Options( |
|
90 | 4 | $options, |
|
91 | [ |
||
92 | 4 | Options::CB_IDENTIFY_STRING_SOURCE => function($source) { |
|
93 | 4 | return $this->startsWith($source, '{') || $this->startsWith($source, '['); |
|
94 | } |
||
95 | 4 | ] |
|
96 | 4 | ); |
|
97 | 4 | return $result; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param \DOMNode|\DOMElement $node |
||
102 | * @param mixed $json |
||
103 | */ |
||
104 | protected abstract function transferTo(\DOMNode $node, $json); |
||
105 | |||
106 | /** |
||
107 | * @param mixed $value |
||
108 | * @return string |
||
109 | */ |
||
110 | 6 | private function getValueAsString($value) { |
|
111 | 6 | if (is_bool($value)) { |
|
112 | 2 | return $value ? 'true' : 'false'; |
|
113 | } else { |
||
114 | 4 | return (string)$value; |
|
115 | } |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param string $nodeName |
||
120 | * @param \stdClass $properties |
||
121 | * @param \DOMNode $parent |
||
122 | * @return string |
||
123 | */ |
||
124 | 2 | private function getNamespaceForNode( |
|
125 | $nodeName, \stdClass $properties, \DOMNode $parent |
||
126 | ) { |
||
127 | 2 | $prefix = substr($nodeName, 0, strpos($nodeName, ':')); |
|
128 | 2 | $xmlns = $this->getNamespacePropertyName($prefix); |
|
129 | 2 | return isset($properties->{$xmlns}) |
|
130 | 2 | ? $properties->{$xmlns} |
|
131 | 2 | : $parent->lookupNamespaceUri(empty($prefix) ? NULL : $prefix); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get the property name for a namespace prefix |
||
136 | * |
||
137 | * @param string $prefix |
||
138 | * @return string |
||
139 | */ |
||
140 | 6 | private function getNamespacePropertyName($prefix) { |
|
141 | 6 | return empty($prefix) ? 'xmlns' : 'xmlns:'.$prefix; |
|
142 | } |
||
143 | } |
||
144 | } |
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.