|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Michaël Perrin |
|
4
|
|
|
* |
|
5
|
|
|
* Source: http://www.michaelperrin.fr/2013/03/19/range-date-validator-for-symfony2/ |
|
6
|
|
|
* |
|
7
|
|
|
* Modificaciones por Luis Ramón López López |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace AppBundle\Validator\Constraints; |
|
11
|
|
|
|
|
12
|
|
|
use Symfony\Component\Process\Exception\LogicException; |
|
13
|
|
|
use Symfony\Component\Validator\Constraint; |
|
14
|
|
|
use Symfony\Component\Validator\ConstraintValidator; |
|
15
|
|
|
use Symfony\Component\Validator\Context\ExecutionContext; |
|
16
|
|
|
|
|
17
|
|
|
class DateRangeValidator extends ConstraintValidator |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $message Violation message |
|
22
|
|
|
* @param array $parameters Message parameters |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function addViolation($message, $parameters) |
|
25
|
|
|
{ |
|
26
|
|
|
if ($this->context instanceof ExecutionContext) { |
|
27
|
|
|
$context = $this->context->buildViolation($message); |
|
28
|
|
|
|
|
29
|
|
|
foreach ($parameters as $name => $value) { |
|
30
|
|
|
$context = $context->setParameter($name, $value); |
|
31
|
|
|
}; |
|
32
|
|
|
|
|
33
|
|
|
$context->addViolation(); |
|
34
|
|
|
} |
|
35
|
|
|
else { |
|
36
|
|
|
throw new LogicException('Context API not 2.5 compatible'); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function validate($value, Constraint $constraint) |
|
44
|
|
|
{ |
|
45
|
|
|
if (null === $value) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!($value instanceof \DateTime)) { |
|
50
|
|
|
$this->addViolation($constraint->invalidMessage, |
|
51
|
|
|
array('{{ value }}' => $value)); |
|
52
|
|
|
|
|
53
|
|
|
return; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// evaluate now |
|
57
|
|
|
if (null !== $this->min) { |
|
58
|
|
|
$this->min = new \DateTime($this->min); |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (null !== $this->max) { |
|
62
|
|
|
$this->max = new \DateTime($this->max); |
|
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
if (null !== $constraint->max && $value > $constraint->max) { |
|
|
|
|
|
|
66
|
|
|
$this->addViolation($constraint->maxMessage, array( |
|
67
|
|
|
'{{ value }}' => $this->formatDate($value), |
|
68
|
|
|
'{{ limit }}' => $this->formatDate($constraint->max) |
|
69
|
|
|
)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
View Code Duplication |
if (null !== $constraint->min && $value < $constraint->min) { |
|
|
|
|
|
|
73
|
|
|
$this->addViolation($constraint->minMessage, array( |
|
74
|
|
|
'{{ value }}' => $this->formatDate($value), |
|
75
|
|
|
'{{ limit }}' => $this->formatDate($constraint->min) |
|
76
|
|
|
)); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function formatDate($date) |
|
81
|
|
|
{ |
|
82
|
|
|
$formatter = new \IntlDateFormatter( |
|
83
|
|
|
null, |
|
84
|
|
|
\IntlDateFormatter::SHORT, |
|
85
|
|
|
\IntlDateFormatter::NONE, |
|
86
|
|
|
date_default_timezone_get(), |
|
87
|
|
|
\IntlDateFormatter::GREGORIAN |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
return $this->processDate($formatter, $date); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param \IntlDateFormatter $formatter |
|
95
|
|
|
* @param \Datetime $date |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function processDate(\IntlDateFormatter $formatter, \Datetime $date) |
|
99
|
|
|
{ |
|
100
|
|
|
return $formatter->format((int) $date->format('U')); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: