Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
98.21% |
55 / 56 |
|
87.50% |
7 / 8 |
CRAP | |
0.00% |
0 / 1 |
ValidationException | |
98.21% |
55 / 56 |
|
87.50% |
7 / 8 |
17 | |
0.00% |
0 / 1 |
getStatusCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
getErrors | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getChainedExceptions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createFromArray | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
4 | |||
toArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
mergeProperty | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
5 | |||
provideSchema | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | namespace Apie\Serializer\Exceptions; |
3 | |
4 | use Apie\Core\Attributes\SchemaMethod; |
5 | use Apie\Core\Exceptions\ApieException; |
6 | use Apie\Core\Exceptions\HttpStatusCodeException; |
7 | use Apie\Core\Lists\StringHashmap; |
8 | use Apie\TypeConverter\Exceptions\GetMultipleChainedExceptionInterface; |
9 | use Exception; |
10 | use Throwable; |
11 | |
12 | #[SchemaMethod('provideSchema')] |
13 | class ValidationException extends ApieException implements HttpStatusCodeException, GetMultipleChainedExceptionInterface |
14 | { |
15 | /** |
16 | * @var array<int|string, Throwable> |
17 | */ |
18 | private array $chainedExceptions = []; |
19 | |
20 | public function getStatusCode(): int |
21 | { |
22 | return 422; |
23 | } |
24 | |
25 | public function __construct(private readonly StringHashmap $errors, ?Exception $previous = null) |
26 | { |
27 | $validationMessage = ''; |
28 | if ($errors->count() > 0) { |
29 | $validationMessage = ': ' . $errors->first(); |
30 | } |
31 | $this->chainedExceptions[] = $previous; |
32 | |
33 | parent::__construct('Validation error' . $validationMessage, 0, $previous); |
34 | } |
35 | |
36 | public function getErrors(): StringHashmap |
37 | { |
38 | return $this->errors; |
39 | } |
40 | |
41 | public function getChainedExceptions(): array |
42 | { |
43 | return $this->chainedExceptions; |
44 | } |
45 | |
46 | /** |
47 | * @param array<string, Exception> $errors |
48 | */ |
49 | public static function createFromArray(array $errors): self |
50 | { |
51 | $list = []; |
52 | $previous = null; |
53 | foreach ($errors as $property => $error) { |
54 | $previous = $error; |
55 | if ($error instanceof ValidationException) { |
56 | $list = array_merge($list, $error->toArray($property)); |
57 | continue; |
58 | } |
59 | if ($error instanceof GetMultipleChainedExceptionInterface) { |
60 | $list[$property] = implode( |
61 | PHP_EOL, |
62 | array_map( |
63 | function (Throwable $error) { |
64 | return $error->getMessage(); |
65 | }, |
66 | $error->getChainedExceptions() |
67 | ) |
68 | ); |
69 | continue; |
70 | } |
71 | $list[$property] = $error->getMessage(); |
72 | } |
73 | $res = new ValidationException( |
74 | new StringHashmap($list), |
75 | $previous |
76 | ); |
77 | $res->chainedExceptions = $errors; |
78 | return $res; |
79 | } |
80 | |
81 | /** |
82 | * @return array<string, string> |
83 | */ |
84 | private function toArray(string $prefix = ''): array |
85 | { |
86 | $newList = []; |
87 | foreach ($this->errors as $property => $errorMessage) { |
88 | $newPropertyName = self::mergeProperty($prefix, $property); |
89 | $newList[$newPropertyName] = $errorMessage; |
90 | } |
91 | |
92 | return $newList; |
93 | } |
94 | |
95 | private static function mergeProperty(string $prefix, string|int $property): string |
96 | { |
97 | if ($prefix) { |
98 | return ($property || $property === 0 || $property === '0') ? ($prefix . '.' . $property) : $prefix; |
99 | } |
100 | return $property; |
101 | } |
102 | |
103 | public static function provideSchema(): array |
104 | { |
105 | return [ |
106 | 'type' => 'object', |
107 | 'required' => ['message', 'errors'], |
108 | 'properties' => [ |
109 | 'message' => [ |
110 | 'type' => 'string', |
111 | ], |
112 | 'errors' => [ |
113 | 'type' => 'object', |
114 | 'additionalProperties' => [ |
115 | 'type' => 'string', |
116 | ] |
117 | ], |
118 | ] |
119 | ]; |
120 | } |
121 | } |