1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Exception; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\DataTransferObjectInterface; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Testing\EntityDebugDumper; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\Traits\RelativePathTraceTrait; |
9
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
10
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
11
|
|
|
|
12
|
|
|
class ValidationException extends DoctrineStaticMetaException |
13
|
|
|
{ |
14
|
|
|
use RelativePathTraceTrait; |
15
|
|
|
|
16
|
|
|
protected $dataObject; |
17
|
|
|
|
18
|
|
|
protected $errors; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ValidationException constructor. |
22
|
|
|
* |
23
|
|
|
* @param ConstraintViolationListInterface|ConstraintViolationInterface[] $errors |
24
|
|
|
* @param DataTransferObjectInterface|EntityInterface $dataObject |
25
|
|
|
* @param int $code |
26
|
|
|
* @param \Exception|null $previous |
27
|
|
|
*/ |
28
|
2 |
|
public function __construct( |
29
|
|
|
ConstraintViolationListInterface $errors, |
30
|
|
|
$dataObject, |
31
|
|
|
$code = 0, |
32
|
|
|
\Exception $previous = null |
33
|
|
|
) { |
34
|
2 |
|
$this->errors = $errors; |
35
|
2 |
|
$this->dataObject = $dataObject; |
36
|
|
|
switch (true) { |
37
|
2 |
|
case $dataObject instanceof EntityInterface: |
38
|
2 |
|
$message = $this->entityExceptionMessage($errors, $dataObject); |
39
|
2 |
|
break; |
40
|
|
|
case $dataObject instanceof DataTransferObjectInterface: |
41
|
|
|
$message = $this->dtoExceptionMessage($errors, $dataObject); |
42
|
|
|
break; |
43
|
|
|
default: |
44
|
|
|
$message = 'Unexpected datObject passed to ValidationException: ' . print_r($dataObject, true); |
45
|
|
|
} |
46
|
2 |
|
parent::__construct($message, $code, $previous); |
47
|
2 |
|
} |
48
|
|
|
|
49
|
2 |
|
private function entityExceptionMessage( |
50
|
|
|
ConstraintViolationListInterface $errors, |
51
|
|
|
EntityInterface $entity |
52
|
|
|
): string { |
53
|
2 |
|
$message = $this->getErrorsSummary($errors, $entity::getDoctrineStaticMeta()->getReflectionClass()->getName()); |
54
|
2 |
|
$message .= "\n\nFull Data Object Dump:" . (new EntityDebugDumper())->dump($entity); |
55
|
|
|
|
56
|
2 |
|
return $message; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ConstraintViolationListInterface|ConstraintViolationInterface[] $errors |
61
|
|
|
* @param string $className |
62
|
|
|
* |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
2 |
|
private function getErrorsSummary(ConstraintViolationListInterface $errors, string $className): string |
66
|
|
|
{ |
67
|
2 |
|
$message = "\nFound " . $errors->count() . " errors validating\n" . $className; |
68
|
2 |
|
foreach ($errors as $error) { |
69
|
|
|
$property = $error->getPropertyPath(); |
70
|
|
|
$getter = 'get' . $property; |
71
|
|
|
if (method_exists($this->dataObject, $getter)) { |
72
|
|
|
try { |
73
|
|
|
$value = $this->dataObject->$getter(); |
74
|
|
|
} catch (\TypeError $e) { |
75
|
|
|
$message .= "\n\n$property has TypeError: " . $e->getMessage(); |
76
|
|
|
continue; |
77
|
|
|
} |
78
|
|
|
if (is_object($value) === true) { |
79
|
|
|
$value = get_class($value); |
80
|
|
|
} |
81
|
|
|
$message .= "\n\n$property [$value]: " . $error->getMessage() . ' (code: ' . $error->getCode() . ')'; |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
$message .= "\n\n$property: " . $error->getMessage(); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
return $message; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function dtoExceptionMessage( |
91
|
|
|
ConstraintViolationListInterface $errors, |
92
|
|
|
DataTransferObjectInterface $dto |
93
|
|
|
): string { |
94
|
|
|
return $this->getErrorsSummary($errors, (new \ReflectionClass($dto))->getShortName()); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
public function getInvalidDataObject(): object |
98
|
|
|
{ |
99
|
1 |
|
return $this->dataObject; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public function getValidationErrors(): ConstraintViolationListInterface |
103
|
|
|
{ |
104
|
1 |
|
return $this->errors; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|