ArrayValidator::validate()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 22
ccs 0
cts 11
cp 0
crap 6
rs 9.7666
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
14
final class ArrayValidator extends Validator
15
{
16
    /** @param mixed $input */
17
    protected function validate($input): array
18
    {
19
        $path = $this->getPath();
20
        $settings = $this->getSettings();
21
        $values = $settings['values'] ?? [];
22
23
        Assertion::notEmpty($values, 'Permitted values must be specified.');
24
25
        Assert::that($input)
26
            ->isArray('Must be an array.')
27
            ->satisfy(function (array $items) use ($path, $values): void {
28
                $formatAssertion = Assert::lazy();
29
                foreach ($items as $key => $item) {
30
                    $formatAssertion
31
                        ->that($item, $path)
32
                        ->notBlank($path."[$key] must not be blank.")
33
                        ->inArray($values, $path."[$key] '$item' is not a valid value.");
34
                }
35
                $formatAssertion->verifyNow();
36
            });
37
38
        return $input;
39
    }
40
}
41