| Total Complexity | 4 |
| Total Lines | 28 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 16 | #[AsService, AsTagged(FormatterInterface::class, priority: 0)] |
||
| 17 | class ValidationFormatter implements FormatterInterface |
||
| 18 | 1 | { |
|
| 19 | public function __invoke(ValidationException $e): ErrorContract |
||
| 20 | 1 | { |
|
| 21 | 1 | return new FormattedError( |
|
| 22 | $e->getMessage(), |
||
| 23 | 1 | Response::HTTP_UNPROCESSABLE_ENTITY, |
|
| 24 | 1 | self::formatErrors(...$e->getErrors()), |
|
| 25 | $e->getTrace() |
||
| 26 | ); |
||
| 27 | } |
||
| 28 | 1 | ||
| 29 | public static function getType(): string |
||
| 30 | 1 | { |
|
| 31 | return ValidationException::class; |
||
| 32 | } |
||
| 33 | 1 | ||
| 34 | private static function formatErrors(Error ...$errors): array |
||
| 35 | 1 | { |
|
| 36 | return ArrayList::collect($errors) |
||
| 37 | ->map(static fn (Error $error) => [$error->getField() => $error->getMessage()]) |
||
| 38 | 1 | ->reduce(array_merge(...)) |
|
|
|
|||
| 39 | ->getOrElse([]) |
||
| 40 | 1 | ; |
|
| 41 | 1 | } |
|
| 42 | } |
||
| 43 |