Completed
Push — development ( daed94...13a157 )
by Thomas
18s
created

DateTimeValidator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 13 3
1
<?php
2
3
namespace Oc\FieldNotes\Validator\Constraints;
4
5
use DateTime as PHPDateTime;
6
use Symfony\Component\Validator\Constraint;
7
use Symfony\Component\Validator\ConstraintValidator;
8
9
class DateTimeValidator extends ConstraintValidator
10
{
11
    const FORMAT_LONG = 'Y-m-d\TH:i:s\Z';
12
13
    const FORMAT_LONG_EXPANDED = 'YYYY-MM-DDThh:mm:ssZ';
14
15
    const FORMAT_SHORT = 'Y-m-d\TH:i\Z';
16
17
    const FORMAT_SHORT_EXPANDED = 'YYYY-MM-DDThh:mmZ';
18
19
    /**
20
     * Checks if the passed value is valid.
21
     *
22
     * @param mixed $value The value that should be validated
0 ignored issues
show
Coding Style introduced by
Expected 6 spaces after parameter name; 1 found
Loading history...
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
23
     * @param Constraint $constraint The constraint for the validation
0 ignored issues
show
Coding Style Documentation introduced by
Parameter comment must end with a full stop
Loading history...
introduced by
Constraint => \Symfony\Component\Validator\Constraint
Loading history...
24
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
25
    public function validate($value, Constraint $constraint)
0 ignored issues
show
introduced by
Method does not have a return void statement in doc block: validate
Loading history...
26
    {
27
        $dateFormatLong = PHPDateTime::createFromFormat(self::FORMAT_LONG, $value);
28
        $dateFormatShort = PHPDateTime::createFromFormat(self::FORMAT_SHORT, $value);
29
30
        if ($dateFormatLong === false && $dateFormatShort === false) {
31
            $this->context->buildViolation($constraint->message)
32
                ->setParameter('%datetime%', $value)
33
                ->setParameter('%expectedFormatLong%', self::FORMAT_LONG_EXPANDED)
34
                ->setParameter('%expectedFormatShort%', self::FORMAT_SHORT_EXPANDED)
35
                ->addViolation();
36
        }
37
    }
38
}
39