1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* (c) Christian Gripp <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Core23\Form\Validator\Constraints; |
13
|
|
|
|
14
|
|
|
use DateTime; |
15
|
|
|
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
16
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
17
|
|
|
use Symfony\Component\Validator\Constraint; |
18
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
19
|
|
|
use Symfony\Component\Validator\Exception\InvalidArgumentException; |
20
|
|
|
use Symfony\Component\Validator\Exception\UnexpectedTypeException; |
21
|
|
|
|
22
|
|
|
final class DateAfterValidator extends ConstraintValidator |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
*/ |
27
|
|
|
public function validate($value, Constraint $constraint): void |
28
|
|
|
{ |
29
|
|
|
if (!$constraint instanceof DateAfter) { |
30
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateAfter'); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (!\is_object($value)) { |
34
|
|
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$firstFieldName = $constraint->firstField; |
38
|
|
|
$secondFieldName = $constraint->secondField; |
39
|
|
|
|
40
|
|
|
$firstDate = $this->getFieldValue($value, $firstFieldName); |
41
|
|
|
$secondDate = $this->getFieldValue($value, $secondFieldName); |
42
|
|
|
|
43
|
|
|
if (!$constraint->required && null === $firstDate && null === $secondDate) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (null === $firstDate && $secondDate) { |
48
|
|
|
$this->context |
49
|
|
|
->buildViolation($constraint->emptyMessage) |
50
|
|
|
->setParameter('%emptyField%', $firstFieldName) |
51
|
|
|
->setParameter('%field%', $secondFieldName) |
52
|
|
|
->atPath($firstFieldName) |
53
|
|
|
->addViolation() |
54
|
|
|
; |
55
|
|
|
|
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($firstDate && null === $secondDate) { |
60
|
|
|
$this->context |
61
|
|
|
->buildViolation($constraint->emptyMessage) |
62
|
|
|
->setParameter('%emptyField%', $secondFieldName) |
63
|
|
|
->setParameter('%field%', $firstFieldName) |
64
|
|
|
->atPath($secondFieldName) |
65
|
|
|
->addViolation() |
66
|
|
|
; |
67
|
|
|
|
68
|
|
|
return; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($firstDate > $secondDate) { |
72
|
|
|
$this->context |
73
|
|
|
->buildViolation($constraint->message) |
74
|
|
|
->setParameter('%firstField%', $firstFieldName) |
75
|
|
|
->setParameter('%secondField%', $secondFieldName) |
76
|
|
|
->atPath($secondFieldName) |
77
|
|
|
->addViolation() |
78
|
|
|
; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param mixed $object |
84
|
|
|
* @param string $field |
85
|
|
|
* |
86
|
|
|
* @return DateTime|null |
87
|
|
|
*/ |
88
|
|
|
private function getFieldValue($object, string $field): ?DateTime |
89
|
|
|
{ |
90
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
91
|
|
|
|
92
|
|
|
try { |
93
|
|
|
$value = $propertyAccessor->getValue($object, $field); |
94
|
|
|
} catch (NoSuchPropertyException $e) { |
95
|
|
|
throw new InvalidArgumentException($e->getMessage()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (null !== $value && !$value instanceof DateTime) { |
99
|
|
|
throw new UnexpectedTypeException($value, DateTime::class); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $value; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|