Passed
Push — master ( cd2db1...fdabae )
by Alexander
02:29
created

Composite   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 21
c 1
b 0
f 0
dl 0
loc 59
ccs 13
cts 14
cp 0.9286
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A getOptions() 0 20 3
A getRules() 0 3 1
A getHandlerClassName() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use JetBrains\PhpStorm\ArrayShape;
10
use Yiisoft\Validator\BeforeValidationInterface;
11
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
use Yiisoft\Validator\RuleInterface;
14
use Yiisoft\Validator\SerializableRuleInterface;
15
use Yiisoft\Validator\ValidationContext;
16
17
/**
18
 * Allows to combine and validate multiple rules.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
21
class Composite implements SerializableRuleInterface, BeforeValidationInterface
22
{
23
    use BeforeValidationTrait;
24
    use RuleNameTrait;
25
26 3
    public function __construct(
27
        /**
28
         * @var iterable<RuleInterface>
29
         */
30
        private iterable $rules = [],
31
        private bool $skipOnEmpty = false,
32
        /**
33
         * @var callable
34
         */
35
        private $skipOnEmptyCallback = null,
36
        private bool $skipOnError = false,
37
        /**
38
         * @var Closure(mixed, ValidationContext):bool|null
39
         */
40
        private ?Closure $when = null,
41
    ) {
42 3
        $this->initSkipOnEmptyProperties($skipOnEmpty, $skipOnEmptyCallback);
43
    }
44
45 1
    #[ArrayShape([
46
        'skipOnEmpty' => 'bool',
47
        'skipOnError' => 'bool',
48
        'rules' => 'array',
49
    ])]
50
    public function getOptions(): array
51
    {
52 1
        $arrayOfRules = [];
53 1
        foreach ($this->getRules() as $rule) {
54 1
            if ($rule instanceof SerializableRuleInterface) {
55 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
56
            } else {
57
                $arrayOfRules[] = [$rule->getName()];
58
            }
59
        }
60
61
        return [
62 1
            'skipOnEmpty' => $this->skipOnEmpty,
63 1
            'skipOnError' => $this->skipOnError,
64
            'rules' => $arrayOfRules,
65
        ];
66
    }
67
68
    /**
69
     * @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>
70
     */
71 3
    public function getRules(): iterable
72
    {
73 3
        return $this->rules;
74
    }
75
76 1
    public function getHandlerClassName(): string
77
    {
78 1
        return CompositeHandler::class;
79
    }
80
}
81