1 | <?php |
||
23 | final class Errors implements IteratorAggregate, Countable, ArrayAccess |
||
24 | { |
||
25 | /** |
||
26 | * @var TranslatorInterface|null |
||
27 | */ |
||
28 | private static $translator; |
||
29 | |||
30 | /** |
||
31 | * @var Error[] |
||
32 | */ |
||
33 | private $stack = []; |
||
34 | |||
35 | /** |
||
36 | * Sets the global translator instance. |
||
37 | */ |
||
38 | public static function setTranslator(TranslatorInterface $translator) |
||
42 | |||
43 | /** |
||
44 | * Clears the global translator instance. |
||
45 | */ |
||
46 | public static function clearTranslator(): void |
||
50 | |||
51 | /** |
||
52 | * Gets the translator. |
||
53 | */ |
||
54 | public function getTranslator(): TranslatorInterface |
||
62 | |||
63 | /** |
||
64 | * Adds an error message to the stack. |
||
65 | * |
||
66 | * @param $error |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function add(string $error, array $context = []) |
||
77 | |||
78 | /** |
||
79 | * Gets all of the error messages on the stack. |
||
80 | * |
||
81 | * @return string[] |
||
82 | */ |
||
83 | public function all(): array |
||
92 | |||
93 | /** |
||
94 | * Gets an error for a specific parameter on the stack. |
||
95 | * |
||
96 | * @param string $value value we are searching for |
||
97 | * @param string $param parameter name |
||
98 | */ |
||
99 | public function find($value, $param = 'field'): ?Error |
||
110 | |||
111 | /** |
||
112 | * Checks if an error exists with a specific parameter on the stack. |
||
113 | * |
||
114 | * @param string $value value we are searching for |
||
115 | * @param string $param parameter name |
||
116 | */ |
||
117 | public function has($value, $param = 'field'): bool |
||
130 | |||
131 | /** |
||
132 | * Clears the error stack. |
||
133 | * |
||
134 | * @return $this |
||
135 | */ |
||
136 | public function clear() |
||
142 | |||
143 | public function __toString() |
||
147 | |||
148 | ////////////////////////// |
||
149 | // Helpers |
||
150 | ////////////////////////// |
||
151 | |||
152 | /** |
||
153 | * Formats an incoming error message. |
||
154 | * |
||
155 | * @param array|string $input |
||
156 | */ |
||
157 | private function sanitize($input): Error |
||
165 | |||
166 | /** |
||
167 | * Parses an error message before displaying it. |
||
168 | */ |
||
169 | private function parse(string $error, array $context): string |
||
173 | |||
174 | ////////////////////////// |
||
175 | // IteratorAggregate Interface |
||
176 | ////////////////////////// |
||
177 | |||
178 | public function getIterator() |
||
182 | |||
183 | ////////////////////////// |
||
184 | // Countable Interface |
||
185 | ////////////////////////// |
||
186 | |||
187 | /** |
||
188 | * Get total number of models matching query. |
||
189 | * |
||
190 | * @return int |
||
191 | */ |
||
192 | public function count() |
||
196 | |||
197 | ///////////////////////////// |
||
198 | // ArrayAccess Interface |
||
199 | ///////////////////////////// |
||
200 | |||
201 | public function offsetExists($offset) |
||
205 | |||
206 | public function offsetGet($offset) |
||
214 | |||
215 | public function offsetSet($offset, $error) |
||
223 | |||
224 | public function offsetUnset($offset) |
||
228 | } |
||
229 |