Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
27 / 27 |
AbstractError | |
100.00% |
1 / 1 |
|
100.00% |
12 / 12 |
16 | |
100.00% |
27 / 27 |
getErrorCodes | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
setErrorCode | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
setErrorCodes | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
addErrorCode | |
100.00% |
1 / 1 |
2 | |
100.00% |
4 / 4 |
|||
addErrorCodes | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
|||
hasErrorCodes | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
hasError | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
isError | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getErrorMessages | |
100.00% |
1 / 1 |
1 | |
100.00% |
4 / 4 |
|||
getAllErrorMessages | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getAllErrorCodes | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getErrorMessage | |
100.00% |
1 / 1 |
2 | |
100.00% |
3 / 3 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2021 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Utils; |
15 | |
16 | /** |
17 | * Pop utils abstract error class |
18 | * |
19 | * @category Pop |
20 | * @package Pop\Utils |
21 | * @author Nick Sagona, III <dev@nolainteractive.com> |
22 | * @copyright Copyright (c) 2009-2021 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
23 | * @license http://www.popphp.org/license New BSD License |
24 | * @version 1.2.0 |
25 | */ |
26 | abstract class AbstractError implements ErrorInterface |
27 | { |
28 | |
29 | /** |
30 | * Current error codes |
31 | * @var mixed |
32 | */ |
33 | protected $errorCodes = []; |
34 | |
35 | /** |
36 | * Error messages |
37 | * @var array |
38 | */ |
39 | protected $errorMessages = []; |
40 | |
41 | /** |
42 | * Get current error codes |
43 | * |
44 | * @return array |
45 | */ |
46 | public function getErrorCodes() |
47 | { |
48 | return $this->errorCodes; |
49 | } |
50 | |
51 | /** |
52 | * Set error code (clear all previous errors) |
53 | * |
54 | * @param mixed $errorCode |
55 | * @return AbstractError |
56 | * @throws Exception |
57 | */ |
58 | public function setErrorCode($errorCode) |
59 | { |
60 | if (!array_key_exists($errorCode, $this->errorMessages)) { |
61 | throw new Exception('Error: That error code is not allowed.'); |
62 | } |
63 | $this->errorCodes = [$errorCode]; |
64 | return $this; |
65 | } |
66 | |
67 | /** |
68 | * Set error codes (clear all previous errors) |
69 | * |
70 | * @param array $errorCodes |
71 | * @return AbstractError |
72 | */ |
73 | public function setErrorCodes(array $errorCodes) |
74 | { |
75 | $this->errorCodes = []; |
76 | $this->addErrorCodes($errorCodes); |
77 | return $this; |
78 | } |
79 | |
80 | /** |
81 | * Add error code |
82 | * |
83 | * @param mixed $errorCode |
84 | * @return AbstractError |
85 | * @throws Exception |
86 | */ |
87 | public function addErrorCode($errorCode) |
88 | { |
89 | if (!array_key_exists($errorCode, $this->errorMessages)) { |
90 | throw new Exception('Error: That error code is not allowed.'); |
91 | } |
92 | $this->errorCodes[] = $errorCode; |
93 | return $this; |
94 | } |
95 | |
96 | /** |
97 | * Add error codes |
98 | * |
99 | * @param array $errorCodes |
100 | * @return AbstractError |
101 | */ |
102 | public function addErrorCodes(array $errorCodes) |
103 | { |
104 | foreach ($errorCodes as $errorCode) { |
105 | $this->addErrorCode($errorCode); |
106 | } |
107 | |
108 | return $this; |
109 | } |
110 | |
111 | /** |
112 | * Has error codes |
113 | * |
114 | * @return boolean |
115 | */ |
116 | public function hasErrorCodes() |
117 | { |
118 | return !empty($this->errorCodes); |
119 | } |
120 | |
121 | /** |
122 | * Has error (alias) |
123 | * |
124 | * @return boolean |
125 | */ |
126 | public function hasError() |
127 | { |
128 | return $this->hasErrorCodes(); |
129 | } |
130 | |
131 | /** |
132 | * Is error (alias) |
133 | * |
134 | * @return boolean |
135 | */ |
136 | public function isError() |
137 | { |
138 | return $this->hasErrorCodes(); |
139 | } |
140 | |
141 | /** |
142 | * Get current error messages |
143 | * |
144 | * @return array |
145 | */ |
146 | public function getErrorMessages() |
147 | { |
148 | $errorCodes = $this->errorCodes; |
149 | |
150 | return array_filter($this->errorMessages, function ($k) use ($errorCodes) { |
151 | return in_array($k, $errorCodes); |
152 | }, ARRAY_FILTER_USE_KEY); |
153 | } |
154 | |
155 | /** |
156 | * Get all error messages |
157 | * |
158 | * @return array |
159 | */ |
160 | public function getAllErrorMessages() |
161 | { |
162 | return $this->errorMessages; |
163 | } |
164 | |
165 | /** |
166 | * Get all error codes |
167 | * |
168 | * @return array |
169 | */ |
170 | public function getAllErrorCodes() |
171 | { |
172 | return array_keys($this->errorMessages); |
173 | } |
174 | |
175 | /** |
176 | * Get error message from a provided error code |
177 | * |
178 | * @param mixed $errorCode |
179 | * @throws Exception |
180 | * @return string |
181 | */ |
182 | public function getErrorMessage($errorCode) |
183 | { |
184 | if (!array_key_exists($errorCode, $this->errorMessages)) { |
185 | throw new Exception('Error: That error code is not allowed.'); |
186 | } |
187 | return $this->errorMessages[$errorCode]; |
188 | } |
189 | |
190 | } |