Completed
Push — master ( 3f0de9...a2fb8e )
by Henry
48:12
created

Reader::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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