These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | namespace Redaxscript; |
||
3 | |||
4 | use SimpleXMLElement; |
||
5 | use function array_replace_recursive; |
||
6 | use function curl_close; |
||
7 | use function curl_exec; |
||
8 | use function curl_init; |
||
9 | use function curl_setopt; |
||
10 | use function curl_setopt_array; |
||
11 | use function file_get_contents; |
||
12 | use function function_exists; |
||
13 | use function is_array; |
||
14 | use function is_file; |
||
15 | use function is_numeric; |
||
16 | use function is_object; |
||
17 | use function json_decode; |
||
18 | use function json_encode; |
||
19 | use function method_exists; |
||
20 | use function simplexml_load_string; |
||
21 | |||
22 | /** |
||
23 | * parent class to load and convert data |
||
24 | * |
||
25 | * @since 3.0.0 |
||
26 | * |
||
27 | * @package Redaxscript |
||
28 | * @category Reader |
||
29 | * @author Henry Ruhs |
||
30 | */ |
||
31 | |||
32 | class Reader |
||
33 | { |
||
34 | /** |
||
35 | * options of the reader |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | |||
40 | protected $_optionArray = |
||
41 | [ |
||
42 | 'curl' => |
||
43 | [ |
||
44 | CURLOPT_RETURNTRANSFER => true, |
||
45 | CURLOPT_FOLLOWLOCATION => true, |
||
46 | CURLOPT_TIMEOUT_MS => 1000 |
||
47 | ] |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * data object |
||
52 | * |
||
53 | * @var object |
||
54 | */ |
||
55 | |||
56 | protected $_dataObject; |
||
57 | |||
58 | /** |
||
59 | * init the class |
||
60 | * |
||
61 | * @since 3.0.0 |
||
62 | * |
||
63 | * @param array $optionArray options of the messenger |
||
64 | * |
||
65 | * @return self |
||
66 | */ |
||
67 | |||
68 | 3 | public function init(array $optionArray = []) : self |
|
69 | { |
||
70 | 3 | $this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray); |
|
71 | 3 | return $this; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * get the object |
||
76 | * |
||
77 | * @since 3.0.0 |
||
78 | * |
||
79 | * @return object|null |
||
80 | */ |
||
81 | |||
82 | 3 | public function getObject() : ?object |
|
83 | { |
||
84 | 3 | return $this->_dataObject ? : null; |
|
85 | } |
||
86 | |||
87 | /** |
||
88 | * get the array |
||
89 | * |
||
90 | * @since 3.1.0 |
||
91 | * |
||
92 | * @return array|null |
||
93 | */ |
||
94 | |||
95 | 2 | public function getArray() : ?array |
|
96 | { |
||
97 | 2 | return json_decode(json_encode($this->_dataObject), true); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * get the json |
||
102 | * |
||
103 | * @since 3.1.0 |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | |||
108 | 2 | public function getJSON() : string |
|
109 | { |
||
110 | 2 | return json_encode($this->_dataObject); |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * get the xml |
||
115 | * |
||
116 | * @since 3.0.0 |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | |||
121 | 2 | public function getXML() : string |
|
122 | { |
||
123 | 2 | $dataObject = $this->getObject(); |
|
124 | 2 | if (method_exists($dataObject, 'asXML')) |
|
125 | { |
||
126 | 1 | return $dataObject->asXML(); |
|
127 | } |
||
128 | 1 | return $this->_convertArrayToObject($this->getArray())->asXML(); |
|
0 ignored issues
–
show
|
|||
129 | } |
||
130 | |||
131 | /** |
||
132 | * load json from url |
||
133 | * |
||
134 | * @since 3.1.0 |
||
135 | * |
||
136 | * @param string $url |
||
137 | * |
||
138 | * @return self |
||
139 | */ |
||
140 | |||
141 | 1 | public function loadJSON(string $url = null) : self |
|
142 | { |
||
143 | 1 | $content = $this->load($url); |
|
144 | 1 | $this->_dataObject = json_decode($content); |
|
145 | 1 | return $this; |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * load xml from url |
||
150 | * |
||
151 | * @since 3.0.0 |
||
152 | * |
||
153 | * @param string $url |
||
154 | * |
||
155 | * @return self |
||
156 | */ |
||
157 | |||
158 | 2 | public function loadXML(string $url = null) : self |
|
159 | { |
||
160 | 2 | $content = $this->load($url); |
|
161 | 2 | $this->_dataObject = simplexml_load_string($content); |
|
162 | 2 | return $this; |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * load from url |
||
167 | * |
||
168 | * @since 3.0.0 |
||
169 | * |
||
170 | * @param string $url |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | |||
175 | 3 | public function load(string $url = null) : string |
|
176 | { |
||
177 | /* remote curl */ |
||
178 | |||
179 | 3 | if (function_exists('curl_version') && !is_file($url)) |
|
180 | { |
||
181 | 1 | $curl = curl_init(); |
|
182 | 1 | curl_setopt_array($curl, $this->_optionArray['curl']); |
|
183 | 1 | curl_setopt($curl, CURLOPT_URL, $url); |
|
184 | 1 | $output = curl_exec($curl); |
|
185 | 1 | curl_close($curl); |
|
186 | } |
||
187 | |||
188 | /* else fallback */ |
||
189 | |||
190 | else |
||
191 | { |
||
192 | 2 | $output = file_get_contents($url); |
|
193 | } |
||
194 | 3 | return $output; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * convert array to object |
||
199 | * |
||
200 | * @since 3.1.0 |
||
201 | * |
||
202 | * @param array $dataArray |
||
203 | * @param object $dataObject |
||
204 | * |
||
205 | * @return object |
||
206 | */ |
||
207 | |||
208 | 1 | protected function _convertArrayToObject(array $dataArray = [], object $dataObject = null) : object |
|
209 | { |
||
210 | 1 | if (!is_object($dataObject)) |
|
211 | { |
||
212 | 1 | $dataObject = new SimpleXMLElement('<root />'); |
|
213 | } |
||
214 | |||
215 | /* process data */ |
||
216 | |||
217 | 1 | foreach ($dataArray as $key => $value) |
|
218 | { |
||
219 | 1 | if(is_numeric($key)) |
|
220 | { |
||
221 | 1 | $key = 'children'; |
|
222 | } |
||
223 | 1 | if ($key === '@attributes') |
|
224 | { |
||
225 | 1 | foreach ($value as $attributeKey => $attributeValue) |
|
226 | { |
||
227 | 1 | $dataObject->addAttribute($attributeKey, $attributeValue); |
|
228 | } |
||
229 | } |
||
230 | 1 | else if (is_array($value)) |
|
231 | { |
||
232 | 1 | $this->_convertArrayToObject($value, $dataObject->addChild($key)); |
|
233 | } |
||
234 | else |
||
235 | { |
||
236 | 1 | $dataObject->addChild($key, $value); |
|
237 | } |
||
238 | } |
||
239 | 1 | return $dataObject; |
|
240 | } |
||
241 | } |
||
242 |
This check looks at variables that 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.