Passed
Push — master ( 84fd91...b967da )
by Bruno
07:22
created

Regex::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
nc 1
nop 0
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\Model;
7
use Formularium\ValidatorArgs;
8
use Formularium\ValidatorInterface;
9
use Formularium\ValidatorMetadata;
10
use Formularium\Exception\ValidatorException;
11
12
class Regex implements ValidatorInterface
13
{
14
    /**
15
     * Checks if $value is a valid value for this datatype considering the validators.
16
     *
17
     * @param mixed $value
18
     * @param array $options The options passed to the validator
19
     * @param Datatype $datatype The datatype being validator.
20
     * @param Model $model The entire model, if you your field depends on other things of the model. may be null.
21
     * @throws ValidatorException If invalid, with the message.
22
     * @return mixed
23
     */
24
    public static function validate($value, array $options = [], Datatype $datatype, ?Model $model = null)
25
    {
26
        if (preg_match($options['value'], $value)) {
27
            return $value;
28
        }
29
        throw new ValidatorException('Value does not match expected regular expression');
30
    }
31
32
    /**
33
     * Documents this validator.
34
     *
35
     * @return ValidatorMetadata
36
     */
37
    public static function getMetadata(): ValidatorMetadata
38
    {
39
        return new ValidatorMetadata(
40
            'Regex',
41
            "Regular expression validator",
42
            [
43
                new ValidatorArgs(
44
                    'value',
45
                    'String',
46
                    'Regular expression, PHP style'
47
                )
48
            ]
49
        );
50
    }
51
}
52