Completed
Push — master ( e9b1ca...cbd317 )
by Henry
07:52
created

includes/Reader.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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 3
	 *
53
	 * @var object
54 3
	 */
55 3
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 3
	 */
67
68 3
	public function init(array $optionArray = []) : self
69
	{
70
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
71
		return $this;
72
	}
73
74
	/**
75
	 * get the object
76
	 *
77
	 * @since 3.0.0
78
	 *
79 2
	 * @return object|null
80
	 */
81 2
82
	public function getObject() : ?object
83
	{
84
		return $this->_dataObject ? : null;
85
	}
86
87
	/**
88
	 * get the array
89
	 *
90
	 * @since 3.1.0
91
	 *
92 2
	 * @return array|null
93
	 */
94 2
95
	public function getArray() : ?array
96
	{
97
		return json_decode(json_encode($this->_dataObject), true);
98
	}
99
100
	/**
101
	 * get the json
102
	 *
103
	 * @since 3.1.0
104
	 *
105 2
	 * @return string
106
	 */
107 2
108
	public function getJSON() : string
109 1
	{
110
		return json_encode($this->_dataObject);
111 1
	}
112
113
	/**
114
	 * get the xml
115
	 *
116
	 * @since 3.0.0
117
	 *
118
	 * @return string
119
	 */
120
121
	public function getXML() : string
122
	{
123
		$dataObject = $this->getObject();
124 1
		if (method_exists($dataObject, 'asXML'))
125
		{
126 1
			return $dataObject->asXML();
127 1
		}
128 1
		return $this->_convertArrayToObject($this->getArray())->asXML();
0 ignored issues
show
It seems like $this->getArray() targeting Redaxscript\Reader::getArray() can also be of type null; however, Redaxscript\Reader::_convertArrayToObject() does only seem to accept array, maybe add an additional type check?

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.

Loading history...
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 2
	public function loadJSON(string $url = null) : self
142
	{
143 2
		$content = $this->load($url);
144 2
		$this->_dataObject = json_decode($content);
145 2
		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 3
	public function loadXML(string $url = null) : self
159
	{
160
		$content = $this->load($url);
161
		$this->_dataObject = simplexml_load_string($content);
162 3
		return $this;
163
	}
164 1
165 1
	/**
166 1
	 * load from url
167 1
	 *
168 1
	 * @since 3.0.0
169
	 *
170
	 * @param string $url
171
	 *
172
	 * @return string
173
	 */
174
175 2
	public function load(string $url = null) : string
176
	{
177 3
		/* remote curl */
178
179
		if (function_exists('curl_version') && !is_file($url))
180
		{
181
			$curl = curl_init();
182
			curl_setopt_array($curl, $this->_optionArray['curl']);
183
			curl_setopt($curl, CURLOPT_URL, $url);
184
			$output = curl_exec($curl);
185
			curl_close($curl);
186
		}
187
188
		/* else fallback */
189
190
		else
191 1
		{
192
			$output = file_get_contents($url);
193 1
		}
194
		return $output;
195 1
	}
196
197
	/**
198
	 * convert array to object
199
	 *
200 1
	 * @since 3.1.0
201
	 *
202 1
	 * @param array $dataArray
203
	 * @param object $dataObject
204 1
	 *
205
	 * @return object
206 1
	 */
207
208 1
	protected function _convertArrayToObject(array $dataArray = [], object $dataObject = null) : object
209
	{
210 1
		if (!is_object($dataObject))
211
		{
212
			$dataObject = new SimpleXMLElement('<root />');
213 1
		}
214
215 1
		/* process data */
216
217
		foreach ($dataArray as $key => $value)
218
		{
219 1
			if(is_numeric($key))
220
			{
221
				$key = 'children';
222 1
			}
223
			if ($key === '@attributes')
224
			{
225
				foreach ($value as $attributeKey => $attributeValue)
226
				{
227
					$dataObject->addAttribute($attributeKey, $attributeValue);
228
				}
229
			}
230
			else if (is_array($value))
231
			{
232
				$this->_convertArrayToObject($value, $dataObject->addChild($key));
233
			}
234
			else
235
			{
236
				$dataObject->addChild($key, $value);
237
			}
238
		}
239
		return $dataObject;
240
	}
241
}
242