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