Passed
Push — master ( e674b7...776652 )
by Stanislau
04:06
created

AttributeValidationProcessor::process()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 4
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Library\DTO\Preparation\Processor\Property;
15
16
use Micro\Library\DTO\ClassDef\ClassDefinition;
17
use Micro\Library\DTO\ClassDef\PropertyDefinition;
18
19
class AttributeValidationProcessor implements PropertyProcessorInterface
20
{
21
    /**
22
     * @param iterable<PropertyProcessorInterface> $validatorProcessor
23
     */
24 6
    public function __construct(
25
        private iterable $validatorProcessor
26
    ) {
27 6
    }
28
29 1
    public function process(PropertyDefinition $propertyDefinition, ClassDefinition $classDefinition, array $propertyData, array $classList): void
30
    {
31 1
        if (!\array_key_exists('validation', $propertyData)) {
32 1
            return;
33
        }
34
35 1
        $propertyValidationConstraints = $propertyData['validation'];
36 1
        foreach ($propertyValidationConstraints as $constraint) {
37 1
            $this->processAddConstraints($propertyDefinition, $classDefinition, $constraint, $classList);
38
        }
39
    }
40
41
    /**
42
     * @param PropertyDefinition          $propertyDefinition
43
     * @param ClassDefinition             $classDefinition
44
     * @param array<array<string, mixed>> $constraints
45
     * @param string[]                    $classList
46
     *
47
     * @return void
48
     */
49 1
    protected function processAddConstraints(PropertyDefinition $propertyDefinition, ClassDefinition $classDefinition, array $constraints, array $classList): void
50
    {
51 1
        foreach ($constraints as $constraint) {
52 1
            foreach ($this->validatorProcessor as $processor) {
53
                /**
54
                 * @phpstan-ignore-next-line
55
                 *
56
                 * @psalm-suppress InvalidArrayOffset
57
                 */
58 1
                $processor->process($propertyDefinition, $classDefinition, [$constraint[0] => $constraint[1]], $classList);
59
            }
60
        }
61
    }
62
}
63