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 Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException; |
15
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
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
|
|
|
|
21
|
|
|
final class DateAfterValidator extends ConstraintValidator |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
|
|
public function validate($value, Constraint $constraint): void |
27
|
|
|
{ |
28
|
|
|
if (!$constraint instanceof DateAfter) { |
29
|
|
|
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateAfter'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (!\is_object($value)) { |
33
|
|
|
return; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
$firstDate = $this->getFieldValue($value, $constraint->firstField); |
38
|
|
|
$secondDate = $this->getFieldValue($value, $constraint->secondField); |
39
|
|
|
} catch (NoSuchPropertyException $e) { |
40
|
|
|
throw new InvalidArgumentException($e->getMessage()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!$constraint->required && !$firstDate && !$secondDate) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!$firstDate && $secondDate) { |
48
|
|
|
$this->context |
49
|
|
|
->buildViolation($constraint->emptyMessage) |
50
|
|
|
->setParameter('%emptyField%', $constraint->firstField) |
51
|
|
|
->setParameter('%field%', $constraint->secondField) |
52
|
|
|
->atPath($constraint->firstField) |
53
|
|
|
->addViolation(); |
54
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} elseif ($firstDate && !$secondDate) { |
57
|
|
|
$this->context |
58
|
|
|
->buildViolation($constraint->emptyMessage) |
59
|
|
|
->setParameter('%emptyField%', $constraint->secondField) |
60
|
|
|
->setParameter('%field%', $constraint->firstField) |
61
|
|
|
->atPath($constraint->secondField) |
62
|
|
|
->addViolation(); |
63
|
|
|
|
64
|
|
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!$firstDate instanceof \DateTime || !$secondDate instanceof \DateTime) { |
68
|
|
|
throw new UnexpectedTypeException($value, \DateTime::class); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($firstDate > $secondDate) { |
72
|
|
|
$this->context |
73
|
|
|
->buildViolation($constraint->message) |
74
|
|
|
->setParameter('%firstField%', $constraint->firstField) |
75
|
|
|
->setParameter('%secondField%', $constraint->secondField) |
76
|
|
|
->atPath($constraint->secondField) |
77
|
|
|
->addViolation(); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param mixed $object |
83
|
|
|
* @param string $field |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
private function getFieldValue($object, string $field) |
88
|
|
|
{ |
89
|
|
|
$propertyAccessor = PropertyAccess::createPropertyAccessor(); |
90
|
|
|
|
91
|
|
|
return $propertyAccessor->getValue($object, $field); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|