@@ -18,76 +18,76 @@ |
||
18 | 18 | |
19 | 19 | final class ErrorCollectionIteratorTest extends TestCase |
20 | 20 | { |
21 | - public function test_renderer(): void |
|
22 | - { |
|
23 | - $format = "error: {arg}"; |
|
24 | - $arg1 = uniqid(); |
|
25 | - $arg2 = uniqid(); |
|
26 | - $arg3 = uniqid(); |
|
27 | - $arg4 = uniqid(); |
|
28 | - $attribute = uniqid(); |
|
29 | - |
|
30 | - $errors = (new ErrorCollection()) |
|
31 | - ->add($attribute, $format, [ 'arg' => $arg3 ]) |
|
32 | - ->add_generic($format, [ 'arg' => $arg1 ]) |
|
33 | - ->add($attribute, $format, [ 'arg' => $arg4 ]) |
|
34 | - ->add_generic($format, [ 'arg' => $arg2 ]); |
|
35 | - |
|
36 | - $renderer = new ErrorCollectionIterator($errors); |
|
37 | - $rendered = []; |
|
38 | - |
|
39 | - foreach ($renderer as $a => $r) { |
|
40 | - $rendered[] = [ $a, $r ]; |
|
41 | - } |
|
42 | - |
|
43 | - $this->assertSame([ |
|
44 | - |
|
45 | - [ ErrorCollection::GENERIC, "error: $arg1" ], |
|
46 | - [ ErrorCollection::GENERIC, "error: $arg2" ], |
|
47 | - [ $attribute, "error: $arg3" ], |
|
48 | - [ $attribute, "error: $arg4" ], |
|
49 | - |
|
50 | - ], $rendered); |
|
51 | - } |
|
52 | - |
|
53 | - public function test_render_with_customer_error_renderer(): void |
|
54 | - { |
|
55 | - $format = "error: {arg}"; |
|
56 | - $arg1 = uniqid(); |
|
57 | - $arg2 = uniqid(); |
|
58 | - $arg3 = uniqid(); |
|
59 | - $arg4 = uniqid(); |
|
60 | - $attribute = uniqid(); |
|
61 | - |
|
62 | - $errors = (new ErrorCollection()) |
|
63 | - ->add($attribute, $format, [ 'arg' => $arg3 ]) |
|
64 | - ->add_generic($format, [ 'arg' => $arg1 ]) |
|
65 | - ->add($attribute, $format, [ 'arg' => $arg4 ]) |
|
66 | - ->add_generic($format, [ 'arg' => $arg2 ]); |
|
67 | - |
|
68 | - $renderer = new ErrorCollectionIterator($errors, function ( |
|
69 | - Error $error, |
|
70 | - $attribute, |
|
71 | - ErrorCollection $collection |
|
72 | - ) use ($errors) { |
|
73 | - $this->assertSame($errors, $collection); |
|
74 | - |
|
75 | - return strrev($error); |
|
76 | - }); |
|
77 | - |
|
78 | - $rendered = []; |
|
79 | - |
|
80 | - foreach ($renderer as $a => $r) { |
|
81 | - $rendered[] = [ $a, $r ]; |
|
82 | - } |
|
83 | - |
|
84 | - $this->assertSame([ |
|
85 | - |
|
86 | - [ ErrorCollection::GENERIC, strrev("error: $arg1") ], |
|
87 | - [ ErrorCollection::GENERIC, strrev("error: $arg2") ], |
|
88 | - [ $attribute, strrev("error: $arg3") ], |
|
89 | - [ $attribute, strrev("error: $arg4") ], |
|
90 | - |
|
91 | - ], $rendered); |
|
92 | - } |
|
21 | + public function test_renderer(): void |
|
22 | + { |
|
23 | + $format = "error: {arg}"; |
|
24 | + $arg1 = uniqid(); |
|
25 | + $arg2 = uniqid(); |
|
26 | + $arg3 = uniqid(); |
|
27 | + $arg4 = uniqid(); |
|
28 | + $attribute = uniqid(); |
|
29 | + |
|
30 | + $errors = (new ErrorCollection()) |
|
31 | + ->add($attribute, $format, [ 'arg' => $arg3 ]) |
|
32 | + ->add_generic($format, [ 'arg' => $arg1 ]) |
|
33 | + ->add($attribute, $format, [ 'arg' => $arg4 ]) |
|
34 | + ->add_generic($format, [ 'arg' => $arg2 ]); |
|
35 | + |
|
36 | + $renderer = new ErrorCollectionIterator($errors); |
|
37 | + $rendered = []; |
|
38 | + |
|
39 | + foreach ($renderer as $a => $r) { |
|
40 | + $rendered[] = [ $a, $r ]; |
|
41 | + } |
|
42 | + |
|
43 | + $this->assertSame([ |
|
44 | + |
|
45 | + [ ErrorCollection::GENERIC, "error: $arg1" ], |
|
46 | + [ ErrorCollection::GENERIC, "error: $arg2" ], |
|
47 | + [ $attribute, "error: $arg3" ], |
|
48 | + [ $attribute, "error: $arg4" ], |
|
49 | + |
|
50 | + ], $rendered); |
|
51 | + } |
|
52 | + |
|
53 | + public function test_render_with_customer_error_renderer(): void |
|
54 | + { |
|
55 | + $format = "error: {arg}"; |
|
56 | + $arg1 = uniqid(); |
|
57 | + $arg2 = uniqid(); |
|
58 | + $arg3 = uniqid(); |
|
59 | + $arg4 = uniqid(); |
|
60 | + $attribute = uniqid(); |
|
61 | + |
|
62 | + $errors = (new ErrorCollection()) |
|
63 | + ->add($attribute, $format, [ 'arg' => $arg3 ]) |
|
64 | + ->add_generic($format, [ 'arg' => $arg1 ]) |
|
65 | + ->add($attribute, $format, [ 'arg' => $arg4 ]) |
|
66 | + ->add_generic($format, [ 'arg' => $arg2 ]); |
|
67 | + |
|
68 | + $renderer = new ErrorCollectionIterator($errors, function ( |
|
69 | + Error $error, |
|
70 | + $attribute, |
|
71 | + ErrorCollection $collection |
|
72 | + ) use ($errors) { |
|
73 | + $this->assertSame($errors, $collection); |
|
74 | + |
|
75 | + return strrev($error); |
|
76 | + }); |
|
77 | + |
|
78 | + $rendered = []; |
|
79 | + |
|
80 | + foreach ($renderer as $a => $r) { |
|
81 | + $rendered[] = [ $a, $r ]; |
|
82 | + } |
|
83 | + |
|
84 | + $this->assertSame([ |
|
85 | + |
|
86 | + [ ErrorCollection::GENERIC, strrev("error: $arg1") ], |
|
87 | + [ ErrorCollection::GENERIC, strrev("error: $arg2") ], |
|
88 | + [ $attribute, strrev("error: $arg3") ], |
|
89 | + [ $attribute, strrev("error: $arg4") ], |
|
90 | + |
|
91 | + ], $rendered); |
|
92 | + } |
|
93 | 93 | } |
@@ -28,24 +28,24 @@ discard block |
||
28 | 28 | $attribute = uniqid(); |
29 | 29 | |
30 | 30 | $errors = (new ErrorCollection()) |
31 | - ->add($attribute, $format, [ 'arg' => $arg3 ]) |
|
32 | - ->add_generic($format, [ 'arg' => $arg1 ]) |
|
33 | - ->add($attribute, $format, [ 'arg' => $arg4 ]) |
|
34 | - ->add_generic($format, [ 'arg' => $arg2 ]); |
|
31 | + ->add($attribute, $format, ['arg' => $arg3]) |
|
32 | + ->add_generic($format, ['arg' => $arg1]) |
|
33 | + ->add($attribute, $format, ['arg' => $arg4]) |
|
34 | + ->add_generic($format, ['arg' => $arg2]); |
|
35 | 35 | |
36 | 36 | $renderer = new ErrorCollectionIterator($errors); |
37 | 37 | $rendered = []; |
38 | 38 | |
39 | 39 | foreach ($renderer as $a => $r) { |
40 | - $rendered[] = [ $a, $r ]; |
|
40 | + $rendered[] = [$a, $r]; |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | $this->assertSame([ |
44 | 44 | |
45 | - [ ErrorCollection::GENERIC, "error: $arg1" ], |
|
46 | - [ ErrorCollection::GENERIC, "error: $arg2" ], |
|
47 | - [ $attribute, "error: $arg3" ], |
|
48 | - [ $attribute, "error: $arg4" ], |
|
45 | + [ErrorCollection::GENERIC, "error: $arg1"], |
|
46 | + [ErrorCollection::GENERIC, "error: $arg2"], |
|
47 | + [$attribute, "error: $arg3"], |
|
48 | + [$attribute, "error: $arg4"], |
|
49 | 49 | |
50 | 50 | ], $rendered); |
51 | 51 | } |
@@ -60,12 +60,12 @@ discard block |
||
60 | 60 | $attribute = uniqid(); |
61 | 61 | |
62 | 62 | $errors = (new ErrorCollection()) |
63 | - ->add($attribute, $format, [ 'arg' => $arg3 ]) |
|
64 | - ->add_generic($format, [ 'arg' => $arg1 ]) |
|
65 | - ->add($attribute, $format, [ 'arg' => $arg4 ]) |
|
66 | - ->add_generic($format, [ 'arg' => $arg2 ]); |
|
63 | + ->add($attribute, $format, ['arg' => $arg3]) |
|
64 | + ->add_generic($format, ['arg' => $arg1]) |
|
65 | + ->add($attribute, $format, ['arg' => $arg4]) |
|
66 | + ->add_generic($format, ['arg' => $arg2]); |
|
67 | 67 | |
68 | - $renderer = new ErrorCollectionIterator($errors, function ( |
|
68 | + $renderer = new ErrorCollectionIterator($errors, function( |
|
69 | 69 | Error $error, |
70 | 70 | $attribute, |
71 | 71 | ErrorCollection $collection |
@@ -78,15 +78,15 @@ discard block |
||
78 | 78 | $rendered = []; |
79 | 79 | |
80 | 80 | foreach ($renderer as $a => $r) { |
81 | - $rendered[] = [ $a, $r ]; |
|
81 | + $rendered[] = [$a, $r]; |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | $this->assertSame([ |
85 | 85 | |
86 | - [ ErrorCollection::GENERIC, strrev("error: $arg1") ], |
|
87 | - [ ErrorCollection::GENERIC, strrev("error: $arg2") ], |
|
88 | - [ $attribute, strrev("error: $arg3") ], |
|
89 | - [ $attribute, strrev("error: $arg4") ], |
|
86 | + [ErrorCollection::GENERIC, strrev("error: $arg1")], |
|
87 | + [ErrorCollection::GENERIC, strrev("error: $arg2")], |
|
88 | + [$attribute, strrev("error: $arg3")], |
|
89 | + [$attribute, strrev("error: $arg4")], |
|
90 | 90 | |
91 | 91 | ], $rendered); |
92 | 92 | } |
@@ -21,240 +21,240 @@ |
||
21 | 21 | |
22 | 22 | final class ErrorCollectionTest extends TestCase |
23 | 23 | { |
24 | - private ErrorCollection $errors; |
|
25 | - |
|
26 | - protected function setUp(): void |
|
27 | - { |
|
28 | - $this->errors = new ErrorCollection(); |
|
29 | - } |
|
30 | - |
|
31 | - public function test_add_with_string(): void |
|
32 | - { |
|
33 | - $attribute = uniqid(); |
|
34 | - $format = uniqid(); |
|
35 | - $args = [ uniqid() => uniqid() ]; |
|
36 | - $this->errors->add($attribute, $format, $args); |
|
37 | - $this->assertEquals(1, $this->errors->count()); |
|
38 | - |
|
39 | - $errors = $this->errors[$attribute]; |
|
40 | - $this->assertIsArray($errors); |
|
41 | - $error = reset($errors); |
|
42 | - |
|
43 | - assert($error instanceof Error); |
|
44 | - |
|
45 | - $this->assertSame($format, $error->format); |
|
46 | - $this->assertSame($args, $error->args); |
|
47 | - } |
|
48 | - |
|
49 | - public function test_add_with_true(): void |
|
50 | - { |
|
51 | - $attribute = uniqid(); |
|
52 | - $args = [ uniqid() => uniqid() ]; |
|
53 | - $this->errors->add($attribute, true, $args); |
|
54 | - $this->assertEquals(1, $this->errors->count()); |
|
55 | - |
|
56 | - $errors = $this->errors[$attribute]; |
|
57 | - $this->assertIsArray($errors); |
|
58 | - $error = reset($errors); |
|
59 | - |
|
60 | - assert($error instanceof Error); |
|
61 | - |
|
62 | - $this->assertSame("", $error->format); |
|
63 | - $this->assertSame($args, $error->args); |
|
64 | - } |
|
65 | - |
|
66 | - public function test_add_with_error(): void |
|
67 | - { |
|
68 | - $error = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
69 | - $attribute = uniqid(); |
|
70 | - $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
71 | - $this->assertEquals(1, $this->errors->count()); |
|
72 | - |
|
73 | - $errors = $this->errors[$attribute]; |
|
74 | - $this->assertIsArray($errors); |
|
75 | - $this->assertSame($error, reset($errors)); |
|
76 | - } |
|
77 | - |
|
78 | - public function test_add_with_exception(): void |
|
79 | - { |
|
80 | - $error = new Exception(); |
|
81 | - $attribute = uniqid(); |
|
82 | - $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
83 | - $this->assertEquals(1, $this->errors->count()); |
|
84 | - |
|
85 | - $errors = $this->errors[$attribute]; |
|
86 | - $this->assertIsArray($errors); |
|
87 | - $first = reset($errors); |
|
88 | - assert($first instanceof Error); |
|
89 | - $this->assertSame((string) $error, $first->format); |
|
90 | - } |
|
91 | - |
|
92 | - public function test_add_with_throwable(): void |
|
93 | - { |
|
94 | - $error = new \Error(); |
|
95 | - $attribute = uniqid(); |
|
96 | - $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
97 | - $this->assertEquals(1, $this->errors->count()); |
|
98 | - |
|
99 | - $errors = $this->errors[$attribute]; |
|
100 | - $this->assertIsArray($errors); |
|
101 | - $first = reset($errors); |
|
102 | - assert($first instanceof Error); |
|
103 | - $this->assertSame((string) $error, $first->format); |
|
104 | - } |
|
105 | - |
|
106 | - public function test_add_generic(): void |
|
107 | - { |
|
108 | - $error = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
109 | - $this->errors->add_generic($error); |
|
110 | - $errors = $this->errors[null]; |
|
111 | - $this->assertSame($error, reset($errors)); |
|
112 | - } |
|
113 | - |
|
114 | - public function test_array_access_interface(): void |
|
115 | - { |
|
116 | - $errors = $this->errors; |
|
117 | - $err1 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
118 | - $err2 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
119 | - $err3 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
120 | - $err4 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
121 | - $attribute = uniqid(); |
|
122 | - $errors[] = $err1; |
|
123 | - $errors[] = $err2; |
|
124 | - $errors[$attribute] = $err3; |
|
125 | - $errors[$attribute] = $err4; |
|
126 | - |
|
127 | - $this->assertTrue(isset($errors[null])); |
|
128 | - $this->assertTrue(isset($errors[$attribute])); |
|
129 | - $this->assertSame([ $err1, $err2 ], $errors[null]); |
|
130 | - $this->assertSame([ $err3, $err4 ], $errors[$attribute]); |
|
131 | - |
|
132 | - unset($errors[null]); |
|
133 | - $this->assertSame([], $errors[null]); |
|
134 | - unset($errors[$attribute]); |
|
135 | - $this->assertSame([], $errors[$attribute]); |
|
136 | - } |
|
137 | - |
|
138 | - public function test_iterator(): void |
|
139 | - { |
|
140 | - $errors = $this->errors; |
|
141 | - $err1 = new Error('err1-' . uniqid()); |
|
142 | - $err2 = new Error('err2-' . uniqid()); |
|
143 | - $err3 = new Error('err3-' . uniqid()); |
|
144 | - $err4 = new Error('err4-' . uniqid()); |
|
145 | - $attribute = uniqid(); |
|
146 | - $errors[$attribute] = $err3; |
|
147 | - $errors[] = $err1; |
|
148 | - $errors[$attribute] = $err4; |
|
149 | - $errors[] = $err2; |
|
150 | - |
|
151 | - $iterator_errors = []; |
|
152 | - |
|
153 | - foreach ($errors as $a => $e) { |
|
154 | - $iterator_errors[] = [ $a => $e ]; |
|
155 | - } |
|
156 | - |
|
157 | - $this->assertSame([ |
|
158 | - |
|
159 | - [ ErrorCollection::GENERIC => $err1 ], |
|
160 | - [ ErrorCollection::GENERIC => $err2 ], |
|
161 | - [ $attribute => $err3 ], |
|
162 | - [ $attribute => $err4 ], |
|
163 | - |
|
164 | - ], $iterator_errors); |
|
165 | - } |
|
166 | - |
|
167 | - public function test_each(): void |
|
168 | - { |
|
169 | - $errors = $this->errors; |
|
170 | - $err1 = new Error('err1-' . uniqid()); |
|
171 | - $err2 = new Error('err2-' . uniqid()); |
|
172 | - $err3 = new Error('err3-' . uniqid()); |
|
173 | - $err4 = new Error('err4-' . uniqid()); |
|
174 | - $attribute = uniqid(); |
|
175 | - $errors[$attribute] = $err3; |
|
176 | - $errors[] = $err1; |
|
177 | - $errors[$attribute] = $err4; |
|
178 | - $errors[] = $err2; |
|
179 | - |
|
180 | - $iterator_errors = []; |
|
181 | - |
|
182 | - $errors->each(function ($error, $attribute, $collection) use (&$iterator_errors, $errors) { |
|
183 | - $this->assertSame($errors, $collection); |
|
184 | - $iterator_errors[] = [ $attribute => $error ]; |
|
185 | - }); |
|
186 | - |
|
187 | - $this->assertSame([ |
|
188 | - |
|
189 | - [ ErrorCollection::GENERIC => $err1 ], |
|
190 | - [ ErrorCollection::GENERIC => $err2 ], |
|
191 | - [ $attribute => $err3 ], |
|
192 | - [ $attribute => $err4 ], |
|
193 | - |
|
194 | - ], $iterator_errors); |
|
195 | - } |
|
196 | - |
|
197 | - public function test_clear(): void |
|
198 | - { |
|
199 | - $errors = $this->errors->add(uniqid())->add(uniqid())->add(uniqid()); |
|
200 | - $this->assertEquals(3, $errors->count()); |
|
201 | - $this->assertEquals(0, $errors->clear()->count()); |
|
202 | - } |
|
203 | - |
|
204 | - public function test_merge(): void |
|
205 | - { |
|
206 | - $er1 = new Error(uniqid()); |
|
207 | - $er2 = new Error(uniqid()); |
|
208 | - |
|
209 | - $col1 = (new ErrorCollection()) |
|
210 | - ->add_generic($er1); |
|
211 | - $col2 = (new ErrorCollection()) |
|
212 | - ->add_generic($er2); |
|
213 | - |
|
214 | - $col1->merge($col2); |
|
215 | - |
|
216 | - $this->assertSame([ |
|
217 | - |
|
218 | - ErrorCollection::GENERIC => [ |
|
219 | - |
|
220 | - $er1, |
|
221 | - $er2 |
|
222 | - |
|
223 | - ] |
|
224 | - |
|
225 | - ], $col1->to_array()); |
|
226 | - } |
|
227 | - |
|
228 | - public function test_json_serialize(): void |
|
229 | - { |
|
230 | - $format = "error: {arg}"; |
|
231 | - $arg1 = uniqid(); |
|
232 | - $arg2 = uniqid(); |
|
233 | - $arg3 = uniqid(); |
|
234 | - $arg4 = uniqid(); |
|
235 | - $attribute = uniqid(); |
|
236 | - $errors = $this->errors; |
|
237 | - $errors->add($attribute, $format, [ 'arg' => $arg3 ]); |
|
238 | - $errors->add_generic($format, [ 'arg' => $arg1 ]); |
|
239 | - $errors->add($attribute, $format, [ 'arg' => $arg4 ]); |
|
240 | - $errors->add_generic($format, [ 'arg' => $arg2 ]); |
|
24 | + private ErrorCollection $errors; |
|
25 | + |
|
26 | + protected function setUp(): void |
|
27 | + { |
|
28 | + $this->errors = new ErrorCollection(); |
|
29 | + } |
|
30 | + |
|
31 | + public function test_add_with_string(): void |
|
32 | + { |
|
33 | + $attribute = uniqid(); |
|
34 | + $format = uniqid(); |
|
35 | + $args = [ uniqid() => uniqid() ]; |
|
36 | + $this->errors->add($attribute, $format, $args); |
|
37 | + $this->assertEquals(1, $this->errors->count()); |
|
38 | + |
|
39 | + $errors = $this->errors[$attribute]; |
|
40 | + $this->assertIsArray($errors); |
|
41 | + $error = reset($errors); |
|
42 | + |
|
43 | + assert($error instanceof Error); |
|
44 | + |
|
45 | + $this->assertSame($format, $error->format); |
|
46 | + $this->assertSame($args, $error->args); |
|
47 | + } |
|
48 | + |
|
49 | + public function test_add_with_true(): void |
|
50 | + { |
|
51 | + $attribute = uniqid(); |
|
52 | + $args = [ uniqid() => uniqid() ]; |
|
53 | + $this->errors->add($attribute, true, $args); |
|
54 | + $this->assertEquals(1, $this->errors->count()); |
|
55 | + |
|
56 | + $errors = $this->errors[$attribute]; |
|
57 | + $this->assertIsArray($errors); |
|
58 | + $error = reset($errors); |
|
59 | + |
|
60 | + assert($error instanceof Error); |
|
61 | + |
|
62 | + $this->assertSame("", $error->format); |
|
63 | + $this->assertSame($args, $error->args); |
|
64 | + } |
|
65 | + |
|
66 | + public function test_add_with_error(): void |
|
67 | + { |
|
68 | + $error = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
69 | + $attribute = uniqid(); |
|
70 | + $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
71 | + $this->assertEquals(1, $this->errors->count()); |
|
72 | + |
|
73 | + $errors = $this->errors[$attribute]; |
|
74 | + $this->assertIsArray($errors); |
|
75 | + $this->assertSame($error, reset($errors)); |
|
76 | + } |
|
77 | + |
|
78 | + public function test_add_with_exception(): void |
|
79 | + { |
|
80 | + $error = new Exception(); |
|
81 | + $attribute = uniqid(); |
|
82 | + $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
83 | + $this->assertEquals(1, $this->errors->count()); |
|
84 | + |
|
85 | + $errors = $this->errors[$attribute]; |
|
86 | + $this->assertIsArray($errors); |
|
87 | + $first = reset($errors); |
|
88 | + assert($first instanceof Error); |
|
89 | + $this->assertSame((string) $error, $first->format); |
|
90 | + } |
|
91 | + |
|
92 | + public function test_add_with_throwable(): void |
|
93 | + { |
|
94 | + $error = new \Error(); |
|
95 | + $attribute = uniqid(); |
|
96 | + $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
97 | + $this->assertEquals(1, $this->errors->count()); |
|
98 | + |
|
99 | + $errors = $this->errors[$attribute]; |
|
100 | + $this->assertIsArray($errors); |
|
101 | + $first = reset($errors); |
|
102 | + assert($first instanceof Error); |
|
103 | + $this->assertSame((string) $error, $first->format); |
|
104 | + } |
|
105 | + |
|
106 | + public function test_add_generic(): void |
|
107 | + { |
|
108 | + $error = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
109 | + $this->errors->add_generic($error); |
|
110 | + $errors = $this->errors[null]; |
|
111 | + $this->assertSame($error, reset($errors)); |
|
112 | + } |
|
113 | + |
|
114 | + public function test_array_access_interface(): void |
|
115 | + { |
|
116 | + $errors = $this->errors; |
|
117 | + $err1 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
118 | + $err2 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
119 | + $err3 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
120 | + $err4 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
121 | + $attribute = uniqid(); |
|
122 | + $errors[] = $err1; |
|
123 | + $errors[] = $err2; |
|
124 | + $errors[$attribute] = $err3; |
|
125 | + $errors[$attribute] = $err4; |
|
126 | + |
|
127 | + $this->assertTrue(isset($errors[null])); |
|
128 | + $this->assertTrue(isset($errors[$attribute])); |
|
129 | + $this->assertSame([ $err1, $err2 ], $errors[null]); |
|
130 | + $this->assertSame([ $err3, $err4 ], $errors[$attribute]); |
|
131 | + |
|
132 | + unset($errors[null]); |
|
133 | + $this->assertSame([], $errors[null]); |
|
134 | + unset($errors[$attribute]); |
|
135 | + $this->assertSame([], $errors[$attribute]); |
|
136 | + } |
|
137 | + |
|
138 | + public function test_iterator(): void |
|
139 | + { |
|
140 | + $errors = $this->errors; |
|
141 | + $err1 = new Error('err1-' . uniqid()); |
|
142 | + $err2 = new Error('err2-' . uniqid()); |
|
143 | + $err3 = new Error('err3-' . uniqid()); |
|
144 | + $err4 = new Error('err4-' . uniqid()); |
|
145 | + $attribute = uniqid(); |
|
146 | + $errors[$attribute] = $err3; |
|
147 | + $errors[] = $err1; |
|
148 | + $errors[$attribute] = $err4; |
|
149 | + $errors[] = $err2; |
|
150 | + |
|
151 | + $iterator_errors = []; |
|
152 | + |
|
153 | + foreach ($errors as $a => $e) { |
|
154 | + $iterator_errors[] = [ $a => $e ]; |
|
155 | + } |
|
156 | + |
|
157 | + $this->assertSame([ |
|
158 | + |
|
159 | + [ ErrorCollection::GENERIC => $err1 ], |
|
160 | + [ ErrorCollection::GENERIC => $err2 ], |
|
161 | + [ $attribute => $err3 ], |
|
162 | + [ $attribute => $err4 ], |
|
163 | + |
|
164 | + ], $iterator_errors); |
|
165 | + } |
|
166 | + |
|
167 | + public function test_each(): void |
|
168 | + { |
|
169 | + $errors = $this->errors; |
|
170 | + $err1 = new Error('err1-' . uniqid()); |
|
171 | + $err2 = new Error('err2-' . uniqid()); |
|
172 | + $err3 = new Error('err3-' . uniqid()); |
|
173 | + $err4 = new Error('err4-' . uniqid()); |
|
174 | + $attribute = uniqid(); |
|
175 | + $errors[$attribute] = $err3; |
|
176 | + $errors[] = $err1; |
|
177 | + $errors[$attribute] = $err4; |
|
178 | + $errors[] = $err2; |
|
179 | + |
|
180 | + $iterator_errors = []; |
|
181 | + |
|
182 | + $errors->each(function ($error, $attribute, $collection) use (&$iterator_errors, $errors) { |
|
183 | + $this->assertSame($errors, $collection); |
|
184 | + $iterator_errors[] = [ $attribute => $error ]; |
|
185 | + }); |
|
186 | + |
|
187 | + $this->assertSame([ |
|
188 | + |
|
189 | + [ ErrorCollection::GENERIC => $err1 ], |
|
190 | + [ ErrorCollection::GENERIC => $err2 ], |
|
191 | + [ $attribute => $err3 ], |
|
192 | + [ $attribute => $err4 ], |
|
193 | + |
|
194 | + ], $iterator_errors); |
|
195 | + } |
|
196 | + |
|
197 | + public function test_clear(): void |
|
198 | + { |
|
199 | + $errors = $this->errors->add(uniqid())->add(uniqid())->add(uniqid()); |
|
200 | + $this->assertEquals(3, $errors->count()); |
|
201 | + $this->assertEquals(0, $errors->clear()->count()); |
|
202 | + } |
|
203 | + |
|
204 | + public function test_merge(): void |
|
205 | + { |
|
206 | + $er1 = new Error(uniqid()); |
|
207 | + $er2 = new Error(uniqid()); |
|
208 | + |
|
209 | + $col1 = (new ErrorCollection()) |
|
210 | + ->add_generic($er1); |
|
211 | + $col2 = (new ErrorCollection()) |
|
212 | + ->add_generic($er2); |
|
213 | + |
|
214 | + $col1->merge($col2); |
|
215 | + |
|
216 | + $this->assertSame([ |
|
217 | + |
|
218 | + ErrorCollection::GENERIC => [ |
|
219 | + |
|
220 | + $er1, |
|
221 | + $er2 |
|
222 | + |
|
223 | + ] |
|
224 | + |
|
225 | + ], $col1->to_array()); |
|
226 | + } |
|
227 | + |
|
228 | + public function test_json_serialize(): void |
|
229 | + { |
|
230 | + $format = "error: {arg}"; |
|
231 | + $arg1 = uniqid(); |
|
232 | + $arg2 = uniqid(); |
|
233 | + $arg3 = uniqid(); |
|
234 | + $arg4 = uniqid(); |
|
235 | + $attribute = uniqid(); |
|
236 | + $errors = $this->errors; |
|
237 | + $errors->add($attribute, $format, [ 'arg' => $arg3 ]); |
|
238 | + $errors->add_generic($format, [ 'arg' => $arg1 ]); |
|
239 | + $errors->add($attribute, $format, [ 'arg' => $arg4 ]); |
|
240 | + $errors->add_generic($format, [ 'arg' => $arg2 ]); |
|
241 | 241 | |
242 | - $this->assertSame(json_encode([ |
|
242 | + $this->assertSame(json_encode([ |
|
243 | 243 | |
244 | - ErrorCollection::GENERIC => [ |
|
244 | + ErrorCollection::GENERIC => [ |
|
245 | 245 | |
246 | - "error: $arg1", |
|
247 | - "error: $arg2", |
|
246 | + "error: $arg1", |
|
247 | + "error: $arg2", |
|
248 | 248 | |
249 | - ], |
|
249 | + ], |
|
250 | 250 | |
251 | - $attribute => [ |
|
251 | + $attribute => [ |
|
252 | 252 | |
253 | - "error: $arg3", |
|
254 | - "error: $arg4", |
|
253 | + "error: $arg3", |
|
254 | + "error: $arg4", |
|
255 | 255 | |
256 | - ] |
|
256 | + ] |
|
257 | 257 | |
258 | - ]), json_encode($errors)); |
|
259 | - } |
|
258 | + ]), json_encode($errors)); |
|
259 | + } |
|
260 | 260 | } |
@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | { |
33 | 33 | $attribute = uniqid(); |
34 | 34 | $format = uniqid(); |
35 | - $args = [ uniqid() => uniqid() ]; |
|
35 | + $args = [uniqid() => uniqid()]; |
|
36 | 36 | $this->errors->add($attribute, $format, $args); |
37 | 37 | $this->assertEquals(1, $this->errors->count()); |
38 | 38 | |
@@ -49,7 +49,7 @@ discard block |
||
49 | 49 | public function test_add_with_true(): void |
50 | 50 | { |
51 | 51 | $attribute = uniqid(); |
52 | - $args = [ uniqid() => uniqid() ]; |
|
52 | + $args = [uniqid() => uniqid()]; |
|
53 | 53 | $this->errors->add($attribute, true, $args); |
54 | 54 | $this->assertEquals(1, $this->errors->count()); |
55 | 55 | |
@@ -65,9 +65,9 @@ discard block |
||
65 | 65 | |
66 | 66 | public function test_add_with_error(): void |
67 | 67 | { |
68 | - $error = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
68 | + $error = new Error(uniqid(), [uniqid() => uniqid()]); |
|
69 | 69 | $attribute = uniqid(); |
70 | - $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
70 | + $this->errors->add($attribute, $error, [uniqid() => uniqid()]); |
|
71 | 71 | $this->assertEquals(1, $this->errors->count()); |
72 | 72 | |
73 | 73 | $errors = $this->errors[$attribute]; |
@@ -79,7 +79,7 @@ discard block |
||
79 | 79 | { |
80 | 80 | $error = new Exception(); |
81 | 81 | $attribute = uniqid(); |
82 | - $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
82 | + $this->errors->add($attribute, $error, [uniqid() => uniqid()]); |
|
83 | 83 | $this->assertEquals(1, $this->errors->count()); |
84 | 84 | |
85 | 85 | $errors = $this->errors[$attribute]; |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | { |
94 | 94 | $error = new \Error(); |
95 | 95 | $attribute = uniqid(); |
96 | - $this->errors->add($attribute, $error, [ uniqid() => uniqid() ]); |
|
96 | + $this->errors->add($attribute, $error, [uniqid() => uniqid()]); |
|
97 | 97 | $this->assertEquals(1, $this->errors->count()); |
98 | 98 | |
99 | 99 | $errors = $this->errors[$attribute]; |
@@ -105,7 +105,7 @@ discard block |
||
105 | 105 | |
106 | 106 | public function test_add_generic(): void |
107 | 107 | { |
108 | - $error = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
108 | + $error = new Error(uniqid(), [uniqid() => uniqid()]); |
|
109 | 109 | $this->errors->add_generic($error); |
110 | 110 | $errors = $this->errors[null]; |
111 | 111 | $this->assertSame($error, reset($errors)); |
@@ -114,10 +114,10 @@ discard block |
||
114 | 114 | public function test_array_access_interface(): void |
115 | 115 | { |
116 | 116 | $errors = $this->errors; |
117 | - $err1 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
118 | - $err2 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
119 | - $err3 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
120 | - $err4 = new Error(uniqid(), [ uniqid() => uniqid() ]); |
|
117 | + $err1 = new Error(uniqid(), [uniqid() => uniqid()]); |
|
118 | + $err2 = new Error(uniqid(), [uniqid() => uniqid()]); |
|
119 | + $err3 = new Error(uniqid(), [uniqid() => uniqid()]); |
|
120 | + $err4 = new Error(uniqid(), [uniqid() => uniqid()]); |
|
121 | 121 | $attribute = uniqid(); |
122 | 122 | $errors[] = $err1; |
123 | 123 | $errors[] = $err2; |
@@ -126,8 +126,8 @@ discard block |
||
126 | 126 | |
127 | 127 | $this->assertTrue(isset($errors[null])); |
128 | 128 | $this->assertTrue(isset($errors[$attribute])); |
129 | - $this->assertSame([ $err1, $err2 ], $errors[null]); |
|
130 | - $this->assertSame([ $err3, $err4 ], $errors[$attribute]); |
|
129 | + $this->assertSame([$err1, $err2], $errors[null]); |
|
130 | + $this->assertSame([$err3, $err4], $errors[$attribute]); |
|
131 | 131 | |
132 | 132 | unset($errors[null]); |
133 | 133 | $this->assertSame([], $errors[null]); |
@@ -138,10 +138,10 @@ discard block |
||
138 | 138 | public function test_iterator(): void |
139 | 139 | { |
140 | 140 | $errors = $this->errors; |
141 | - $err1 = new Error('err1-' . uniqid()); |
|
142 | - $err2 = new Error('err2-' . uniqid()); |
|
143 | - $err3 = new Error('err3-' . uniqid()); |
|
144 | - $err4 = new Error('err4-' . uniqid()); |
|
141 | + $err1 = new Error('err1-'.uniqid()); |
|
142 | + $err2 = new Error('err2-'.uniqid()); |
|
143 | + $err3 = new Error('err3-'.uniqid()); |
|
144 | + $err4 = new Error('err4-'.uniqid()); |
|
145 | 145 | $attribute = uniqid(); |
146 | 146 | $errors[$attribute] = $err3; |
147 | 147 | $errors[] = $err1; |
@@ -151,15 +151,15 @@ discard block |
||
151 | 151 | $iterator_errors = []; |
152 | 152 | |
153 | 153 | foreach ($errors as $a => $e) { |
154 | - $iterator_errors[] = [ $a => $e ]; |
|
154 | + $iterator_errors[] = [$a => $e]; |
|
155 | 155 | } |
156 | 156 | |
157 | 157 | $this->assertSame([ |
158 | 158 | |
159 | - [ ErrorCollection::GENERIC => $err1 ], |
|
160 | - [ ErrorCollection::GENERIC => $err2 ], |
|
161 | - [ $attribute => $err3 ], |
|
162 | - [ $attribute => $err4 ], |
|
159 | + [ErrorCollection::GENERIC => $err1], |
|
160 | + [ErrorCollection::GENERIC => $err2], |
|
161 | + [$attribute => $err3], |
|
162 | + [$attribute => $err4], |
|
163 | 163 | |
164 | 164 | ], $iterator_errors); |
165 | 165 | } |
@@ -167,10 +167,10 @@ discard block |
||
167 | 167 | public function test_each(): void |
168 | 168 | { |
169 | 169 | $errors = $this->errors; |
170 | - $err1 = new Error('err1-' . uniqid()); |
|
171 | - $err2 = new Error('err2-' . uniqid()); |
|
172 | - $err3 = new Error('err3-' . uniqid()); |
|
173 | - $err4 = new Error('err4-' . uniqid()); |
|
170 | + $err1 = new Error('err1-'.uniqid()); |
|
171 | + $err2 = new Error('err2-'.uniqid()); |
|
172 | + $err3 = new Error('err3-'.uniqid()); |
|
173 | + $err4 = new Error('err4-'.uniqid()); |
|
174 | 174 | $attribute = uniqid(); |
175 | 175 | $errors[$attribute] = $err3; |
176 | 176 | $errors[] = $err1; |
@@ -179,17 +179,17 @@ discard block |
||
179 | 179 | |
180 | 180 | $iterator_errors = []; |
181 | 181 | |
182 | - $errors->each(function ($error, $attribute, $collection) use (&$iterator_errors, $errors) { |
|
182 | + $errors->each(function($error, $attribute, $collection) use (&$iterator_errors, $errors) { |
|
183 | 183 | $this->assertSame($errors, $collection); |
184 | - $iterator_errors[] = [ $attribute => $error ]; |
|
184 | + $iterator_errors[] = [$attribute => $error]; |
|
185 | 185 | }); |
186 | 186 | |
187 | 187 | $this->assertSame([ |
188 | 188 | |
189 | - [ ErrorCollection::GENERIC => $err1 ], |
|
190 | - [ ErrorCollection::GENERIC => $err2 ], |
|
191 | - [ $attribute => $err3 ], |
|
192 | - [ $attribute => $err4 ], |
|
189 | + [ErrorCollection::GENERIC => $err1], |
|
190 | + [ErrorCollection::GENERIC => $err2], |
|
191 | + [$attribute => $err3], |
|
192 | + [$attribute => $err4], |
|
193 | 193 | |
194 | 194 | ], $iterator_errors); |
195 | 195 | } |
@@ -234,10 +234,10 @@ discard block |
||
234 | 234 | $arg4 = uniqid(); |
235 | 235 | $attribute = uniqid(); |
236 | 236 | $errors = $this->errors; |
237 | - $errors->add($attribute, $format, [ 'arg' => $arg3 ]); |
|
238 | - $errors->add_generic($format, [ 'arg' => $arg1 ]); |
|
239 | - $errors->add($attribute, $format, [ 'arg' => $arg4 ]); |
|
240 | - $errors->add_generic($format, [ 'arg' => $arg2 ]); |
|
237 | + $errors->add($attribute, $format, ['arg' => $arg3]); |
|
238 | + $errors->add_generic($format, ['arg' => $arg1]); |
|
239 | + $errors->add($attribute, $format, ['arg' => $arg4]); |
|
240 | + $errors->add_generic($format, ['arg' => $arg2]); |
|
241 | 241 | |
242 | 242 | $this->assertSame(json_encode([ |
243 | 243 |
@@ -16,13 +16,13 @@ |
||
16 | 16 | |
17 | 17 | final class ErrorTest extends TestCase |
18 | 18 | { |
19 | - public function test_getters(): void |
|
20 | - { |
|
21 | - $format = uniqid(); |
|
22 | - $args = [ uniqid() => uniqid() ]; |
|
23 | - $error = new Error($format, $args); |
|
19 | + public function test_getters(): void |
|
20 | + { |
|
21 | + $format = uniqid(); |
|
22 | + $args = [ uniqid() => uniqid() ]; |
|
23 | + $error = new Error($format, $args); |
|
24 | 24 | |
25 | - $this->assertSame($format, $error->format); |
|
26 | - $this->assertSame($args, $error->args); |
|
27 | - } |
|
25 | + $this->assertSame($format, $error->format); |
|
26 | + $this->assertSame($args, $error->args); |
|
27 | + } |
|
28 | 28 | } |
@@ -19,7 +19,7 @@ |
||
19 | 19 | public function test_getters(): void |
20 | 20 | { |
21 | 21 | $format = uniqid(); |
22 | - $args = [ uniqid() => uniqid() ]; |
|
22 | + $args = [uniqid() => uniqid()]; |
|
23 | 23 | $error = new Error($format, $args); |
24 | 24 | |
25 | 25 | $this->assertSame($format, $error->format); |
@@ -22,37 +22,37 @@ |
||
22 | 22 | class ErrorCollectionIterator implements IteratorAggregate |
23 | 23 | { |
24 | 24 | |
25 | - /** |
|
26 | - * @var ErrorRenderer|callable |
|
27 | - * @phpstan-var ErrorRenderer|(callable(Error,string $attribute,ErrorCollection):string) |
|
28 | - */ |
|
29 | - private $render_error; |
|
30 | - |
|
31 | - /** |
|
32 | - * @phpstan-param ErrorRenderer|(callable(Error,string $attribute,ErrorCollection):string)|null $render_error |
|
33 | - */ |
|
34 | - public function __construct( |
|
35 | - private readonly ErrorCollection $collection, |
|
36 | - ErrorRenderer|callable $render_error = null |
|
37 | - ) { |
|
38 | - $this->render_error = $render_error ?? fn(Error $error) => (string) $error; |
|
39 | - } |
|
40 | - |
|
41 | - /** |
|
42 | - * @return Traversable<string, string> |
|
43 | - */ |
|
44 | - public function getIterator(): Traversable |
|
45 | - { |
|
46 | - foreach ($this->collection as $attribute => $error) { |
|
47 | - yield $attribute => $this->render_error($error, $attribute); |
|
48 | - } |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Renders an error into a string. |
|
53 | - */ |
|
54 | - private function render_error(Error $error, string $attribute): string |
|
55 | - { |
|
56 | - return ($this->render_error)($error, $attribute, $this->collection); |
|
57 | - } |
|
25 | + /** |
|
26 | + * @var ErrorRenderer|callable |
|
27 | + * @phpstan-var ErrorRenderer|(callable(Error,string $attribute,ErrorCollection):string) |
|
28 | + */ |
|
29 | + private $render_error; |
|
30 | + |
|
31 | + /** |
|
32 | + * @phpstan-param ErrorRenderer|(callable(Error,string $attribute,ErrorCollection):string)|null $render_error |
|
33 | + */ |
|
34 | + public function __construct( |
|
35 | + private readonly ErrorCollection $collection, |
|
36 | + ErrorRenderer|callable $render_error = null |
|
37 | + ) { |
|
38 | + $this->render_error = $render_error ?? fn(Error $error) => (string) $error; |
|
39 | + } |
|
40 | + |
|
41 | + /** |
|
42 | + * @return Traversable<string, string> |
|
43 | + */ |
|
44 | + public function getIterator(): Traversable |
|
45 | + { |
|
46 | + foreach ($this->collection as $attribute => $error) { |
|
47 | + yield $attribute => $this->render_error($error, $attribute); |
|
48 | + } |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Renders an error into a string. |
|
53 | + */ |
|
54 | + private function render_error(Error $error, string $attribute): string |
|
55 | + { |
|
56 | + return ($this->render_error)($error, $attribute, $this->collection); |
|
57 | + } |
|
58 | 58 | } |
@@ -33,7 +33,7 @@ |
||
33 | 33 | */ |
34 | 34 | public function __construct( |
35 | 35 | private readonly ErrorCollection $collection, |
36 | - ErrorRenderer|callable $render_error = null |
|
36 | + ErrorRenderer | callable $render_error = null |
|
37 | 37 | ) { |
38 | 38 | $this->render_error = $render_error ?? fn(Error $error) => (string) $error; |
39 | 39 | } |
@@ -29,267 +29,267 @@ |
||
29 | 29 | */ |
30 | 30 | class ErrorCollection implements ArrayAccess, IteratorAggregate, Countable, JsonSerializable, ToArray |
31 | 31 | { |
32 | - /** |
|
33 | - * Special identifier used when an error is not associated with a specific attribute. |
|
34 | - */ |
|
35 | - public const GENERIC = '__generic__'; |
|
32 | + /** |
|
33 | + * Special identifier used when an error is not associated with a specific attribute. |
|
34 | + */ |
|
35 | + public const GENERIC = '__generic__'; |
|
36 | 36 | |
37 | - /** |
|
38 | - * @var array<string, Error[]> |
|
39 | - */ |
|
40 | - private array $collection = []; |
|
37 | + /** |
|
38 | + * @var array<string, Error[]> |
|
39 | + */ |
|
40 | + private array $collection = []; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Add an error associated with an attribute. |
|
44 | - * |
|
45 | - * @param string $attribute Attribute name. |
|
46 | - * @param Throwable|bool|string|Error $error_or_format_or_true A {@link Error} instance or |
|
47 | - * a format to create that instance, or `true`. |
|
48 | - * @param array<int|string, mixed> $args Only used if `$error_or_format_or_true` is not a {@link Error} |
|
49 | - * instance or `true`. |
|
50 | - * |
|
51 | - * @return $this |
|
52 | - */ |
|
53 | - public function add( |
|
54 | - string $attribute, |
|
55 | - Throwable|bool|string|Error $error_or_format_or_true = true, |
|
56 | - array $args = [] |
|
57 | - ): static { |
|
58 | - $this->assert_valid_error($error_or_format_or_true); |
|
42 | + /** |
|
43 | + * Add an error associated with an attribute. |
|
44 | + * |
|
45 | + * @param string $attribute Attribute name. |
|
46 | + * @param Throwable|bool|string|Error $error_or_format_or_true A {@link Error} instance or |
|
47 | + * a format to create that instance, or `true`. |
|
48 | + * @param array<int|string, mixed> $args Only used if `$error_or_format_or_true` is not a {@link Error} |
|
49 | + * instance or `true`. |
|
50 | + * |
|
51 | + * @return $this |
|
52 | + */ |
|
53 | + public function add( |
|
54 | + string $attribute, |
|
55 | + Throwable|bool|string|Error $error_or_format_or_true = true, |
|
56 | + array $args = [] |
|
57 | + ): static { |
|
58 | + $this->assert_valid_error($error_or_format_or_true); |
|
59 | 59 | |
60 | - $this->collection[$attribute][] = $this |
|
61 | - ->ensure_error_instance($error_or_format_or_true, $args); |
|
60 | + $this->collection[$attribute][] = $this |
|
61 | + ->ensure_error_instance($error_or_format_or_true, $args); |
|
62 | 62 | |
63 | - return $this; |
|
64 | - } |
|
63 | + return $this; |
|
64 | + } |
|
65 | 65 | |
66 | - /** |
|
67 | - * Add an error not associated with any attribute. |
|
68 | - * |
|
69 | - * @param Throwable|bool|string|Error $error_or_format_or_true A {@link Error} instance or |
|
70 | - * a format to create that instance, or `true`. |
|
71 | - * @param array<int|string, mixed> $args Only used if `$error_or_format_or_true` is not a {@link Error} |
|
72 | - * instance or `true`. |
|
73 | - * |
|
74 | - * @return $this |
|
75 | - */ |
|
76 | - public function add_generic( |
|
77 | - Throwable|bool|string|Error $error_or_format_or_true = true, |
|
78 | - array $args = [] |
|
79 | - ): static { |
|
80 | - return $this->add(self::GENERIC, $error_or_format_or_true, $args); |
|
81 | - } |
|
66 | + /** |
|
67 | + * Add an error not associated with any attribute. |
|
68 | + * |
|
69 | + * @param Throwable|bool|string|Error $error_or_format_or_true A {@link Error} instance or |
|
70 | + * a format to create that instance, or `true`. |
|
71 | + * @param array<int|string, mixed> $args Only used if `$error_or_format_or_true` is not a {@link Error} |
|
72 | + * instance or `true`. |
|
73 | + * |
|
74 | + * @return $this |
|
75 | + */ |
|
76 | + public function add_generic( |
|
77 | + Throwable|bool|string|Error $error_or_format_or_true = true, |
|
78 | + array $args = [] |
|
79 | + ): static { |
|
80 | + return $this->add(self::GENERIC, $error_or_format_or_true, $args); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * Asserts that the error type is valid. |
|
85 | - * |
|
86 | - * @param mixed $error_or_format_or_true |
|
87 | - */ |
|
88 | - private function assert_valid_error(mixed $error_or_format_or_true): void |
|
89 | - { |
|
90 | - if ( |
|
91 | - $error_or_format_or_true === true |
|
92 | - || is_string($error_or_format_or_true) |
|
93 | - || $error_or_format_or_true instanceof Error |
|
94 | - || $error_or_format_or_true instanceof Throwable |
|
95 | - ) { |
|
96 | - return; |
|
97 | - } |
|
83 | + /** |
|
84 | + * Asserts that the error type is valid. |
|
85 | + * |
|
86 | + * @param mixed $error_or_format_or_true |
|
87 | + */ |
|
88 | + private function assert_valid_error(mixed $error_or_format_or_true): void |
|
89 | + { |
|
90 | + if ( |
|
91 | + $error_or_format_or_true === true |
|
92 | + || is_string($error_or_format_or_true) |
|
93 | + || $error_or_format_or_true instanceof Error |
|
94 | + || $error_or_format_or_true instanceof Throwable |
|
95 | + ) { |
|
96 | + return; |
|
97 | + } |
|
98 | 98 | |
99 | - throw new InvalidArgumentException(sprintf( |
|
100 | - "\$error_or_format_or_true must be a an instance of `%s`, a string, or true. Given: `%s`", |
|
101 | - Error::class, |
|
102 | - get_debug_type($error_or_format_or_true) |
|
103 | - )); |
|
104 | - } |
|
99 | + throw new InvalidArgumentException(sprintf( |
|
100 | + "\$error_or_format_or_true must be a an instance of `%s`, a string, or true. Given: `%s`", |
|
101 | + Error::class, |
|
102 | + get_debug_type($error_or_format_or_true) |
|
103 | + )); |
|
104 | + } |
|
105 | 105 | |
106 | - /** |
|
107 | - * Ensures a {@link Error} instance. |
|
108 | - * |
|
109 | - * @param Throwable|bool|string|Error $error_or_format_or_true |
|
110 | - * @param array<int|string, mixed> $args |
|
111 | - */ |
|
112 | - private function ensure_error_instance( |
|
113 | - Throwable|bool|string|Error $error_or_format_or_true, |
|
114 | - array $args = [] |
|
115 | - ): Error { |
|
116 | - $error = $error_or_format_or_true; |
|
106 | + /** |
|
107 | + * Ensures a {@link Error} instance. |
|
108 | + * |
|
109 | + * @param Throwable|bool|string|Error $error_or_format_or_true |
|
110 | + * @param array<int|string, mixed> $args |
|
111 | + */ |
|
112 | + private function ensure_error_instance( |
|
113 | + Throwable|bool|string|Error $error_or_format_or_true, |
|
114 | + array $args = [] |
|
115 | + ): Error { |
|
116 | + $error = $error_or_format_or_true; |
|
117 | 117 | |
118 | - if (!$error instanceof Error) { |
|
119 | - $error = new Error($error === true ? "" : (string) $error, $args); |
|
120 | - } |
|
118 | + if (!$error instanceof Error) { |
|
119 | + $error = new Error($error === true ? "" : (string) $error, $args); |
|
120 | + } |
|
121 | 121 | |
122 | - return $error; |
|
123 | - } |
|
122 | + return $error; |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * Adds an error. |
|
127 | - * |
|
128 | - * @param string|null $offset An attribute name or `null` for _generic_. |
|
129 | - * @param Throwable|Error|string|bool $value An error. |
|
130 | - * |
|
131 | - * @see add() |
|
132 | - * |
|
133 | - * @phpstan-ignore-next-line |
|
134 | - */ |
|
135 | - public function offsetSet($offset, $value): void |
|
136 | - { |
|
137 | - $this->add($offset ?? self::GENERIC, $value); |
|
138 | - } |
|
125 | + /** |
|
126 | + * Adds an error. |
|
127 | + * |
|
128 | + * @param string|null $offset An attribute name or `null` for _generic_. |
|
129 | + * @param Throwable|Error|string|bool $value An error. |
|
130 | + * |
|
131 | + * @see add() |
|
132 | + * |
|
133 | + * @phpstan-ignore-next-line |
|
134 | + */ |
|
135 | + public function offsetSet($offset, $value): void |
|
136 | + { |
|
137 | + $this->add($offset ?? self::GENERIC, $value); |
|
138 | + } |
|
139 | 139 | |
140 | - /** |
|
141 | - * Clears the errors of an attribute. |
|
142 | - * |
|
143 | - * @param string|null $offset An attribute name or `null` for _generic_. |
|
144 | - */ |
|
145 | - public function offsetUnset($offset): void |
|
146 | - { |
|
147 | - unset($this->collection[$offset ?? self::GENERIC]); |
|
148 | - } |
|
140 | + /** |
|
141 | + * Clears the errors of an attribute. |
|
142 | + * |
|
143 | + * @param string|null $offset An attribute name or `null` for _generic_. |
|
144 | + */ |
|
145 | + public function offsetUnset($offset): void |
|
146 | + { |
|
147 | + unset($this->collection[$offset ?? self::GENERIC]); |
|
148 | + } |
|
149 | 149 | |
150 | - /** |
|
151 | - * Checks if an error is defined for an attribute. |
|
152 | - * |
|
153 | - * ```php |
|
154 | - * <?php |
|
155 | - * |
|
156 | - * use ICanBoogie\ErrorCollection |
|
157 | - * |
|
158 | - * $errors = new ErrorCollection; |
|
159 | - * isset($errors['username']); |
|
160 | - * // false |
|
161 | - * $errors->add('username'); |
|
162 | - * isset($errors['username']); |
|
163 | - * // true |
|
164 | - * ``` |
|
165 | - * |
|
166 | - * @param string|null $offset An attribute name or `null` for _generic_. |
|
167 | - */ |
|
168 | - public function offsetExists($offset): bool |
|
169 | - { |
|
170 | - return isset($this->collection[$offset ?? self::GENERIC]); |
|
171 | - } |
|
150 | + /** |
|
151 | + * Checks if an error is defined for an attribute. |
|
152 | + * |
|
153 | + * ```php |
|
154 | + * <?php |
|
155 | + * |
|
156 | + * use ICanBoogie\ErrorCollection |
|
157 | + * |
|
158 | + * $errors = new ErrorCollection; |
|
159 | + * isset($errors['username']); |
|
160 | + * // false |
|
161 | + * $errors->add('username'); |
|
162 | + * isset($errors['username']); |
|
163 | + * // true |
|
164 | + * ``` |
|
165 | + * |
|
166 | + * @param string|null $offset An attribute name or `null` for _generic_. |
|
167 | + */ |
|
168 | + public function offsetExists($offset): bool |
|
169 | + { |
|
170 | + return isset($this->collection[$offset ?? self::GENERIC]); |
|
171 | + } |
|
172 | 172 | |
173 | - /** |
|
174 | - * Returns errors associated with an attribute. |
|
175 | - * |
|
176 | - * ```php |
|
177 | - * <?php |
|
178 | - * |
|
179 | - * use ICanBoogie\ErrorCollection; |
|
180 | - * |
|
181 | - * $errors = new ErrorCollection; |
|
182 | - * $errors['password'] |
|
183 | - * // [] |
|
184 | - * $errors->add('password') |
|
185 | - * // [ Message ] |
|
186 | - * ``` |
|
187 | - * |
|
188 | - * @param string|null $offset An attribute name or `null` for _generic_. |
|
189 | - * |
|
190 | - * @return Error[] |
|
191 | - */ |
|
192 | - public function offsetGet($offset): array |
|
193 | - { |
|
194 | - if (!$this->offsetExists($offset)) { |
|
195 | - return []; |
|
196 | - } |
|
173 | + /** |
|
174 | + * Returns errors associated with an attribute. |
|
175 | + * |
|
176 | + * ```php |
|
177 | + * <?php |
|
178 | + * |
|
179 | + * use ICanBoogie\ErrorCollection; |
|
180 | + * |
|
181 | + * $errors = new ErrorCollection; |
|
182 | + * $errors['password'] |
|
183 | + * // [] |
|
184 | + * $errors->add('password') |
|
185 | + * // [ Message ] |
|
186 | + * ``` |
|
187 | + * |
|
188 | + * @param string|null $offset An attribute name or `null` for _generic_. |
|
189 | + * |
|
190 | + * @return Error[] |
|
191 | + */ |
|
192 | + public function offsetGet($offset): array |
|
193 | + { |
|
194 | + if (!$this->offsetExists($offset)) { |
|
195 | + return []; |
|
196 | + } |
|
197 | 197 | |
198 | - return $this->collection[$offset ?? self::GENERIC]; |
|
199 | - } |
|
198 | + return $this->collection[$offset ?? self::GENERIC]; |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * Clears errors. |
|
203 | - * |
|
204 | - * @return $this |
|
205 | - */ |
|
206 | - public function clear(): static |
|
207 | - { |
|
208 | - $this->collection = []; |
|
201 | + /** |
|
202 | + * Clears errors. |
|
203 | + * |
|
204 | + * @return $this |
|
205 | + */ |
|
206 | + public function clear(): static |
|
207 | + { |
|
208 | + $this->collection = []; |
|
209 | 209 | |
210 | - return $this; |
|
211 | - } |
|
210 | + return $this; |
|
211 | + } |
|
212 | 212 | |
213 | - /** |
|
214 | - * Merges with another error collection. |
|
215 | - */ |
|
216 | - public function merge(ErrorCollection $collection): void |
|
217 | - { |
|
218 | - foreach ($collection as $attribute => $error) { |
|
219 | - $this->add($attribute, $error); |
|
220 | - } |
|
221 | - } |
|
213 | + /** |
|
214 | + * Merges with another error collection. |
|
215 | + */ |
|
216 | + public function merge(ErrorCollection $collection): void |
|
217 | + { |
|
218 | + foreach ($collection as $attribute => $error) { |
|
219 | + $this->add($attribute, $error); |
|
220 | + } |
|
221 | + } |
|
222 | 222 | |
223 | - /** |
|
224 | - * @return Traversable<string, Error> |
|
225 | - */ |
|
226 | - public function getIterator(): Traversable |
|
227 | - { |
|
228 | - foreach ($this->to_array() as $attribute => $errors) { |
|
229 | - foreach ($errors as $error) { |
|
230 | - yield $attribute => $error; |
|
231 | - } |
|
232 | - } |
|
233 | - } |
|
223 | + /** |
|
224 | + * @return Traversable<string, Error> |
|
225 | + */ |
|
226 | + public function getIterator(): Traversable |
|
227 | + { |
|
228 | + foreach ($this->to_array() as $attribute => $errors) { |
|
229 | + foreach ($errors as $error) { |
|
230 | + yield $attribute => $error; |
|
231 | + } |
|
232 | + } |
|
233 | + } |
|
234 | 234 | |
235 | - /** |
|
236 | - * Iterates through errors using a callback. |
|
237 | - * |
|
238 | - * ```php |
|
239 | - * <?php |
|
240 | - * |
|
241 | - * use ICanBoogie\ErrorCollection; |
|
242 | - * |
|
243 | - * $errors = new ErrorCollection; |
|
244 | - * $errors->add('username', "Funny user name"); |
|
245 | - * $errors->add('password', "Weak password"); |
|
246 | - * |
|
247 | - * $errors->each(function ($error, $attribute, $errors) { |
|
248 | - * |
|
249 | - * echo "$attribute => $error\n"; |
|
250 | - * |
|
251 | - * }); |
|
252 | - * ``` |
|
253 | - * |
|
254 | - * @param callable(Error, string $attribute, ErrorCollection): void $callback |
|
255 | - * Function to execute for each element, taking three arguments: |
|
256 | - * |
|
257 | - * - `Error $error`: The current error. |
|
258 | - * - `string $attribute`: The attribute or {@link self::GENERIC}. |
|
259 | - * - `ErrorCollection $collection`: This instance. |
|
260 | - */ |
|
261 | - public function each(callable $callback): void |
|
262 | - { |
|
263 | - foreach ($this as $attribute => $error) { |
|
264 | - $callback($error, $attribute, $this); |
|
265 | - } |
|
266 | - } |
|
235 | + /** |
|
236 | + * Iterates through errors using a callback. |
|
237 | + * |
|
238 | + * ```php |
|
239 | + * <?php |
|
240 | + * |
|
241 | + * use ICanBoogie\ErrorCollection; |
|
242 | + * |
|
243 | + * $errors = new ErrorCollection; |
|
244 | + * $errors->add('username', "Funny user name"); |
|
245 | + * $errors->add('password', "Weak password"); |
|
246 | + * |
|
247 | + * $errors->each(function ($error, $attribute, $errors) { |
|
248 | + * |
|
249 | + * echo "$attribute => $error\n"; |
|
250 | + * |
|
251 | + * }); |
|
252 | + * ``` |
|
253 | + * |
|
254 | + * @param callable(Error, string $attribute, ErrorCollection): void $callback |
|
255 | + * Function to execute for each element, taking three arguments: |
|
256 | + * |
|
257 | + * - `Error $error`: The current error. |
|
258 | + * - `string $attribute`: The attribute or {@link self::GENERIC}. |
|
259 | + * - `ErrorCollection $collection`: This instance. |
|
260 | + */ |
|
261 | + public function each(callable $callback): void |
|
262 | + { |
|
263 | + foreach ($this as $attribute => $error) { |
|
264 | + $callback($error, $attribute, $this); |
|
265 | + } |
|
266 | + } |
|
267 | 267 | |
268 | - /** |
|
269 | - * Returns the total number of errors. |
|
270 | - * |
|
271 | - * @inheritDoc |
|
272 | - */ |
|
273 | - public function count(): int |
|
274 | - { |
|
275 | - return count($this->collection, COUNT_RECURSIVE) - count($this->collection); |
|
276 | - } |
|
268 | + /** |
|
269 | + * Returns the total number of errors. |
|
270 | + * |
|
271 | + * @inheritDoc |
|
272 | + */ |
|
273 | + public function count(): int |
|
274 | + { |
|
275 | + return count($this->collection, COUNT_RECURSIVE) - count($this->collection); |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * @inheritdoc |
|
280 | - */ |
|
281 | - public function jsonSerialize(): mixed |
|
282 | - { |
|
283 | - return $this->to_array(); |
|
284 | - } |
|
278 | + /** |
|
279 | + * @inheritdoc |
|
280 | + */ |
|
281 | + public function jsonSerialize(): mixed |
|
282 | + { |
|
283 | + return $this->to_array(); |
|
284 | + } |
|
285 | 285 | |
286 | - /** |
|
287 | - * Converts the object into an array. |
|
288 | - * |
|
289 | - * @return array<string, Error[]> |
|
290 | - */ |
|
291 | - public function to_array(): array |
|
292 | - { |
|
293 | - return array_filter(array_merge([ self::GENERIC => [] ], $this->collection)); |
|
294 | - } |
|
286 | + /** |
|
287 | + * Converts the object into an array. |
|
288 | + * |
|
289 | + * @return array<string, Error[]> |
|
290 | + */ |
|
291 | + public function to_array(): array |
|
292 | + { |
|
293 | + return array_filter(array_merge([ self::GENERIC => [] ], $this->collection)); |
|
294 | + } |
|
295 | 295 | } |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public function add( |
54 | 54 | string $attribute, |
55 | - Throwable|bool|string|Error $error_or_format_or_true = true, |
|
55 | + Throwable | bool | string | Error $error_or_format_or_true = true, |
|
56 | 56 | array $args = [] |
57 | 57 | ): static { |
58 | 58 | $this->assert_valid_error($error_or_format_or_true); |
@@ -74,7 +74,7 @@ discard block |
||
74 | 74 | * @return $this |
75 | 75 | */ |
76 | 76 | public function add_generic( |
77 | - Throwable|bool|string|Error $error_or_format_or_true = true, |
|
77 | + Throwable | bool | string | Error $error_or_format_or_true = true, |
|
78 | 78 | array $args = [] |
79 | 79 | ): static { |
80 | 80 | return $this->add(self::GENERIC, $error_or_format_or_true, $args); |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * @param array<int|string, mixed> $args |
111 | 111 | */ |
112 | 112 | private function ensure_error_instance( |
113 | - Throwable|bool|string|Error $error_or_format_or_true, |
|
113 | + Throwable | bool | string | Error $error_or_format_or_true, |
|
114 | 114 | array $args = [] |
115 | 115 | ): Error { |
116 | 116 | $error = $error_or_format_or_true; |
@@ -290,6 +290,6 @@ discard block |
||
290 | 290 | */ |
291 | 291 | public function to_array(): array |
292 | 292 | { |
293 | - return array_filter(array_merge([ self::GENERIC => [] ], $this->collection)); |
|
293 | + return array_filter(array_merge([self::GENERIC => []], $this->collection)); |
|
294 | 294 | } |
295 | 295 | } |
@@ -16,8 +16,8 @@ |
||
16 | 16 | */ |
17 | 17 | interface ErrorRenderer |
18 | 18 | { |
19 | - /** |
|
20 | - * Renders an error into a string. |
|
21 | - */ |
|
22 | - public function __invoke(Error $error, string $attribute, ErrorCollection $collection): string; |
|
19 | + /** |
|
20 | + * Renders an error into a string. |
|
21 | + */ |
|
22 | + public function __invoke(Error $error, string $attribute, ErrorCollection $collection): string; |
|
23 | 23 | } |
@@ -18,25 +18,25 @@ |
||
18 | 18 | */ |
19 | 19 | class Error implements JsonSerializable |
20 | 20 | { |
21 | - /** |
|
22 | - * @param array<int|string, mixed> $args |
|
23 | - */ |
|
24 | - public function __construct( |
|
25 | - public readonly string $format, |
|
26 | - public readonly array $args = [] |
|
27 | - ) { |
|
28 | - } |
|
21 | + /** |
|
22 | + * @param array<int|string, mixed> $args |
|
23 | + */ |
|
24 | + public function __construct( |
|
25 | + public readonly string $format, |
|
26 | + public readonly array $args = [] |
|
27 | + ) { |
|
28 | + } |
|
29 | 29 | |
30 | - public function __toString() |
|
31 | - { |
|
32 | - return format($this->format, $this->args); |
|
33 | - } |
|
30 | + public function __toString() |
|
31 | + { |
|
32 | + return format($this->format, $this->args); |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * @inheritDoc |
|
37 | - */ |
|
38 | - public function jsonSerialize(): mixed |
|
39 | - { |
|
40 | - return (string) $this; |
|
41 | - } |
|
35 | + /** |
|
36 | + * @inheritDoc |
|
37 | + */ |
|
38 | + public function jsonSerialize(): mixed |
|
39 | + { |
|
40 | + return (string) $this; |
|
41 | + } |
|
42 | 42 | } |