1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see AppUtils\XMLHelper_DOMErrors} class. |
4
|
|
|
* |
5
|
|
|
* @package Application Utils |
6
|
|
|
* @subpackage XMLHelper |
7
|
|
|
* @see XMLHelper_DOMErrors |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AppUtils; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Container for libxml errors: converts an array of libxml errors |
16
|
|
|
* to dom error instances which are a lot easier to work with. |
17
|
|
|
* |
18
|
|
|
* @package Application Utils |
19
|
|
|
* @subpackage XMLHelper |
20
|
|
|
* @author Sebastian Mordziol <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class XMLHelper_DOMErrors |
23
|
|
|
{ |
24
|
|
|
const SERIALIZE_SEPARATOR = '__SERSEP__'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var XMLHelper_DOMErrors_Error[] |
28
|
|
|
*/ |
29
|
|
|
private $errors; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \LibXMLError[]|XMLHelper_DOMErrors_Error[] $libxmlErrors |
33
|
|
|
*/ |
34
|
|
|
public function __construct(array $libxmlErrors) |
35
|
|
|
{ |
36
|
|
|
foreach($libxmlErrors as $error) |
37
|
|
|
{ |
38
|
|
|
if($error instanceof XMLHelper_DOMErrors_Error) |
39
|
|
|
{ |
40
|
|
|
$this->errors[] = $error; |
41
|
|
|
} |
42
|
|
|
else if($error instanceof \LibXMLError) |
43
|
|
|
{ |
44
|
|
|
$this->errors[] = new XMLHelper_DOMErrors_Error($error); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function isValid() : bool |
50
|
|
|
{ |
51
|
|
|
return empty($this->errors); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getAll() |
55
|
|
|
{ |
56
|
|
|
return $this->errors; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Retrieves all warnings, if any. |
61
|
|
|
* |
62
|
|
|
* @return XMLHelper_DOMErrors_Error[] |
63
|
|
|
*/ |
64
|
|
|
public function getWarnings() |
65
|
|
|
{ |
66
|
|
|
return $this->getByLevel(LIBXML_ERR_WARNING); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getErrors() |
70
|
|
|
{ |
71
|
|
|
return $this->getByLevel(LIBXML_ERR_ERROR); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getFatalErrors() |
75
|
|
|
{ |
76
|
|
|
return $this->getByLevel(LIBXML_ERR_FATAL); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getNestingErrors() |
80
|
|
|
{ |
81
|
|
|
return $this->getByCode(XMLHelper_LibXML::TAG_NAME_MISMATCH); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function hasWarnings() : bool |
85
|
|
|
{ |
86
|
|
|
return $this->hasErrorsByLevel(LIBXML_ERR_WARNING); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function hasErrors() : bool |
90
|
|
|
{ |
91
|
|
|
return $this->hasErrorsByLevel(LIBXML_ERR_ERROR); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function hasFatalErrors() : bool |
95
|
|
|
{ |
96
|
|
|
return $this->hasErrorsByLevel(LIBXML_ERR_FATAL); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function hasNestingErrors() : bool |
100
|
|
|
{ |
101
|
|
|
return $this->hasErrorsByCode(XMLHelper_LibXML::TAG_NAME_MISMATCH); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function hasUnknownTags() : bool |
105
|
|
|
{ |
106
|
|
|
return $this->hasErrorsByCode(XMLHelper_LibXML::XML_HTML_UNKNOWN_TAG); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Retrieves all errors by the specified libxml error level. |
112
|
|
|
* |
113
|
|
|
* @param int $level |
114
|
|
|
* @return \AppUtils\XMLHelper_DOMErrors_Error[] |
115
|
|
|
*/ |
116
|
|
|
public function getByLevel(int $level) |
117
|
|
|
{ |
118
|
|
|
$result = array(); |
119
|
|
|
|
120
|
|
|
foreach($this->errors as $error) |
121
|
|
|
{ |
122
|
|
|
if($error->isLevel($level)) |
123
|
|
|
{ |
124
|
|
|
$result[] = $error; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $result; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Retrieves all errors by the specified libxml error code. |
133
|
|
|
* |
134
|
|
|
* @param int $code |
135
|
|
|
* @return \AppUtils\XMLHelper_DOMErrors_Error[] |
136
|
|
|
*/ |
137
|
|
|
public function getByCode(int $code) |
138
|
|
|
{ |
139
|
|
|
$result = array(); |
140
|
|
|
|
141
|
|
|
foreach($this->errors as $error) |
142
|
|
|
{ |
143
|
|
|
if($error->isCode($code)) |
144
|
|
|
{ |
145
|
|
|
$result[] = $error; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $result; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Checks whether there are errors matching the libxml error level. |
154
|
|
|
* |
155
|
|
|
* @param int $level |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
public function hasErrorsByLevel(int $level) : bool |
159
|
|
|
{ |
160
|
|
|
foreach($this->errors as $error) |
161
|
|
|
{ |
162
|
|
|
if($error->isLevel($level)) |
163
|
|
|
{ |
164
|
|
|
return true; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Checks whether there are any errors matching the libxml error code. |
173
|
|
|
* |
174
|
|
|
* @param int $code |
175
|
|
|
* @return bool |
176
|
|
|
*/ |
177
|
|
|
public function hasErrorsByCode(int $code) : bool |
178
|
|
|
{ |
179
|
|
|
foreach($this->errors as $error) |
180
|
|
|
{ |
181
|
|
|
if($error->isCode($code)) |
182
|
|
|
{ |
183
|
|
|
return true; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
return false; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function toArray() : array |
191
|
|
|
{ |
192
|
|
|
$result = array(); |
193
|
|
|
|
194
|
|
|
foreach($this->errors as $error) |
195
|
|
|
{ |
196
|
|
|
$result[] = $error->toArray(); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Serializes the errors collection, so it can be stored and |
204
|
|
|
* restored as needed, using the `fromSerialized()` method. |
205
|
|
|
* |
206
|
|
|
* @return string |
207
|
|
|
* @see XMLHelper_DOMErrors::fromSerialized() |
208
|
|
|
*/ |
209
|
|
|
public function serialize() : string |
210
|
|
|
{ |
211
|
|
|
$data = array(); |
212
|
|
|
|
213
|
|
|
foreach($this->errors as $error) |
214
|
|
|
{ |
215
|
|
|
$data[] = $error->serialize(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return implode(self::SERIALIZE_SEPARATOR, $data); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Restores the errors collection from a previously serialized |
223
|
|
|
* collection, using `serialize()`. |
224
|
|
|
* |
225
|
|
|
* @param string $serialized |
226
|
|
|
* @return XMLHelper_DOMErrors |
227
|
|
|
* @see XMLHelper_DOMErrors::serialize() |
228
|
|
|
*/ |
229
|
|
|
public static function fromSerialized(string $serialized) : XMLHelper_DOMErrors |
230
|
|
|
{ |
231
|
|
|
$parts = explode(self::SERIALIZE_SEPARATOR, $serialized); |
232
|
|
|
$list = array(); |
233
|
|
|
|
234
|
|
|
foreach($parts as $part) |
235
|
|
|
{ |
236
|
|
|
$list[] = XMLHelper_DOMErrors_Error::fromSerialized($part); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return new XMLHelper_DOMErrors($list); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|