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 StopOnError 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 StopOnError rule are required.' |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return class-string|iterable|RulesProviderInterface|null |
|
|
|
|
68
|
|
|
* |
69
|
|
|
* @psalm-return RulesProviderInterface|class-string|iterable<mixed, Closure|RuleInterface|array<Closure|RuleInterface>>|null |
70
|
|
|
*/ |
71
|
4 |
|
public function getRules(): iterable|string|RulesProviderInterface|null |
72
|
|
|
{ |
73
|
4 |
|
return $this->rules; |
74
|
|
|
} |
75
|
|
|
|
76
|
1 |
|
#[ArrayShape([ |
77
|
|
|
'skipOnEmpty' => 'bool', |
78
|
|
|
'skipOnError' => 'bool', |
79
|
|
|
'rules' => 'array|null', |
80
|
|
|
])] |
81
|
|
|
public function getOptions(): array |
82
|
|
|
{ |
83
|
|
|
return [ |
84
|
1 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
85
|
1 |
|
'skipOnError' => $this->skipOnError, |
86
|
1 |
|
'rules' => $this->rules === null ? null : (new RulesDumper())->asArray($this->rules), |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
public function getHandlerClassName(): string |
91
|
|
|
{ |
92
|
2 |
|
return StopOnErrorHandler::class; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|