Skip to content

Commit 7f9542b

Browse files
committed
Extend conditional exception
1 parent 6af5dcd commit 7f9542b

File tree

1 file changed

+74
-6
lines changed

1 file changed

+74
-6
lines changed

src/Exception/ComposedValue/ConditionalException.php

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace PHPModelGenerator\Exception\ComposedValue;
66

7+
use Exception;
8+
use PHPModelGenerator\Exception\ErrorRegistryExceptionInterface;
79
use PHPModelGenerator\Exception\ValidationException;
810

911
/**
@@ -13,17 +15,83 @@
1315
*/
1416
class ConditionalException extends ValidationException
1517
{
18+
/** @var Exception|null */
19+
private $ifException;
20+
/** @var Exception|null */
21+
private $thenException;
22+
/** @var Exception|null */
23+
private $elseException;
24+
1625
/**
1726
* ConditionalException constructor.
1827
*
1928
* @param $providedValue
2029
* @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
2160
*/
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();
2896
}
2997
}

0 commit comments

Comments
 (0)