EntityListValidator::validate()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 19
nc 1
nop 1
dl 0
loc 26
ccs 0
cts 11
cp 0
crap 6
rs 9.6333
c 1
b 0
f 0
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\Entity\EntityInterface;
13
use Daikon\Entity\EntityList;
14
use Daikon\Validize\Validator\Validator;
15
16
final class EntityListValidator extends Validator
17
{
18
    /** @param mixed $input */
19
    protected function validate($input): EntityList
20
    {
21
        $path = $this->getPath();
22
23
        Assert::that($input)
24
            ->isArray('Must be an array.')
25
            ->satisfy(function (array $items) use ($path): void {
26
                $formatAssertion = Assert::lazy();
27
                $typeAssertion = Assert::lazy();
28
                foreach ($items as $index => $item) {
29
                    $formatAssertion
30
                        ->that($item, $path)
31
                        ->isArray($path."[$index] must be an array.")
32
                        ->notEmpty($path."[$index] must not be empty.");
33
                    $typeAssertion
34
                        ->that($item, $path)
35
                        ->keyExists(
36
                            EntityInterface::TYPE_KEY,
37
                            'Required input for '.$path."[$index] '".EntityInterface::TYPE_KEY."' is missing."
38
                        );
39
                }
40
                $formatAssertion->verifyNow();
41
                $typeAssertion->verifyNow();
42
            });
43
44
        return EntityList::fromNative($input);
45
    }
46
}
47