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

Match   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 10 3
A getMetadata() 0 10 1
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