|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Attribute; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use Countable; |
|
10
|
|
|
use Yiisoft\Validator\LimitInterface; |
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\LimitTrait; |
|
12
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
|
13
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
|
14
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
|
15
|
|
|
use Yiisoft\Validator\SerializableRuleInterface; |
|
16
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
|
17
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
|
18
|
|
|
use Yiisoft\Validator\ValidationContext; |
|
19
|
|
|
use Yiisoft\Validator\WhenInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Validates that the value contains certain number of items. Can be applied to arrays or classes implementing |
|
23
|
|
|
* {@see Countable} interface. |
|
24
|
|
|
*/ |
|
25
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
26
|
|
|
final class Count implements |
|
27
|
|
|
SerializableRuleInterface, |
|
28
|
|
|
SkipOnErrorInterface, |
|
29
|
|
|
WhenInterface, |
|
30
|
|
|
SkipOnEmptyInterface, |
|
31
|
|
|
LimitInterface |
|
32
|
|
|
{ |
|
33
|
|
|
use LimitTrait; |
|
34
|
|
|
use SkipOnEmptyTrait; |
|
35
|
|
|
use SkipOnErrorTrait; |
|
36
|
|
|
use WhenTrait; |
|
37
|
|
|
|
|
38
|
18 |
|
public function __construct( |
|
39
|
|
|
/** |
|
40
|
|
|
* @var int|null minimum number of items. null means no minimum number limit. Can't be combined with |
|
41
|
|
|
* {@see $exactly}. |
|
42
|
|
|
* |
|
43
|
|
|
* @see $lessThanMinMessage for the customized message for a value with too few items. |
|
44
|
|
|
*/ |
|
45
|
|
|
?int $min = null, |
|
46
|
|
|
/** |
|
47
|
|
|
* @var int|null maximum number of items. null means no maximum number limit. Can't be combined with |
|
48
|
|
|
* {@see $exactly}. |
|
49
|
|
|
* |
|
50
|
|
|
* @see $greaterThanMaxMessage for the customized message for a value wuth too many items. |
|
51
|
|
|
*/ |
|
52
|
|
|
?int $max = null, |
|
53
|
|
|
/** |
|
54
|
|
|
* @var int|null exact number of items. `null` means no strict comparison. Mutually exclusive with {@see $min} |
|
55
|
|
|
* and {@see $max}. |
|
56
|
|
|
*/ |
|
57
|
|
|
?int $exactly = null, |
|
58
|
|
|
/** |
|
59
|
|
|
* @var string user-defined error message used when the value is neither an array nor implementing |
|
60
|
|
|
* {@see \Countable} interface. |
|
61
|
|
|
* |
|
62
|
|
|
* @see Countable |
|
63
|
|
|
*/ |
|
64
|
|
|
private string $incorrectInputMessage = 'This value must be an array or implement \Countable interface.', |
|
65
|
|
|
/** |
|
66
|
|
|
* @var string user-defined error message used when the number of items is smaller than {@see $min}. |
|
67
|
|
|
*/ |
|
68
|
|
|
string $lessThanMinMessage = 'This value must contain at least {min, number} {min, plural, one{item} ' . |
|
69
|
|
|
'other{items}}.', |
|
70
|
|
|
/** |
|
71
|
|
|
* @var string user-defined error message used when the number of items is greater than {@see $max}. |
|
72
|
|
|
*/ |
|
73
|
|
|
string $greaterThanMaxMessage = 'This value must contain at most {max, number} {max, plural, one{item} ' . |
|
74
|
|
|
'other{items}}.', |
|
75
|
|
|
/** |
|
76
|
|
|
* @var string user-defined error message used when the number of items does not equal {@see $exactly}. |
|
77
|
|
|
*/ |
|
78
|
|
|
string $notExactlyMessage = 'This value must contain exactly {exactly, number} {exactly, plural, one{item} ' . |
|
79
|
|
|
'other{items}}.', |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var bool|callable|null |
|
83
|
|
|
*/ |
|
84
|
|
|
private $skipOnEmpty = null, |
|
85
|
|
|
private bool $skipOnError = false, |
|
86
|
|
|
/** |
|
87
|
|
|
* @var Closure(mixed, ValidationContext):bool|null |
|
88
|
|
|
*/ |
|
89
|
|
|
private ?Closure $when = null, |
|
90
|
|
|
) { |
|
91
|
18 |
|
$this->initLimitProperties( |
|
92
|
|
|
$min, |
|
93
|
|
|
$max, |
|
94
|
|
|
$exactly, |
|
95
|
|
|
$lessThanMinMessage, |
|
96
|
|
|
$greaterThanMaxMessage, |
|
97
|
|
|
$notExactlyMessage |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
public function getName(): string |
|
102
|
|
|
{ |
|
103
|
1 |
|
return 'count'; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
5 |
|
public function getIncorrectInputMessage(): string |
|
107
|
|
|
{ |
|
108
|
5 |
|
return $this->incorrectInputMessage; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
public function getOptions(): array |
|
112
|
|
|
{ |
|
113
|
1 |
|
return array_merge($this->getLimitOptions(), [ |
|
114
|
|
|
'incorrectInputMessage' => [ |
|
115
|
1 |
|
'message' => $this->getIncorrectInputMessage(), |
|
116
|
|
|
], |
|
117
|
1 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
|
118
|
1 |
|
'skipOnError' => $this->skipOnError, |
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
34 |
|
public function getHandlerClassName(): string |
|
123
|
|
|
{ |
|
124
|
34 |
|
return CountHandler::class; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|