|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Chubbyphp\Parsing; |
| 6 | + |
| 7 | +/** |
| 8 | + * @phpstan-type ErrorWithPath array{path: string, error: Error} |
| 9 | + * @phpstan-type ErrorsWithPath array<ErrorWithPath> |
| 10 | + * @phpstan-type ErrorAsJson array{code: string, template: string, variables: array<string, mixed>} |
| 11 | + * @phpstan-type ErrorWithPathJson array{path: string, error: ErrorAsJson} |
| 12 | + * @phpstan-type ErrorsWithPathJson array<ErrorWithPathJson> |
| 13 | + * @phpstan-type ApiProblemInvalidParameter array{name: string, reason: string, details: non-empty-array<string, mixed>} |
| 14 | + */ |
| 15 | +final class Errors implements \JsonSerializable |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var ErrorsWithPath |
| 19 | + */ |
| 20 | + private array $errorsWithPath = []; |
| 21 | + |
| 22 | + public function __toString() |
| 23 | + { |
| 24 | + return implode(PHP_EOL, array_map(static fn ($error) => ('' !== $error['path'] ? $error['path'].': ' : '').$error['error'], $this->errorsWithPath)); |
| 25 | + } |
| 26 | + |
| 27 | + public function add(Error|self $errors, string $path = ''): self |
| 28 | + { |
| 29 | + if ($errors instanceof self) { |
| 30 | + foreach ($errors->errorsWithPath as $errorWithPath) { |
| 31 | + $this->errorsWithPath[] = ['path' => $this->mergePath($path, $errorWithPath['path']), 'error' => $errorWithPath['error']]; |
| 32 | + } |
| 33 | + |
| 34 | + return $this; |
| 35 | + } |
| 36 | + |
| 37 | + $this->errorsWithPath[] = ['path' => $path, 'error' => $errors]; |
| 38 | + |
| 39 | + return $this; |
| 40 | + } |
| 41 | + |
| 42 | + public function has(): bool |
| 43 | + { |
| 44 | + return 0 !== \count($this->errorsWithPath); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return ErrorsWithPathJson |
| 49 | + */ |
| 50 | + public function jsonSerialize(): array |
| 51 | + { |
| 52 | + return array_map(static fn ($errorWithPath) => [ |
| 53 | + 'path' => $errorWithPath['path'], |
| 54 | + 'error' => $errorWithPath['error']->jsonSerialize(), |
| 55 | + ], $this->errorsWithPath); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return array<ApiProblemInvalidParameter> |
| 60 | + */ |
| 61 | + public function toApiProblemInvalidParameters(): array |
| 62 | + { |
| 63 | + return array_map( |
| 64 | + fn ($errorWithPath) => [ |
| 65 | + 'name' => $this->pathToName($errorWithPath['path']), |
| 66 | + 'reason' => (string) $errorWithPath['error'], |
| 67 | + 'details' => [ |
| 68 | + '_template' => $errorWithPath['error']->template, |
| 69 | + ...$errorWithPath['error']->variables, |
| 70 | + ], |
| 71 | + ], |
| 72 | + $this->errorsWithPath |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @return array<string, mixed> |
| 78 | + */ |
| 79 | + public function toTree(): array |
| 80 | + { |
| 81 | + // @var array<string, mixed> $tree |
| 82 | + return array_reduce( |
| 83 | + $this->errorsWithPath, |
| 84 | + static function (array $tree, $errorWithPath): array { |
| 85 | + $pathParts = explode('.', $errorWithPath['path']); |
| 86 | + |
| 87 | + $current = &$tree; |
| 88 | + $lastIndex = \count($pathParts) - 1; |
| 89 | + |
| 90 | + foreach ($pathParts as $i => $pathPart) { |
| 91 | + if ($i < $lastIndex) { |
| 92 | + $current[$pathPart] ??= []; |
| 93 | + $current = &$current[$pathPart]; |
| 94 | + |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + $current[$pathPart] = array_merge($current[$pathPart] ?? [], [(string) $errorWithPath['error']]); |
| 99 | + } |
| 100 | + |
| 101 | + return $tree; |
| 102 | + }, |
| 103 | + [] |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + private function mergePath(string $path, string $existingPath): string |
| 108 | + { |
| 109 | + return implode('.', array_filter([$path, $existingPath], static fn ($part) => '' !== $part)); |
| 110 | + } |
| 111 | + |
| 112 | + private function pathToName(string $path): string |
| 113 | + { |
| 114 | + $pathParts = explode('.', $path); |
| 115 | + |
| 116 | + return implode('', array_map( |
| 117 | + static fn (string $pathPart, $i) => 0 === $i ? $pathPart : '['.$pathPart.']', |
| 118 | + $pathParts, |
| 119 | + array_keys($pathParts) |
| 120 | + )); |
| 121 | + } |
| 122 | +} |
0 commit comments