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
|
|
|
/** |
47
|
|
|
* @var bool |
48
|
|
|
*/ |
49
|
|
|
private bool $skipOnError = false, |
50
|
|
|
/** |
51
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
52
|
|
|
*/ |
53
|
|
|
private ?Closure $when = null |
54
|
|
|
) { |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function getName(): string |
58
|
|
|
{ |
59
|
1 |
|
return 'atLeast'; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string[] |
64
|
|
|
*/ |
65
|
11 |
|
public function getAttributes(): array |
66
|
|
|
{ |
67
|
11 |
|
return $this->attributes; |
68
|
|
|
} |
69
|
|
|
|
70
|
11 |
|
public function getMin(): int |
71
|
|
|
{ |
72
|
11 |
|
return $this->min; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getIncorrectInputMessage(): string |
76
|
|
|
{ |
77
|
|
|
return $this->incorrectInputMessage; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
public function getMessage(): string |
81
|
|
|
{ |
82
|
3 |
|
return $this->message; |
83
|
|
|
} |
84
|
|
|
|
85
|
2 |
|
public function getOptions(): array |
86
|
|
|
{ |
87
|
|
|
return [ |
88
|
2 |
|
'attributes' => $this->attributes, |
89
|
2 |
|
'min' => $this->min, |
90
|
2 |
|
'incorrectInputMessage' => $this->incorrectInputMessage, |
91
|
|
|
'message' => [ |
92
|
2 |
|
'message' => $this->message, |
93
|
2 |
|
'parameters' => ['min' => $this->min], |
94
|
|
|
], |
95
|
2 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
96
|
2 |
|
'skipOnError' => $this->skipOnError, |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
|
100
|
11 |
|
public function getHandlerClassName(): string |
101
|
|
|
{ |
102
|
11 |
|
return AtLeastHandler::class; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|