@@ -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(): string |
|
| 39 | - { |
|
| 40 | - return (string) $this; |
|
| 41 | - } |
|
| 35 | + /** |
|
| 36 | + * @inheritDoc |
|
| 37 | + */ |
|
| 38 | + public function jsonSerialize(): string |
|
| 39 | + { |
|
| 40 | + return (string) $this; |
|
| 41 | + } |
|
| 42 | 42 | } |
@@ -21,37 +21,37 @@ |
||
| 21 | 21 | */ |
| 22 | 22 | class ErrorCollectionIterator implements IteratorAggregate |
| 23 | 23 | { |
| 24 | - /** |
|
| 25 | - * @var ErrorRenderer|callable |
|
| 26 | - * @phpstan-var ErrorRenderer|(callable(Error,string $attribute,ErrorCollection):string) |
|
| 27 | - */ |
|
| 28 | - private $render_error; |
|
| 24 | + /** |
|
| 25 | + * @var ErrorRenderer|callable |
|
| 26 | + * @phpstan-var ErrorRenderer|(callable(Error,string $attribute,ErrorCollection):string) |
|
| 27 | + */ |
|
| 28 | + private $render_error; |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * @phpstan-param ErrorRenderer|(callable(Error,string $attribute,ErrorCollection):string)|null $render_error |
|
| 32 | - */ |
|
| 33 | - public function __construct( |
|
| 34 | - private readonly ErrorCollection $collection, |
|
| 35 | - ErrorRenderer|callable $render_error = null |
|
| 36 | - ) { |
|
| 37 | - $this->render_error = $render_error ?? fn(Error $error) => (string) $error; |
|
| 38 | - } |
|
| 30 | + /** |
|
| 31 | + * @phpstan-param ErrorRenderer|(callable(Error,string $attribute,ErrorCollection):string)|null $render_error |
|
| 32 | + */ |
|
| 33 | + public function __construct( |
|
| 34 | + private readonly ErrorCollection $collection, |
|
| 35 | + ErrorRenderer|callable $render_error = null |
|
| 36 | + ) { |
|
| 37 | + $this->render_error = $render_error ?? fn(Error $error) => (string) $error; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @return Traversable<string, string> |
|
| 42 | - */ |
|
| 43 | - public function getIterator(): Traversable |
|
| 44 | - { |
|
| 45 | - foreach ($this->collection as $attribute => $error) { |
|
| 46 | - yield $attribute => $this->render_error($error, $attribute); |
|
| 47 | - } |
|
| 48 | - } |
|
| 40 | + /** |
|
| 41 | + * @return Traversable<string, string> |
|
| 42 | + */ |
|
| 43 | + public function getIterator(): Traversable |
|
| 44 | + { |
|
| 45 | + foreach ($this->collection as $attribute => $error) { |
|
| 46 | + yield $attribute => $this->render_error($error, $attribute); |
|
| 47 | + } |
|
| 48 | + } |
|
| 49 | 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 | - } |
|
| 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 | } |
@@ -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 | - * @return array<string, Error[]> |
|
| 280 | - */ |
|
| 281 | - public function jsonSerialize(): array |
|
| 282 | - { |
|
| 283 | - return $this->to_array(); |
|
| 284 | - } |
|
| 278 | + /** |
|
| 279 | + * @return array<string, Error[]> |
|
| 280 | + */ |
|
| 281 | + public function jsonSerialize(): array |
|
| 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 | } |