Passed
Push — master ( 68d9d2...a847ca )
by Bruno
05:30
created

MaxDatetime   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 24
rs 10
c 1
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetadata() 0 10 1
A validate() 0 10 2
1
<?php declare(strict_types=1);
2
3
namespace Formularium\Validator;
4
5
use Formularium\Datatype;
6
use Formularium\Exception\ValidatorException;
7
use Formularium\Model;
8
use Formularium\ValidatorInterface;
9
use Formularium\Metadata;
10
use Formularium\MetadataParameter;
11
use Respect\Validation\Validator as Respect;
12
13
class MaxDatetime implements ValidatorInterface
14
{
15
    public static function validate($value, array $options = [], ?Model $model = null)
16
    {
17
        $max = $options['value'];
18
        $dt = \DateTime::createFromFormat(\DateTime::ISO8601, $value);
19
        $max = \DateTime::createFromFormat(\DateTime::ISO8601, $max);
20
        if ($dt > $max) {
21
            throw new ValidatorException('Value is too small.');
22
        }
23
24
        return $value;
25
    }
26
27
    public static function getMetadata(): Metadata
28
    {
29
        return new Metadata(
30
            'MaxDatetime',
31
            "Maximum datetime value",
32
            [
33
                new MetadataParameter(
34
                    'value',
35
                    'Datetime',
36
                    'Value'
37
                )
38
            ]
39
        );
40
    }
41
}
42