|
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
|
|
View Code Duplication |
if (null !== $constraint->min) { |
|
|
|
|
|
|
58
|
|
|
$dateMin = new \DateTime($constraint->min); |
|
59
|
|
|
|
|
60
|
|
|
if ($value < $dateMin) { |
|
61
|
|
|
$this->addViolation($constraint->minMessage, array( |
|
62
|
|
|
'{{ value }}' => $this->formatDate($value), |
|
63
|
|
|
'{{ limit }}' => $this->formatDate($constraint->min) |
|
64
|
|
|
)); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
if (null !== $constraint->max) { |
|
|
|
|
|
|
69
|
|
|
$dateMax = new \DateTime($constraint->max); |
|
70
|
|
|
|
|
71
|
|
|
if ($value > $dateMax) { |
|
72
|
|
|
$this->addViolation($constraint->maxMessage, array( |
|
73
|
|
|
'{{ value }}' => $this->formatDate($value), |
|
74
|
|
|
'{{ limit }}' => $this->formatDate($constraint->max) |
|
75
|
|
|
)); |
|
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
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.