TextListValidator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 31
ccs 0
cts 14
cp 0
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 24 3
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\Interop\Assertion;
13
use Daikon\ValueObject\TextList;
14
15
final class TextListValidator extends Validator
16
{
17
    private const MAX_SIZE = 0;
18
    private const MIN_TEXT_LEN = 1;
19
    private const MAX_TEXT_LEN = 80;
20
21
    /** @param mixed $input */
22
    protected function validate($input): TextList
23
    {
24
        Assertion::isArray($input, 'Must be an array.');
25
26
        $path = $this->getPath();
27
        $settings = $this->getSettings();
28
        $size = $settings['size'] ?? self::MAX_SIZE;
29
        $min = $settings['min'] ?? self::MIN_TEXT_LEN;
30
        $max = $settings['max'] ?? self::MAX_TEXT_LEN;
31
32
        if ($size > 0) {
33
            Assertion::maxCount($input, $size, "Must have at most $size items.");
34
        }
35
36
        $formatAssertion = Assert::lazy();
37
        foreach ($input as $index => $item) {
38
            $formatAssertion
39
                ->that($item, $path)
40
                ->string($path."[$index] must be a string.")
41
                ->betweenLength($min, $max, $path."[$index] must be between $min and $max characters.");
42
        }
43
        $formatAssertion->verifyNow();
44
45
        return TextList::fromNative($input);
46
    }
47
}
48