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

MaxDate::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
nc 2
nop 3
dl 0
loc 10
rs 10
c 1
b 0
f 0
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 MaxDate implements ValidatorInterface
14
{
15
    public static function validate($value, array $options = [], ?Model $model = null)
16
    {
17
        $max = $options['value'];
18
        $val = Respect::date('Y-m-d');
19
        $val->max($max);
20
        if (!$val->validate($value)) {
21
            throw new ValidatorException('Value is too large.');
22
        }
23
24
        return $value;
25
    }
26
27
    public static function getMetadata(): Metadata
28
    {
29
        return new Metadata(
30
            'MaxDate',
31
            "Maximum date value",
32
            [
33
                new MetadataParameter(
34
                    'value',
35
                    'Date',
36
                    'Value'
37
                )
38
            ]
39
        );
40
    }
41
}
42