Passed
Pull Request — master (#245)
by Rustam
12:07
created

Composite   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
c 1
b 0
f 0
dl 0
loc 40
ccs 9
cts 10
cp 0.9
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A getOptions() 0 11 3
A getRules() 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 Yiisoft\Validator\BeforeValidationInterface;
10
use Yiisoft\Validator\ParametrizedRuleInterface;
11
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
12
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
13
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
14
use Yiisoft\Validator\RuleInterface;
15
use Yiisoft\Validator\ValidationContext;
16
17
/**
18
 * Validates that the value is a valid json.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class Composite implements ParametrizedRuleInterface, BeforeValidationInterface
22
{
23
    use BeforeValidationTrait;
24
    use HandlerClassNameTrait;
25
    use RuleNameTrait;
26
27 1
    public function __construct(
28
        /**
29
         * @var iterable<RuleInterface>
30
         */
31
        private iterable $rules = [],
32
        private bool $skipOnEmpty = false,
33
        private bool $skipOnError = false,
34
        /**
35
         * @var Closure(mixed, ValidationContext):bool|null
36
         */
37
        private ?Closure $when = null,
38
    ) {
39
    }
40
41 1
    public function getOptions(): array
42
    {
43 1
        $arrayOfRules = [];
44 1
        foreach ($this->rules as $rule) {
45 1
            if ($rule instanceof ParametrizedRuleInterface) {
46 1
                $arrayOfRules[] = array_merge([$rule->getName()], $rule->getOptions());
47
            } else {
48
                $arrayOfRules[] = [$rule->getName()];
49
            }
50
        }
51 1
        return $arrayOfRules;
52
    }
53
54
    /**
55
     * @return iterable<RuleInterface>
56
     */
57 2
    public function getRules(): iterable
58
    {
59 2
        return $this->rules;
60
    }
61
}
62