-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXml.php
More file actions
260 lines (233 loc) · 5.8 KB
/
Xml.php
File metadata and controls
260 lines (233 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php if ( ! defined('DENY_ACCESS')) exit('403: No direct file access allowed');
/**
* A Bright CMS
*
* Open source, lightweight, web application framework and content management
* system in PHP.
*
* @package A Bright CMS
* @author Gabriel Liwerant
*/
/**
* Xml Class
*
* @subpackage system/core
* @author Gabriel Liwerant
*/
class Xml implements ApplicationStorageInterface
{
/**
* Error codes for Xml
*/
const XML_NOT_INSTANCE_OF_SIMPLE_XML_ELEMENT = 1001;
const INVALID_XML_FILE = 1002;
const COULD_NOT_CONVERT_TO_BOOELAN = 1003;
const FILE_DOES_NOT_EXIST = 1004;
const FILE_IS_NOT_READABLE = 1005;
/**
* Stores array of arrays for xml files with their associated data.
*
* @var array $_xml
*/
private $_xml = array();
/**
* Nothing to see here...
*/
public function __construct()
{
//
}
/**
* Uses DOMDocument to check for errors in XML file.
*
* @param string $file_path
*
* @todo consider logging errors without coupling logger class
* @deprecated consider removing in favor of well formed checking since
* there are issues the the implementation of this method.
*
* @return boolean
*/
public function isXmlFileValid($file_path)
{
// Allow us to retrieve internal errors
libxml_use_internal_errors(true);
$dom_doc = new DOMDocument('1.0', 'UTF-8');
$dom_doc->loadXML($file_path);
$errors = libxml_get_errors();
if (empty($errors))
{
return true;
}
// @kludge error code 4 is giving an issue with XML when there is no
// discerinble problem. The internet does not know what is going on
// either. We throw our hands up for now and pretend that code 4
// doesn't matter.
if ($errors[0]->level < 3 OR $errors[0]->code === 4)
{
// Should also be fine, but we may want to log
//Debug::printArray($errors);
return true;
}
else
{
//Debug::printArray($errors);
return false;
}
}
/**
* Converts SimpleXMLElement object to an array using recursion to loop
* through all the nested objects.
*
* @param SimpleXMLElement object $xml
*
* @return array
*/
public function convertSimpleXmlElementToArray($xml)
{
if ($xml instanceof SimpleXMLElement)
{
$children = $xml->children();
}
else
{
throw ApplicationFactory::makeException('Xml Exception', self::XML_NOT_INSTANCE_OF_SIMPLE_XML_ELEMENT);
//throw new Exception('Xml Exception', self::XML_NOT_INSTANCE_OF_SIMPLE_XML_ELEMENT);
}
foreach ($children as $key => $value)
{
if ( ! $value instanceof SimpleXMLElement)
{
continue;
}
$values = (array)$value->children();
if (count($values) > 0)
{
$xml_arr[$key] = $this->convertSimpleXmlElementToArray($value);
}
else
{
if ( ! isset($xml_arr[$key]))
{
$xml_arr[$key] = (string)$value;
}
else
{
if ( ! is_array($xml_arr[$key]))
{
$xml_arr[$key] = array($xml_arr[$key], (string)$value);
}
else
{
$xml_arr[$key][] = (string)$value;
}
}
}
}
return $xml_arr;
}
/**
* Handles XML decoding.
*
* @param string $file_path
*
* @return array
*/
public function getXmlDecode($file_path)
{
$is_xml_valid = $this->isXmlFileValid($file_path);
if ($is_xml_valid)
{
$xml = simplexml_load_file($file_path);
}
else
{
throw ApplicationFactory::makeException('Xml Exception', self::INVALID_XML_FILE);
//throw new Exception('Xml Exception', self::INVALID_XML_FILE);
}
return $this->convertSimpleXmlElementToArray($xml);
}
/**
* Encode a simple string as XML.
*
* @todo make functional for array with recursion
* @todo method needs to be tested
*
* @param string $value
*/
public function getEncodedDataAsString($value)
{
return '<root>' . $value . '</root>';
//throw ApplicationFactory::makeException('Xml Exception: ' . __METHOD__ . ' not yet built.');
//throw new Exception('Xml Exception: ' . __METHOD__ . ' not yet built.');
}
/**
* Loads an XML file and then stores the decoded data into an array.
*
* @param string $path Path to the XML file we want data from
* @param string $key Allows us to set a name for the XML array
*
* @return object Xml
*/
public function setFileAsArray($path, $key)
{
if ( ! file_exists($path))
{
throw ApplicationFactory::makeException('Xml Exception', self::FILE_DOES_NOT_EXIST);
//throw new Exception('Xml Exception', self::FILE_DOES_NOT_EXIST);
}
$this->_xml[$key] = $this->getXmlDecode($path);
return $this;
}
/**
* Get an XML file as an array from our property.
*
* @param string $xml_key Is the name of the XML file
*
* @return array Gets us the specific array we want
*/
public function getFileAsArray($xml_key)
{
return $this->_xml[$xml_key];
}
/**
* Get all XML files as an array of arrays.
*
* @return array
*/
public function getAllDataAsArray()
{
return $this->_xml;
}
/**
* We sometimes need a true or false value passed with a string. We may
* attempt to pass "true" and "false" and this function will convert them to
* their intended boolean. Use with care.
*
* @deprecated method is dangerous and unnecessary, we should use 0 | 1 and
* cast them as boolean instead
*
* @param string $psuedo_boolean String we attempt to convert to boolean
*
* @return boolean Successfully converted boolean value
*/
public function getStringValueAsBoolean($psuedo_boolean)
{
if ($psuedo_boolean === 'true')
{
$real_boolean = true;
}
elseif ($psuedo_boolean === 'false')
{
$real_boolean = false;
}
else
{
throw ApplicationFactory::makeException('Xml Exception', self::COULD_NOT_CONVERT_TO_BOOELAN);
//throw new Exception('Json Exception', self::COULD_NOT_CONVERT_TO_BOOELAN);
}
return $real_boolean;
}
}
// End of Xml Class
/* EOF system/core/Xml.php */