Passed
Pull Request — master (#364)
by
unknown
03:05
created

StopOnError::getName()   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 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
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 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\RulesDumper;
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
 * Can be used for validation of nested structures.
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25
final class StopOnError implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
26
{
27
    use SkipOnEmptyTrait;
28
    use SkipOnErrorTrait;
29
    use WhenTrait;
30
31 3
    public function __construct(
32
        /**
33
         * @var iterable<RuleInterface>
34
         */
35
        private iterable $rules,
36
        /**
37
         * @var bool|callable|null
38
         */
39
        private mixed $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 'stopOnError';
51
    }
52
53
    /**
54
     * @return iterable<RuleInterface>
55
     */
56 3
    public function getRules(): iterable
57
    {
58 3
        return $this->rules;
59
    }
60
61 1
    #[ArrayShape([
62
        'skipOnEmpty' => 'bool',
63
        'skipOnError' => 'bool',
64
        'rules' => 'array|null',
65
    ])]
66
    public function getOptions(): array
67
    {
68
        return [
69 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
70 1
            'skipOnError' => $this->skipOnError,
71 1
            'rules' => (new RulesDumper())->asArray($this->rules),
72
        ];
73
    }
74
75 3
    public function getHandlerClassName(): string
76
    {
77 3
        return StopOnErrorHandler::class;
78
    }
79
}
80