Passed
Push — master ( 8efd82...c644e1 )
by Alexander
02:39
created

Composite   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Test Coverage

Coverage 92.31%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 20
c 1
b 0
f 0
dl 0
loc 54
ccs 12
cts 13
cp 0.9231
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 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\SerializableRuleInterface;
12
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
13
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
14
use Yiisoft\Validator\RuleInterface;
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 2
    public function __construct(
27
        /**
28
         * @var iterable<RuleInterface>
29
         */
30
        private iterable $rules = [],
31
        private bool $skipOnEmpty = false,
32
        private bool $skipOnError = false,
33
        /**
34
         * @var Closure(mixed, ValidationContext):bool|null
35
         */
36
        private ?Closure $when = null,
37
    ) {
38
    }
39
40 1
    #[ArrayShape([
41
        'skipOnEmpty' => 'bool',
42
        'skipOnError' => 'bool',
43
        'rules' => 'array',
44
    ])]
45
    public function getOptions(): array
46
    {
47 1
        $arrayOfRules = [];
48 1
        foreach ($this->getRules() as $rule) {
49 1
            if ($rule instanceof SerializableRuleInterface) {
50 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
51
            } else {
52
                $arrayOfRules[] = [$rule->getName()];
53
            }
54
        }
55
56
        return [
57 1
            'skipOnEmpty' => $this->skipOnEmpty,
58 1
            'skipOnError' => $this->skipOnError,
59
            'rules' => $arrayOfRules,
60
        ];
61
    }
62
63
    /**
64
     * @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>
65
     */
66 3
    public function getRules(): iterable
67
    {
68 3
        return $this->rules;
69
    }
70
71 1
    public function getHandlerClassName(): string
72
    {
73 1
        return CompositeHandler::class;
74
    }
75
}
76