RangeValidator::validate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 15
ccs 0
cts 8
cp 0
crap 2
rs 9.9666
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/validize project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\Validize\Validator;
10
11
use Daikon\Interop\Assert;
12
use Daikon\ValueObject\Range;
13
14
final class RangeValidator extends Validator
15
{
16
    private const SIZE_LIMIT = 50;
17
18
    /** @param mixed $input */
19
    protected function validate($input): Range
20
    {
21
        $settings = $this->getSettings();
22
        $limit = $settings['limit'] ?? self::SIZE_LIMIT;
23
24
        Assert::that($input)->string('Must be a string.')->regex('/^\[\d+,\d+\]$/', 'Invalid format.');
25
        
26
        $nativeRange = array_map('intval', explode(',', trim($input, '[]')));
27
        $range = Range::fromNative($nativeRange);
28
29
        Assert::that($range->getSize())
30
            ->greaterOrEqualThan(0, 'Range size must be greater than 0.')
31
            ->lessOrEqualThan($limit, "Range size must be at most $limit.");
32
33
        return $range;
34
    }
35
}
36