1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see AppUtils\XMLHelper_DOMErrors_Error} class. |
4
|
|
|
* |
5
|
|
|
* @package Application Utils |
6
|
|
|
* @subpackage XMLHelper |
7
|
|
|
* @see XMLHelper_DOMErrors_Error |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AppUtils; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* LibXML error wrapper: offers easy access to an error's |
16
|
|
|
* iformation. |
17
|
|
|
* |
18
|
|
|
* @package Application Utils |
19
|
|
|
* @subpackage XMLHelper |
20
|
|
|
* @author Sebastian Mordziol <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class XMLHelper_DOMErrors_Error |
23
|
|
|
{ |
24
|
|
|
const ERROR_CANNOT_UNSERIALIZE_ERROR_DATA = 57201; |
25
|
|
|
const ERROR_ERROR_DATA_KEY_MISSING = 57202; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \LibXMLError |
29
|
|
|
*/ |
30
|
|
|
private $error; |
31
|
|
|
|
32
|
|
|
private static $requiredKeys = array( |
33
|
|
|
'code', |
34
|
|
|
'column', |
35
|
|
|
'level', |
36
|
|
|
'message', |
37
|
|
|
'line' |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
public function __construct(\LibXMLError $error) |
41
|
|
|
{ |
42
|
|
|
$this->error = $error; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public static function fromSerialized(string $serialized) : XMLHelper_DOMErrors_Error |
46
|
|
|
{ |
47
|
|
|
$data = @json_decode($serialized, true); |
48
|
|
|
|
49
|
|
|
if(!is_array($data)) |
50
|
|
|
{ |
51
|
|
|
throw new XMLHelper_Exception( |
52
|
|
|
'Could not unserialize error data', |
53
|
|
|
sprintf( |
54
|
|
|
'The specified serialized error data could not be desrialized. Source string: [%s]', |
55
|
|
|
$serialized |
56
|
|
|
), |
57
|
|
|
self::ERROR_CANNOT_UNSERIALIZE_ERROR_DATA |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
self::checkErrorData($data); |
62
|
|
|
|
63
|
|
|
$error = new \LibXMLError(); |
64
|
|
|
$error->code = (int)$data['code']; |
65
|
|
|
$error->column = (int)$data['column']; |
66
|
|
|
$error->level = (int)$data['level']; |
67
|
|
|
$error->message = (string)$data['message']; |
68
|
|
|
$error->line = (int)$data['line']; |
69
|
|
|
|
70
|
|
|
return new XMLHelper_DOMErrors_Error($error); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private static function checkErrorData(array $data) : void |
74
|
|
|
{ |
75
|
|
|
foreach(self::$requiredKeys as $key) |
76
|
|
|
{ |
77
|
|
|
if(!array_key_exists($key, $data)) |
78
|
|
|
{ |
79
|
|
|
throw new XMLHelper_Exception( |
80
|
|
|
'Required key missing in error data', |
81
|
|
|
sprintf( |
82
|
|
|
'The key [%s] is not present in the error data. Existing keys are [%s].', |
83
|
|
|
$key, |
84
|
|
|
implode(', ', array_keys($data)) |
85
|
|
|
), |
86
|
|
|
self::ERROR_ERROR_DATA_KEY_MISSING |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getLevel() : int |
93
|
|
|
{ |
94
|
|
|
return (int)$this->error->level; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getCode() : int |
98
|
|
|
{ |
99
|
|
|
return (int)$this->error->code; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getMessage() : string |
103
|
|
|
{ |
104
|
|
|
return (string)$this->error->message; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getLine() : int |
108
|
|
|
{ |
109
|
|
|
return (int)$this->error->line; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getColumn() : int |
113
|
|
|
{ |
114
|
|
|
return (int)$this->error->column; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function isWarning() : bool |
118
|
|
|
{ |
119
|
|
|
return $this->isLevel(LIBXML_ERR_WARNING); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function isError() : bool |
123
|
|
|
{ |
124
|
|
|
return $this->isLevel(LIBXML_ERR_ERROR); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function isFatal() : bool |
128
|
|
|
{ |
129
|
|
|
return $this->isLevel(LIBXML_ERR_FATAL); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function isLevel(int $level) : bool |
133
|
|
|
{ |
134
|
|
|
return $this->getLevel() === $level; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function isTypeTagMismatch() : bool |
138
|
|
|
{ |
139
|
|
|
return $this->isCode(XMLHelper_LibXML::TAG_NAME_MISMATCH); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function isTypeUnknownTag() : bool |
143
|
|
|
{ |
144
|
|
|
return $this->isCode(XMLHelper_LibXML::XML_HTML_UNKNOWN_TAG); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function isCode(int $code) : bool |
148
|
|
|
{ |
149
|
|
|
return $this->getCode() === $code; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function serialize() : string |
153
|
|
|
{ |
154
|
|
|
return json_encode($this->toArray()); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
public function toArray() : array |
158
|
|
|
{ |
159
|
|
|
return array( |
160
|
|
|
'code' => $this->getCode(), |
161
|
|
|
'column' => $this->getColumn(), |
162
|
|
|
'level' => $this->getLevel(), |
163
|
|
|
'message' => $this->getMessage(), |
164
|
|
|
'line' => $this->getLine() |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|