Completed
Push — master ( 767608...f936ed )
by Luis Ramón
02:42
created

DateRangeValidator::addViolation()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12
Metric Value
dl 0
loc 15
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 12
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);
0 ignored issues
show
Bug introduced by
The property min does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
        }
60
61
        if (null !== $this->max) {
62
            $this->max = new \DateTime($this->max);
0 ignored issues
show
Bug introduced by
The property max does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
63
        }
64
65 View Code Duplication
        if (null !== $constraint->max && $value > $constraint->max) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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