1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
10
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
11
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
12
|
|
|
use Yiisoft\Validator\SerializableRuleInterface; |
13
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
14
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
15
|
|
|
use Yiisoft\Validator\ValidationContext; |
16
|
|
|
use Yiisoft\Validator\WhenInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Checks if at least {@see AtLeast::$min} of many attributes are filled. |
20
|
|
|
*/ |
21
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
22
|
|
|
final class AtLeast implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
23
|
|
|
{ |
24
|
|
|
use SkipOnEmptyTrait; |
25
|
|
|
use SkipOnErrorTrait; |
26
|
|
|
use WhenTrait; |
27
|
|
|
|
28
|
3 |
|
public function __construct( |
29
|
|
|
/** |
30
|
|
|
* @var string[] The list of required attributes that will be checked. |
31
|
|
|
*/ |
32
|
|
|
private array $attributes, |
33
|
|
|
/** |
34
|
|
|
* @var int The minimum required quantity of filled attributes to pass the validation. Defaults to 1. |
35
|
|
|
*/ |
36
|
|
|
private int $min = 1, |
37
|
|
|
private string $incorrectInputMessage = 'Value must be an array or an object.', |
38
|
|
|
/** |
39
|
|
|
* @var string Message to display in case of error. |
40
|
|
|
*/ |
41
|
|
|
private string $message = 'The model is not valid. Must have at least "{min}" filled attributes.', |
42
|
|
|
/** |
43
|
|
|
* @var bool|callable|null |
44
|
|
|
*/ |
45
|
|
|
private mixed $skipOnEmpty = null, |
46
|
|
|
private bool $skipOnError = false, |
47
|
|
|
/** |
48
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
49
|
|
|
*/ |
50
|
|
|
private ?Closure $when = null |
51
|
|
|
) { |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
public function getName(): string |
55
|
|
|
{ |
56
|
1 |
|
return 'atLeast'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string[] |
61
|
|
|
*/ |
62
|
11 |
|
public function getAttributes(): array |
63
|
|
|
{ |
64
|
11 |
|
return $this->attributes; |
65
|
|
|
} |
66
|
|
|
|
67
|
11 |
|
public function getMin(): int |
68
|
|
|
{ |
69
|
11 |
|
return $this->min; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getIncorrectInputMessage(): string |
73
|
|
|
{ |
74
|
|
|
return $this->incorrectInputMessage; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
public function getMessage(): string |
78
|
|
|
{ |
79
|
3 |
|
return $this->message; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
public function getOptions(): array |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
2 |
|
'attributes' => $this->attributes, |
86
|
2 |
|
'min' => $this->min, |
87
|
2 |
|
'incorrectInputMessage' => $this->incorrectInputMessage, |
88
|
|
|
'message' => [ |
89
|
2 |
|
'message' => $this->message, |
90
|
2 |
|
'parameters' => ['min' => $this->min], |
91
|
|
|
], |
92
|
2 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
93
|
2 |
|
'skipOnError' => $this->skipOnError, |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
11 |
|
public function getHandlerClassName(): string |
98
|
|
|
{ |
99
|
11 |
|
return AtLeastHandler::class; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|