|
4 | 4 |
|
5 | 5 | namespace PHPModelGenerator\Exception\ComposedValue;
|
6 | 6 |
|
| 7 | +use Exception; |
| 8 | +use PHPModelGenerator\Exception\ErrorRegistryExceptionInterface; |
7 | 9 | use PHPModelGenerator\Exception\ValidationException;
|
8 | 10 |
|
9 | 11 | /**
|
|
13 | 15 | */
|
14 | 16 | class ConditionalException extends ValidationException
|
15 | 17 | {
|
| 18 | + /** @var Exception|null */ |
| 19 | + private $ifException; |
| 20 | + /** @var Exception|null */ |
| 21 | + private $thenException; |
| 22 | + /** @var Exception|null */ |
| 23 | + private $elseException; |
| 24 | + |
16 | 25 | /**
|
17 | 26 | * ConditionalException constructor.
|
18 | 27 | *
|
19 | 28 | * @param $providedValue
|
20 | 29 | * @param string $propertyName
|
| 30 | + * @param Exception|null $ifException |
| 31 | + * @param Exception|null $thenException |
| 32 | + * @param Exception|null $elseException |
| 33 | + */ |
| 34 | + public function __construct( |
| 35 | + $providedValue, |
| 36 | + string $propertyName, |
| 37 | + ?Exception $ifException, |
| 38 | + ?Exception $thenException, |
| 39 | + ?Exception $elseException |
| 40 | + ) { |
| 41 | + |
| 42 | + // , |
| 43 | + $this->ifException = $ifException; |
| 44 | + $this->thenException = $thenException; |
| 45 | + $this->elseException = $elseException; |
| 46 | + |
| 47 | + parent::__construct($this->getErrorMessage($propertyName), $propertyName, $providedValue); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @return Exception|null |
| 52 | + */ |
| 53 | + public function getIfException(): ?Exception |
| 54 | + { |
| 55 | + return $this->ifException; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @return Exception|null |
21 | 60 | */
|
22 |
| - public function __construct($providedValue, string $propertyName) { |
23 |
| - parent::__construct( |
24 |
| - "Invalid value for $propertyName declined by conditional composition constraint", |
25 |
| - $propertyName, |
26 |
| - $providedValue |
27 |
| - ); |
| 61 | + public function getThenException(): ?Exception |
| 62 | + { |
| 63 | + return $this->thenException; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @return Exception|null |
| 68 | + */ |
| 69 | + public function getElseException(): ?Exception |
| 70 | + { |
| 71 | + return $this->elseException; |
| 72 | + } |
| 73 | + |
| 74 | + private function getErrorMessage(string $propertyName): string |
| 75 | + { |
| 76 | + $message = "Invalid value for $propertyName declined by conditional composition constraint\n"; |
| 77 | + |
| 78 | + $message .= $this->ifException |
| 79 | + ? " - Condition: Failed" . $this->getExceptionMessage($this->ifException) |
| 80 | + :' - Condition: Valid'; |
| 81 | + |
| 82 | + return $message . "\n - Conditional branch failed:" . |
| 83 | + $this->getExceptionMessage($this->thenException ?: $this->elseException); |
| 84 | + } |
| 85 | + |
| 86 | + private function getExceptionMessage(Exception $exception): string |
| 87 | + { |
| 88 | + return $exception instanceof ErrorRegistryExceptionInterface |
| 89 | + ? implode( |
| 90 | + "\n * ", |
| 91 | + array_map(function (ValidationException $exception): string { |
| 92 | + return $exception->getMessage(); |
| 93 | + }, $exception->getErrors()) |
| 94 | + ) |
| 95 | + : "\n * " . $exception->getMessage(); |
28 | 96 | }
|
29 | 97 | }
|
0 commit comments