1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Validation; |
4
|
|
|
|
5
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\DataTransferObjectInterface; |
6
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface; |
7
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\Validation\EntityDataValidatorInterface; |
8
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\Exception\ValidationException; |
9
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
10
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
11
|
|
|
|
12
|
|
|
class EntityDataValidator implements EntityDataValidatorInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ValidatorInterface |
16
|
|
|
*/ |
17
|
|
|
protected $validator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var EntityInterface|DataTransferObjectInterface |
21
|
|
|
*/ |
22
|
|
|
protected $dataObject; |
23
|
|
|
|
24
|
|
|
public function __construct(ValidatorInterface $validator) |
25
|
|
|
{ |
26
|
|
|
$this->validator = $validator; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Set an Entity to be validated |
31
|
|
|
* |
32
|
|
|
* @param EntityInterface $entity |
33
|
|
|
* |
34
|
|
|
* @return EntityDataValidatorInterface |
35
|
|
|
*/ |
36
|
|
|
public function setEntity(EntityInterface $entity): EntityDataValidatorInterface |
37
|
|
|
{ |
38
|
|
|
$this->dataObject = $entity; |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set an data Transfer Object to be validated |
45
|
|
|
* |
46
|
|
|
* @param DataTransferObjectInterface $dto |
47
|
|
|
* |
48
|
|
|
* @return EntityDataValidatorInterface |
49
|
|
|
*/ |
50
|
|
|
public function setDto(DataTransferObjectInterface $dto): EntityDataValidatorInterface |
51
|
|
|
{ |
52
|
|
|
$this->dataObject = $dto; |
53
|
|
|
|
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Simply check for validity as a boolean, with no error reporting |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function isValid(): bool |
63
|
|
|
{ |
64
|
|
|
return $this->getErrors()->count() === 0; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Perform validation and return any error that are present |
69
|
|
|
* |
70
|
|
|
* @return ConstraintViolationListInterface |
71
|
|
|
*/ |
72
|
|
|
public function getErrors(): ConstraintViolationListInterface |
73
|
|
|
{ |
74
|
|
|
return $this->validator->validate($this->dataObject); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Perform validation and then return a message that details the number and the details of the errors |
79
|
|
|
* |
80
|
|
|
* Will return an empty string if there are no errors |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getErrorsAsString(): string |
85
|
|
|
{ |
86
|
|
|
$errors = $this->getErrors(); |
87
|
|
|
if (0 === $errors->count()) { |
88
|
|
|
return ''; |
89
|
|
|
} |
90
|
|
|
$message = 'found ' . $errors->count() . ' errors validating ' |
91
|
|
|
. \get_class($this->dataObject); |
92
|
|
|
foreach ($errors as $error) { |
93
|
|
|
$message .= "\n\n" . $error->getPropertyPath() . ': ' . $error->getMessage(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $message; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Validate the whole entity and provide a verbose error report |
101
|
|
|
* |
102
|
|
|
* @throws ValidationException |
103
|
|
|
*/ |
104
|
|
|
public function validate(): void |
105
|
|
|
{ |
106
|
|
|
$errors = $this->getErrors(); |
107
|
|
|
$this->throwExceptionIfErrors($errors); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param ConstraintViolationListInterface $errors |
112
|
|
|
* |
113
|
|
|
* @throws ValidationException |
114
|
|
|
*/ |
115
|
|
|
private function throwExceptionIfErrors(ConstraintViolationListInterface $errors): void |
116
|
|
|
{ |
117
|
|
|
if (0 === $errors->count()) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
throw new ValidationException($errors, $this->dataObject); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Validate a single entity property |
125
|
|
|
* |
126
|
|
|
* @param string $propertyName |
127
|
|
|
* |
128
|
|
|
* @throws ValidationException |
129
|
|
|
*/ |
130
|
|
|
public function validateProperty(string $propertyName): void |
131
|
|
|
{ |
132
|
|
|
$errors = $this->validator->validateProperty($this->dataObject, $propertyName); |
133
|
|
|
$this->throwExceptionIfErrors($errors); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|