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

MinDatetime   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 25
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 11 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
12
class MinDatetime implements ValidatorInterface
13
{
14
    public static function validate($value, array $options = [], ?Model $model = null)
15
    {
16
        $min = $options['value'];
17
        // TODO: now
18
        $dt = \DateTime::createFromFormat(\DateTime::ISO8601, $value);
19
        $min = \DateTime::createFromFormat(\DateTime::ISO8601, $min);
20
        if ($dt < $min) {
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
            'MinDatetime',
31
            "Minimum value for datetimes",
32
            [
33
                new MetadataParameter(
34
                    'value',
35
                    'Datetime',
36
                    'Value'
37
                )
38
            ]
39
        );
40
    }
41
}
42