Test Failed
Pull Request — master (#244)
by Dmitriy
02:50
created

Composite::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 12
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 4
crap 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