MaxLength   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 94.12%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 16
c 1
b 0
f 0
dl 0
loc 27
ccs 16
cts 17
cp 0.9412
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 13 4
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\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