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