Passed
Pull Request — master (#316)
by Dmitriy
13:25
created

Lazy::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.1481

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 29
ccs 2
cts 3
cp 0.6667
rs 10
cc 2
nc 2
nop 4
crap 2.1481
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 Traversable;
12
use Yiisoft\Validator\BeforeValidationInterface;
13
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
14
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
15
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
16
use Yiisoft\Validator\RuleInterface;
17
use Yiisoft\Validator\RulesDumper;
18
use Yiisoft\Validator\RulesProviderInterface;
19
use Yiisoft\Validator\SerializableRuleInterface;
20
use Yiisoft\Validator\SkipOnEmptyInterface;
21
use Yiisoft\Validator\ValidationContext;
22
23
/**
24
 * Can be used for validation of nested structures.
25
 */
26
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
27
final class Lazy implements SerializableRuleInterface, BeforeValidationInterface, SkipOnEmptyInterface
28
{
29
    use BeforeValidationTrait;
30
    use RuleNameTrait;
31
    use SkipOnEmptyTrait;
32
33 5
    public function __construct(
34
        /**
35
         * Rules for validate value that can be described by:
36
         * - object that implement {@see RulesProviderInterface};
37
         * - name of class from whose attributes their will be derived;
38
         * - array or object implementing the `Traversable` interface that contain {@see RuleInterface} implementations
39
         *   or closures.
40
         *
41
         * `$rules` can be null if validatable value is object. In this case rules will be derived from object via
42
         * `getRules()` method if object implement {@see RulesProviderInterface} or from attributes otherwise.
43
         *
44
         * @var class-string|iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>|RulesProviderInterface|null
45
         */
46
        private iterable|object|string|null $rules = null,
47
48
        /**
49
         * @var bool|callable|null
50
         */
51
        private $skipOnEmpty = null,
52
        private bool $skipOnError = false,
53
54
        /**
55
         * @var Closure(mixed, ValidationContext):bool|null
56
         */
57
        private ?Closure $when = null,
58
    ) {
59 5
        if ($this->rules === []) {
60
            throw new InvalidArgumentException(
61
                'Rules for Lazy rule are required.'
62
            );
63
        }
64
    }
65
66
    /**
67
     * @return iterable<Closure|Closure[]|RuleInterface|RuleInterface[]>
68
     */
69 4
    public function getRules(): iterable
70
    {
71 4
        return $this->rules;
72
    }
73
74 1
    #[ArrayShape([
75
        'skipOnEmpty' => 'bool',
76
        'skipOnError' => 'bool',
77
        'rules' => 'array|null',
78
    ])]
79
    public function getOptions(): array
80
    {
81
        return [
82 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
83 1
            'skipOnError' => $this->skipOnError,
84 1
            'rules' => $this->rules === null ? null : (new RulesDumper())->asArray($this->rules),
85
        ];
86
    }
87
88 2
    public function getHandlerClassName(): string
89
    {
90 2
        return LazyHandler::class;
91
    }
92
}
93