|
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\AfterInitAttributeEventInterface; |
|
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\RuleWithOptionsInterface; |
|
17
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
18
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
19
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Can be used for validation of nested structures. |
|
23
|
|
|
* |
|
24
|
|
|
* @psalm-import-type WhenType from WhenInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
27
|
|
|
final class StopOnError implements |
|
28
|
|
|
RuleWithOptionsInterface, |
|
29
|
|
|
SkipOnErrorInterface, |
|
30
|
|
|
WhenInterface, |
|
31
|
3 |
|
SkipOnEmptyInterface, |
|
32
|
|
|
AfterInitAttributeEventInterface |
|
33
|
|
|
{ |
|
34
|
|
|
use SkipOnEmptyTrait; |
|
35
|
|
|
use SkipOnErrorTrait; |
|
36
|
|
|
use WhenTrait; |
|
37
|
|
|
|
|
38
|
|
|
private ?RulesDumper $rulesDumper = null; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct( |
|
41
|
|
|
/** |
|
42
|
|
|
* @var iterable<RuleInterface> |
|
43
|
|
|
*/ |
|
44
|
|
|
private iterable $rules, |
|
45
|
|
|
/** |
|
46
|
|
|
* @var bool|callable|null |
|
47
|
|
|
*/ |
|
48
|
1 |
|
private mixed $skipOnEmpty = null, |
|
49
|
|
|
private bool $skipOnError = false, |
|
50
|
1 |
|
/** |
|
51
|
|
|
* @var WhenType |
|
52
|
|
|
*/ |
|
53
|
|
|
private Closure|null $when = null, |
|
54
|
|
|
) { |
|
55
|
|
|
} |
|
56
|
3 |
|
|
|
57
|
|
|
public function getName(): string |
|
58
|
3 |
|
{ |
|
59
|
|
|
return 'stopOnError'; |
|
60
|
|
|
} |
|
61
|
1 |
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return iterable<RuleInterface> |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getRules(): iterable |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->rules; |
|
68
|
|
|
} |
|
69
|
1 |
|
|
|
70
|
1 |
|
#[ArrayShape([ |
|
71
|
1 |
|
'skipOnEmpty' => 'bool', |
|
72
|
|
|
'skipOnError' => 'bool', |
|
73
|
|
|
'rules' => 'array|null', |
|
74
|
|
|
])] |
|
75
|
3 |
|
public function getOptions(): array |
|
76
|
|
|
{ |
|
77
|
3 |
|
return [ |
|
78
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
79
|
|
|
'skipOnError' => $this->skipOnError, |
|
80
|
|
|
'rules' => $this->getRulesDumper()->asArray($this->rules), |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function getHandlerClassName(): string |
|
85
|
|
|
{ |
|
86
|
|
|
return StopOnErrorHandler::class; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function afterInitAttribute(object $object, int $target): void |
|
90
|
|
|
{ |
|
91
|
|
|
foreach ($this->rules as $rule) { |
|
92
|
|
|
if ($rule instanceof AfterInitAttributeEventInterface) { |
|
93
|
|
|
$rule->afterInitAttribute($object, $target); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function getRulesDumper(): RulesDumper |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->rulesDumper === null) { |
|
101
|
|
|
$this->rulesDumper = new RulesDumper(); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->rulesDumper; |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|