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