Passed
Pull Request — master (#364)
by
unknown
02:53
created

StopOnError::getHandlerClassName()   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\RulesDumper;
16
use Yiisoft\Validator\RulesProviderInterface;
17
use Yiisoft\Validator\SerializableRuleInterface;
18
use Yiisoft\Validator\SkipOnEmptyInterface;
19
use Yiisoft\Validator\SkipOnErrorInterface;
20
use Yiisoft\Validator\ValidationContext;
21
use Yiisoft\Validator\WhenInterface;
22
23
/**
24
 * Can be used for validation of nested structures.
25
 */
26
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
27
final class StopOnError implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
28
{
29
    use SkipOnEmptyTrait;
30
    use SkipOnErrorTrait;
31
    use WhenTrait;
32
33 3
    public function __construct(
34
        /**
35
         * @var iterable<RuleInterface>
36
         */
37
        private iterable $rules,
38
        /**
39
         * @var bool|callable|null
40
         */
41
        private mixed $skipOnEmpty = null,
42
        private bool $skipOnError = false,
43
        /**
44
         * @var Closure(mixed, ValidationContext):bool|null
45
         */
46
        private ?Closure $when = null,
47
    ) {
48
    }
49
50 1
    public function getName(): string
51
    {
52 1
        return 'stopOnError';
53
    }
54
55
    /**
56
     * @return iterable<RuleInterface>
57
     */
58 3
    public function getRules(): iterable
59
    {
60 3
        return $this->rules;
61
    }
62
63 1
    #[ArrayShape([
64
        'skipOnEmpty' => 'bool',
65
        'skipOnError' => 'bool',
66
        'rules' => 'array|null',
67
    ])]
68
    public function getOptions(): array
69
    {
70
        return [
71 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
72 1
            'skipOnError' => $this->skipOnError,
73 1
            'rules' => (new RulesDumper())->asArray($this->rules),
74
        ];
75
    }
76
77 3
    public function getHandlerClassName(): string
78
    {
79 3
        return StopOnErrorHandler::class;
80
    }
81
}
82