MaxLength::getMetadata()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
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\Field;
8
use Formularium\Model;
9
use Formularium\ValidatorInterface;
10
use Formularium\Metadata;
11
use Formularium\MetadataParameter;
12
13
class MaxLength implements ValidatorInterface
14
{
15 38
    public static function validate($value, array $options = [], ?Model $model = null)
16
    {
17 38
        if ($value === null) {
18 4
            return $value;
19
        }
20 38
        if (!is_string($value)) {
21
            throw new ValidatorException('Expected a string.');
22
        }
23 38
        $maxlength = $options['value'];
24 38
        if (mb_strlen($value) > $maxlength) {
25 1
            throw new ValidatorException('String is too long.');
26
        }
27 37
        return $value;
28
    }
29
30 1
    public static function getMetadata(): Metadata
31
    {
32 1
        return new Metadata(
33 1
            'MaxLength',
34 1
            "Maximum string length",
35
            [
36 1
                new MetadataParameter(
37 1
                    'value',
38 1
                    'Int',
39 1
                    'Value'
40
                )
41
            ]
42
        );
43
    }
44
}
45