Passed
Pull Request — master (#364)
by Alexander
05:03 queued 02:17
created

Composite::getOptions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4.016

Importance

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