1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the zibios/sharep. |
7
|
|
|
* |
8
|
|
|
* (c) Zbigniew Ślązak |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace App\Validator\Constraints; |
12
|
|
|
|
13
|
|
|
use App\Entity\EntityInterface; |
14
|
|
|
use App\Enum\Functional\ApplicationEnum; |
15
|
|
|
use App\Repository\Functional\NotOverlappedDatesRepository; |
16
|
|
|
use Symfony\Component\Validator\Constraint; |
17
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
18
|
|
|
use Symfony\Component\Validator\Exception\InvalidArgumentException; |
19
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
20
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedValueException; |
21
|
|
|
|
22
|
|
|
class NotOverlappedDatesValidator extends ConstraintValidator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var NotOverlappedDatesRepository |
26
|
|
|
*/ |
27
|
|
|
private $repository; |
28
|
|
|
|
29
|
4 |
|
public function __construct(NotOverlappedDatesRepository $repository) |
30
|
|
|
{ |
31
|
4 |
|
$this->repository = $repository; |
32
|
4 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param EntityInterface $entity |
36
|
|
|
* @param NotOverlappedDates|Constraint $constraint |
37
|
|
|
*/ |
38
|
4 |
|
public function validate($entity, Constraint $constraint): void |
39
|
|
|
{ |
40
|
4 |
|
$this->assertInstances($entity, $constraint); |
41
|
4 |
|
$this->assertConstraintProperties($entity, $constraint); |
|
|
|
|
42
|
4 |
|
$this->assertDatesInstanceOf($entity, $constraint); |
|
|
|
|
43
|
|
|
|
44
|
4 |
|
if (false === $this->validateDatesNotEmpty($entity, $constraint)) { |
|
|
|
|
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
if (false === $this->validateDatesOrder($entity, $constraint)) { |
|
|
|
|
49
|
|
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
$overlappedEntities = $this->repository->getOverlappedEntities( |
53
|
4 |
|
\get_class($entity), |
54
|
4 |
|
$constraint->fromDateProperty, |
55
|
4 |
|
$constraint->toDateProperty, |
56
|
4 |
|
$this->getFromDate($entity, $constraint), |
|
|
|
|
57
|
4 |
|
$this->getToDate($entity, $constraint) |
|
|
|
|
58
|
|
|
); |
59
|
|
|
|
60
|
4 |
|
if (1 === \count($overlappedEntities) && $overlappedEntities[0]->getId() === $entity->getId()) { |
61
|
1 |
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
if (\count($overlappedEntities) > 0) { |
65
|
|
|
$periodsString = ''; |
66
|
|
|
foreach ($overlappedEntities as $overlappedEntity) { |
67
|
|
|
$periodsString .= sprintf( |
68
|
|
|
'%s - %s,', |
69
|
|
|
$this->getFromDateString($overlappedEntity, $constraint), |
|
|
|
|
70
|
|
|
$this->getToDateString($overlappedEntity, $constraint) |
|
|
|
|
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
$this->context->buildViolation($constraint::INVALID_PERIOD_OVERLAPPED) |
74
|
|
|
->setParameter('{{ fromDate }}', $this->getFromDateString($entity, $constraint)) |
|
|
|
|
75
|
|
|
->setParameter('{{ toDate }}', $this->getToDateString($entity, $constraint)) |
|
|
|
|
76
|
|
|
->setParameter('{{ periods }}', $periodsString) |
77
|
|
|
->atPath($constraint->toDateProperty) |
78
|
|
|
->addViolation(); |
79
|
|
|
} |
80
|
4 |
|
} |
81
|
|
|
|
82
|
|
|
//------------------------------------------------------------------------------------------------------------------ |
83
|
|
|
|
84
|
4 |
|
private function assertInstances($entity, Constraint $constraint): void |
85
|
|
|
{ |
86
|
4 |
|
if (!$constraint instanceof NotOverlappedDates) { |
87
|
|
|
throw new UnexpectedTypeException($constraint, NotOverlappedDates::class); |
88
|
|
|
} |
89
|
4 |
|
if (!$entity instanceof EntityInterface) { |
90
|
|
|
throw new UnexpectedValueException($entity, EntityInterface::class); |
91
|
|
|
} |
92
|
4 |
|
} |
93
|
|
|
|
94
|
4 |
|
private function assertConstraintProperties(EntityInterface $entity, NotOverlappedDates $constraint): void |
95
|
|
|
{ |
96
|
4 |
|
if (!method_exists($entity, $constraint->fromDateMethod)) { |
97
|
|
|
throw new InvalidArgumentException( |
98
|
|
|
sprintf('Method \'%s\' not exist \'%s\'', $constraint->fromDateMethod, \get_class($entity)) |
99
|
|
|
); |
100
|
|
|
} |
101
|
4 |
View Code Duplication |
if (!\is_callable([$entity, $constraint->fromDateMethod])) { |
|
|
|
|
102
|
|
|
throw new InvalidArgumentException( |
103
|
|
|
sprintf('Method \'%s\' not public \'%s\'', $constraint->fromDateMethod, \get_class($entity)) |
104
|
|
|
); |
105
|
|
|
} |
106
|
4 |
View Code Duplication |
if (!property_exists($entity, $constraint->fromDateProperty)) { |
|
|
|
|
107
|
|
|
throw new InvalidArgumentException( |
108
|
|
|
sprintf('Property \'%s\' not exist \'%s\'', $constraint->fromDateProperty, \get_class($entity)) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
4 |
View Code Duplication |
if (!method_exists($entity, $constraint->toDateMethod)) { |
|
|
|
|
113
|
|
|
throw new InvalidArgumentException( |
114
|
|
|
sprintf('Method \'%s\' not exist in \'%s\'', $constraint->toDateMethod, \get_class($entity)) |
115
|
|
|
); |
116
|
|
|
} |
117
|
4 |
View Code Duplication |
if (!\is_callable([$entity, $constraint->toDateMethod])) { |
|
|
|
|
118
|
|
|
throw new InvalidArgumentException( |
119
|
|
|
sprintf('Method \'%s\' not public \'%s\'', $constraint->toDateMethod, \get_class($entity)) |
120
|
|
|
); |
121
|
|
|
} |
122
|
4 |
View Code Duplication |
if (!property_exists($entity, $constraint->toDateProperty)) { |
|
|
|
|
123
|
|
|
throw new InvalidArgumentException( |
124
|
|
|
sprintf('Property \'%s\' not exist \'%s\'', $constraint->toDateProperty, \get_class($entity)) |
125
|
|
|
); |
126
|
|
|
} |
127
|
4 |
|
} |
128
|
|
|
|
129
|
4 |
|
private function assertDatesInstanceOf(EntityInterface $entity, NotOverlappedDates $constraint): void |
130
|
|
|
{ |
131
|
4 |
|
$fromDate = $entity->{$constraint->fromDateMethod}(); |
132
|
4 |
|
if (null !== $fromDate && !$fromDate instanceof \DateTimeInterface) { |
133
|
|
|
throw new UnexpectedValueException($fromDate, \DateTimeInterface::class); |
134
|
|
|
} |
135
|
4 |
|
$toDate = $entity->{$constraint->toDateMethod}(); |
136
|
4 |
|
if (null !== $toDate && !$toDate instanceof \DateTimeInterface) { |
137
|
|
|
throw new UnexpectedValueException($toDate, \DateTimeInterface::class); |
138
|
|
|
} |
139
|
4 |
|
} |
140
|
|
|
|
141
|
4 |
|
private function validateDatesNotEmpty(EntityInterface $entity, NotOverlappedDates $constraint): bool |
|
|
|
|
142
|
|
|
{ |
143
|
4 |
|
$result = true; |
144
|
4 |
View Code Duplication |
if (!$this->getFromDate($entity, $constraint) instanceof \DateTimeInterface) { |
|
|
|
|
145
|
|
|
$this->context->buildViolation($constraint::INVALID_FROM_DATE) |
146
|
|
|
->setParameter('{{ fromDate }}', 'N/A') |
147
|
|
|
->atPath($constraint->fromDateProperty) |
148
|
|
|
->addViolation(); |
149
|
|
|
$result = false; |
150
|
|
|
} |
151
|
4 |
View Code Duplication |
if (!$this->getToDate($entity, $constraint) instanceof \DateTimeInterface) { |
|
|
|
|
152
|
|
|
$this->context->buildViolation($constraint::INVALID_TO_DATE) |
153
|
|
|
->setParameter('{{ toDate }}', 'N/A') |
154
|
|
|
->atPath($constraint->toDateProperty) |
155
|
|
|
->addViolation(); |
156
|
|
|
$result = false; |
157
|
|
|
} |
158
|
|
|
|
159
|
4 |
|
return $result; |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
private function validateDatesOrder(EntityInterface $entity, NotOverlappedDates $constraint): bool |
|
|
|
|
163
|
|
|
{ |
164
|
4 |
|
$fromDateString = $this->getFromDateString($entity, $constraint); |
165
|
4 |
|
$toDateString = $this->getToDateString($entity, $constraint); |
166
|
|
|
|
167
|
4 |
View Code Duplication |
if ($fromDateString >= $toDateString) { |
|
|
|
|
168
|
|
|
$this->context->buildViolation($constraint::INVALID_ORDER) |
169
|
|
|
->setParameter('{{ fromDate }}', $fromDateString) |
170
|
|
|
->setParameter('{{ toDate }}', $toDateString) |
171
|
|
|
->atPath($constraint->toDateProperty) |
172
|
|
|
->addViolation(); |
173
|
|
|
|
174
|
|
|
return false; |
175
|
|
|
} |
176
|
|
|
|
177
|
4 |
|
return true; |
178
|
|
|
} |
179
|
|
|
|
180
|
4 |
|
private function getFromDate(EntityInterface $entity, NotOverlappedDates $constraint): \DateTimeInterface |
181
|
|
|
{ |
182
|
4 |
|
return $entity->{$constraint->fromDateMethod}(); |
183
|
|
|
} |
184
|
|
|
|
185
|
4 |
|
private function getToDate(EntityInterface $entity, NotOverlappedDates $constraint): \DateTimeInterface |
186
|
|
|
{ |
187
|
4 |
|
return $entity->{$constraint->toDateMethod}(); |
188
|
|
|
} |
189
|
|
|
|
190
|
4 |
|
private function getFromDateString(EntityInterface $entity, NotOverlappedDates $constraint): string |
191
|
|
|
{ |
192
|
4 |
|
return $this->getFromDate($entity, $constraint)->format(ApplicationEnum::DATE_FORMAT); |
193
|
|
|
} |
194
|
|
|
|
195
|
4 |
|
private function getToDateString(EntityInterface $entity, NotOverlappedDates $constraint): string |
196
|
|
|
{ |
197
|
4 |
|
return $this->getToDate($entity, $constraint)->format(ApplicationEnum::DATE_FORMAT); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.