Passed
Pull Request — master (#369)
by
unknown
02:46
created

Composite::getRules()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
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 InvalidArgumentException;
10
use JetBrains\PhpStorm\ArrayShape;
11
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
12
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
13
use Yiisoft\Validator\Rule\Trait\WhenTrait;
14
use Yiisoft\Validator\RuleInterface;
15
use Yiisoft\Validator\SerializableRuleInterface;
16
use Yiisoft\Validator\SkipOnEmptyInterface;
17
use Yiisoft\Validator\SkipOnErrorInterface;
18
use Yiisoft\Validator\ValidationContext;
19
use Yiisoft\Validator\WhenInterface;
20
21
/**
22
 * Allows to combine and validate multiple rules.
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25
class Composite implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
26
{
27
    use SkipOnEmptyTrait;
28
    use SkipOnErrorTrait;
29
    use WhenTrait;
30
31 4
    public function __construct(
32
        /**
33
         * @var iterable<RuleInterface>
34
         */
35
        private iterable $rules = [],
36
37
        /**
38
         * @var bool|callable|null
39
         */
40
        private $skipOnEmpty = null,
41
        private bool $skipOnError = false,
42
        /**
43
         * @var Closure(mixed, ValidationContext):bool|null
44
         */
45
        private ?Closure $when = null,
46
    ) {
47
    }
48
49 1
    public function getName(): string
50
    {
51 1
        return 'composite';
52
    }
53
54 3
    #[ArrayShape([
55
        'skipOnEmpty' => 'bool',
56
        'skipOnError' => 'bool',
57
        'rules' => 'array',
58
    ])]
59
    public function getOptions(): array
60
    {
61 3
        $arrayOfRules = [];
62 3
        foreach ($this->getRules() as $rule) {
63 3
            if (!$rule instanceof RuleInterface) {
64 1
                throw new InvalidArgumentException('A rule must be an instance of RuleInterface.');
65
            }
66
67 3
            $nameArray = [$rule->getName()];
68
69 3
            if ($rule instanceof SerializableRuleInterface) {
70 3
                $arrayOfRules[] = array_merge($nameArray, $rule->getOptions());
71
            } else {
72 1
                $arrayOfRules[] = $nameArray;
73
            }
74
        }
75
76
        return [
77 2
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
78 2
            'skipOnError' => $this->skipOnError,
79
            'rules' => $arrayOfRules,
80
        ];
81
    }
82
83
    /**
84
     * @return iterable<\Closure|\Closure[]|RuleInterface|RuleInterface[]>
85
     */
86 5
    public function getRules(): iterable
87
    {
88 5
        return $this->rules;
89
    }
90
91 3
    public function getHandlerClassName(): string
92
    {
93 3
        return CompositeHandler::class;
94
    }
95
}
96