Passed
Pull Request — development (#682)
by Nick
07:18
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
/**
10
 * Class DateTimeValidator
11
 *
12
 * @package Oc\FieldNotes\Validator\Constraints
0 ignored issues
show
introduced by
Unexpected tag type @package in doc block
Loading history...
13
 */
14
class DateTimeValidator extends ConstraintValidator
15
{
16
    const FORMAT_LONG = 'Y-m-d\TH:i:s\Z';
17
18
    const FORMAT_LONG_EXPANDED = 'YYYY-MM-DDThh:mm:ssZ';
19
20
    const FORMAT_SHORT = 'Y-m-d\TH:i\Z';
21
22
    const FORMAT_SHORT_EXPANDED = 'YYYY-MM-DDThh:mmZ';
23
24
    /**
25
     * Checks if the passed value is valid.
26
     *
27
     * @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...
28
     * @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...
29
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @return tag in function comment
Loading history...
30
    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...
31
    {
32
        $dateFormatLong = PHPDateTime::createFromFormat(self::FORMAT_LONG, $value);
33
        $dateFormatShort = PHPDateTime::createFromFormat(self::FORMAT_SHORT, $value);
34
35
        if ($dateFormatLong === false && $dateFormatShort === false) {
36
            $this->context->buildViolation($constraint->message)
37
                ->setParameter('%datetime%', $value)
38
                ->setParameter('%expectedFormatLong%', self::FORMAT_LONG_EXPANDED)
39
                ->setParameter('%expectedFormatShort%', self::FORMAT_SHORT_EXPANDED)
40
                ->addViolation();
41
        }
42
    }
43
}
44