|
15 | 15 |
|
16 | 16 | use OutOfBoundsException; |
17 | 17 | use Psr\Log\AbstractLogger; |
| 18 | +use Psr\Log\LogLevel; |
18 | 19 | use Stringable; |
19 | 20 |
|
20 | 21 | use function is_scalar; |
21 | 22 | use function is_string; |
22 | | -use function strtr; |
23 | 23 |
|
24 | 24 | /** |
25 | 25 | * @internal |
26 | 26 | * |
27 | 27 | * This class allows accessing WHATWG errors |
28 | 28 | * emitted by \Rowbot\URL\URL and convert them |
29 | | - * into \Uri\WhatWg\UrlValidationError instances |
| 29 | + * into an array of \Uri\WhatWg\UrlValidationError instances |
30 | 30 | * |
31 | 31 | * This class IS NOT PART of the RFC public API |
32 | | - * but is needed to implement the polyfill. |
| 32 | + * but is needed to implement the polyfill against |
| 33 | + * the \Rowbot\URL\URL package |
33 | 34 | */ |
34 | | -final class ValidationErrorLog extends AbstractLogger |
| 35 | +final class ValidationErrorLogger extends AbstractLogger |
35 | 36 | { |
36 | | - /** @var array<int, UrlValidationError> */ |
37 | | - private array $recoverableErrors = []; |
38 | | - /** @var array<int, UrlValidationError> */ |
| 37 | + /** @var list<UrlValidationError> */ |
39 | 38 | private array $errors = []; |
40 | 39 |
|
| 40 | + public function reset(): void |
| 41 | + { |
| 42 | + $this->errors = []; |
| 43 | + } |
| 44 | + |
41 | 45 | /** |
42 | | - * @return array<int, UrlValidationError> |
| 46 | + * @return list<UrlValidationError> |
43 | 47 | */ |
44 | 48 | public function errors(): array |
45 | 49 | { |
46 | 50 | return $this->errors; |
47 | 51 | } |
48 | 52 |
|
49 | 53 | /** |
50 | | - * @return array<int, UrlValidationError> |
| 54 | + * @return list<UrlValidationError> |
51 | 55 | */ |
52 | 56 | public function recoverableErrors(): array |
53 | 57 | { |
54 | | - return $this->recoverableErrors; |
| 58 | + return array_values( |
| 59 | + array_filter( |
| 60 | + $this->errors, |
| 61 | + fn (UrlValidationError $error): bool => !$error->failure |
| 62 | + ) |
| 63 | + ); |
55 | 64 | } |
56 | 65 |
|
57 | 66 | public function log(mixed $level, string|Stringable $message, array $context = []): void |
58 | 67 | { |
59 | 68 | $errorContext = $context['input'] ?? null; |
60 | | - if ($errorContext instanceof Stringable) { |
| 69 | + if (is_scalar($errorContext) || $errorContext instanceof Stringable) { |
61 | 70 | $errorContext = (string) $errorContext; |
62 | 71 | } |
63 | 72 |
|
64 | 73 | if (!is_string($errorContext)) { |
65 | 74 | return; |
66 | 75 | } |
67 | 76 |
|
68 | | - $validationError = new UrlValidationError( |
69 | | - $errorContext, |
70 | | - $this->messageMapper($this->interpolate($message, $context)), |
71 | | - 'warning' === $level |
72 | | - ); |
73 | | - |
74 | | - $this->errors[] = $validationError; |
75 | | - if ('notice' === $level) { |
76 | | - $this->recoverableErrors[] = $validationError; |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - public function reset(): void |
81 | | - { |
82 | | - $this->recoverableErrors = []; |
83 | | - $this->errors = []; |
84 | | - } |
85 | | - |
86 | | - private function messageMapper(string $message): UrlValidationErrorType |
87 | | - { |
88 | | - return match ($message) { |
| 77 | + $type = match ((string) $message) { |
89 | 78 | // recoverable errors |
90 | 79 | 'special-scheme-missing-following-solidus' => UrlValidationErrorType::SpecialSchemeMissingFollowingSolidus, |
91 | 80 | 'invalid-URL-unit' => UrlValidationErrorType::InvalidUrlUnit, |
@@ -117,20 +106,7 @@ private function messageMapper(string $message): UrlValidationErrorType |
117 | 106 | 'port-invalid' => UrlValidationErrorType::PortInvalid, |
118 | 107 | default => throw new OutOfBoundsException('unknown error type:'.$message), |
119 | 108 | }; |
120 | | - } |
121 | | - |
122 | | - /** |
123 | | - * @param array<array-key, mixed> $context |
124 | | - */ |
125 | | - private function interpolate(string|Stringable $message, array $context = []): string |
126 | | - { |
127 | | - $replacements = []; |
128 | | - foreach ($context as $key => $val) { |
129 | | - if (is_scalar($val) || $val instanceof Stringable) { |
130 | | - $replacements['{'.$key.'}'] = $val; |
131 | | - } |
132 | | - } |
133 | 109 |
|
134 | | - return strtr((string) $message, $replacements); |
| 110 | + $this->errors[] = new UrlValidationError($errorContext, $type, LogLevel::WARNING === $level); |
135 | 111 | } |
136 | 112 | } |
0 commit comments