Total Complexity | 6 |
Total Lines | 55 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
10 | class ValidationException extends \RuntimeException |
||
11 | { |
||
12 | protected ValidationResult $validationResult; |
||
13 | |||
14 | /** |
||
15 | * ValidationException constructor. |
||
16 | * |
||
17 | * @throws UnexpectedValueException |
||
18 | */ |
||
19 | 6 | public function __construct(ValidationResult $validation) |
|
20 | { |
||
21 | 6 | if (!$validation->failed()) { |
|
22 | 1 | throw new UnexpectedValueException("Validation didn't fail, no exception should be thrown"); |
|
23 | } |
||
24 | |||
25 | 5 | parent::__construct("Validation failed;\n * " . join("\n * ", $validation->getErrors())); |
|
26 | |||
27 | 5 | $this->validationResult = $validation; |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Get the validation result with the validation errors. |
||
32 | */ |
||
33 | 1 | public function getValidationResult(): ValidationResult |
|
34 | { |
||
35 | 1 | return $this->validationResult; |
|
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get the (first) validation error |
||
40 | */ |
||
41 | 2 | public function getError(): ?string |
|
42 | { |
||
43 | 2 | return $this->validationResult->getError(); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * Get the validation errors |
||
48 | * |
||
49 | * @return string[] |
||
50 | */ |
||
51 | 1 | public function getErrors(): array |
|
52 | { |
||
53 | 1 | return $this->validationResult->getErrors(); |
|
54 | } |
||
55 | |||
56 | |||
57 | /** |
||
58 | * Factory method for failed validation |
||
59 | */ |
||
60 | 1 | public static function error(string $message, mixed ...$args): static |
|
65 | } |
||
66 | } |
||
67 |