Completed
Push — master ( a52438...bf8826 )
by Henry
06:30
created

includes/Reader.php (2 issues)

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
use function stream_context_create;
22
23
/**
24
 * parent class to load and convert data
25
 *
26
 * @since 3.0.0
27
 *
28
 * @package Redaxscript
29
 * @category Reader
30
 * @author Henry Ruhs
31
 */
32
33
class Reader
34
{
35
	/**
36
	 * options of the reader
37
	 *
38
	 * @var array
39
	 */
40
41
	protected $_optionArray =
42
	[
43
		'timeout' => 1000
44
	];
45
46
	/**
47
	 * data object
48
	 *
49
	 * @var object
50
	 */
51
52
	protected $_dataObject;
53
54
	/**
55
	 * init the class
56
	 *
57
	 * @since 3.0.0
58
	 *
59
	 * @param array $optionArray options of the messenger
60
	 *
61
	 * @return self
62
	 */
63
64
	public function init(array $optionArray = []) : self
65
	{
66
		$this->_optionArray = array_replace_recursive($this->_optionArray, $optionArray);
67
		return $this;
68 3
	}
69
70 3
	/**
71 3
	 * get the object
72
	 *
73
	 * @since 3.0.0
74
	 *
75
	 * @return object|null
76
	 */
77
78
	public function getObject() : ?object
79
	{
80
		return $this->_dataObject ? : null;
81
	}
82 3
83
	/**
84 3
	 * get the array
85
	 *
86
	 * @since 3.1.0
87
	 *
88
	 * @return array|null
89
	 */
90
91
	public function getArray() : ?array
92
	{
93
		return json_decode(json_encode($this->_dataObject), true) ? : null;
94
	}
95 3
96
	/**
97 3
	 * get the json
98
	 *
99
	 * @since 3.1.0
100
	 *
101
	 * @return string
102
	 */
103
104
	public function getJSON() : string
105
	{
106
		return json_encode($this->_dataObject);
107
	}
108 3
109
	/**
110 3
	 * get the xml
111
	 *
112
	 * @since 3.0.0
113
	 *
114
	 * @return string
115
	 */
116
117
	public function getXML() : string
118
	{
119
		$dataObject = $this->getObject();
120
		if (method_exists($dataObject, 'asXML'))
121 3
		{
122
			return $dataObject->asXML();
123 3
		}
124 3
		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...
125
	}
126 2
127
	/**
128 1
	 * load the content from url
129
	 *
130
	 * @since 5.0.0
131
	 *
132
	 * @param string $url
133
	 *
134
	 * @return string|null
135
	 */
136
137
	public function load(string $url = null) : ?string
138
	{
139
		$output = null;
0 ignored issues
show
$output is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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