DateTimeValidator   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 13
c 1
b 0
f 1
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A support() 0 3 2
A validate() 0 14 3
1
<?php
2
/**
3
 * Schema Validator
4
 *
5
 * @author Vlad Shashkov <[email protected]>
6
 * @copyright Copyright (c) 2021, The Myaza Software
7
 */
8
9
declare(strict_types=1);
10
11
namespace SchemaValidator\Validator;
12
13
use SchemaValidator\Argument;
14
use SchemaValidator\Context;
15
use Symfony\Component\Validator\Util\PropertyPath;
16
17
final class DateTimeValidator implements ValidatorInterface
18
{
19
    private const INVALID_DATE_MESSAGE = 'This is not a valid date.';
20
    private const INVALID_DATE_CODE    = '780e721d-ce3c-42f3-854d-f990ebffdc4f';
21
22 4
    public function support(\ReflectionType $type): bool
23
    {
24 4
        return $type instanceof \ReflectionNamedType && is_subclass_of($type->getName(), \DateTimeInterface::class);
25
    }
26
27
    /**
28
     * @psalm-suppress MixedAssignment
29
     */
30 18
    public function validate(Argument $argument, Context $context): void
31
    {
32 18
        $execution = $context->getExecution();
33 18
        $value     = $argument->getValueByArgumentName();
34
35 18
        if (is_string($value) && is_int(strtotime($value))) {
36 6
            return;
37
        }
38
39 12
        $execution->buildViolation(self::INVALID_DATE_MESSAGE)
40 12
            ->atPath(PropertyPath::append($context->getRootPath(), $argument->getName()))
41 12
            ->setInvalidValue($value)
42 12
            ->setCode(self::INVALID_DATE_CODE)
43 12
            ->addViolation()
44
        ;
45 12
    }
46
}
47