|
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
|
|
|
|