Passed
Push — master ( f53c15...e7ac2a )
by Bruno
09:12
created

Match::getMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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\ValidatorMetadata;
10
use Formularium\ValidatorArgs;
11
12
class Match implements ValidatorInterface
13
{
14
    public static function validate($value, array $options = [], Datatype $datatype, ?Model $model = null)
15
    {
16
        $ret = preg_match($options['regex'], $value);
17
        if ($ret === false) {
18
            throw new ValidatorException('Error, invalid regex.');
19
        } elseif ($ret === 0) {
20
            throw new ValidatorException('Value does not match regex.');
21
        }
22
23
        return $value;
24
    }
25
26
    public static function getMetadata(): ValidatorMetadata
27
    {
28
        return new ValidatorMetadata(
29
            'Match',
30
            "Matches against a regex",
31
            [
32
                new ValidatorArgs(
33
                    'value',
34
                    'String',
35
                    'Value'
36
                )
37
            ]
38
        );
39
    }
40
}
41