|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule\Count; |
|
6
|
|
|
|
|
7
|
|
|
use Attribute; |
|
8
|
|
|
use Closure; |
|
9
|
|
|
use Countable; |
|
10
|
|
|
use InvalidArgumentException; |
|
11
|
|
|
use Yiisoft\Validator\Rule\RuleNameTrait; |
|
12
|
|
|
use Yiisoft\Validator\Rule\HandlerClassNameTrait; |
|
13
|
|
|
use Yiisoft\Validator\RuleInterface; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Validates that the value contains certain number of items. Can be applied to arrays or classes implementing |
|
17
|
|
|
* {@see Countable} interface. |
|
18
|
|
|
*/ |
|
19
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
|
20
|
|
|
final class Count implements RuleInterface |
|
21
|
|
|
{ |
|
22
|
|
|
use HandlerClassNameTrait; |
|
23
|
|
|
use RuleNameTrait; |
|
24
|
|
|
|
|
25
|
6 |
|
public function __construct( |
|
26
|
|
|
/** |
|
27
|
|
|
* @var int|null minimum number of items. null means no minimum number limit. |
|
28
|
|
|
* |
|
29
|
|
|
* @see $tooFewItemsMessage for the customized message for a value with too few items. |
|
30
|
|
|
*/ |
|
31
|
|
|
public ?int $min = null, |
|
32
|
|
|
/** |
|
33
|
|
|
* @var int|null maximum number of items. null means no maximum number limit. |
|
34
|
|
|
* |
|
35
|
|
|
* @see $tooManyItemsMessage for the customized message for a value wuth too many items. |
|
36
|
|
|
*/ |
|
37
|
|
|
public ?int $max = null, |
|
38
|
|
|
/** |
|
39
|
|
|
* @var int|null exact number of items. null means no strict comparison. Mutually exclusive with {@see $min} and |
|
40
|
|
|
* {@see $max}. |
|
41
|
|
|
*/ |
|
42
|
|
|
public ?int $exactly = null, |
|
43
|
|
|
/** |
|
44
|
|
|
* @var string user-defined error message used when the value is neither an array nor implementing |
|
45
|
|
|
* {@see \Countable} interface. |
|
46
|
|
|
* |
|
47
|
|
|
* @see Countable |
|
48
|
|
|
*/ |
|
49
|
|
|
public string $message = 'This value must be an array or implement \Countable interface.', |
|
50
|
|
|
/** |
|
51
|
|
|
* @var string user-defined error message used when the number of items is smaller than {@see $min}. |
|
52
|
|
|
*/ |
|
53
|
|
|
public string $tooFewItemsMessage = 'This value must contain at least {min, number} ' . |
|
54
|
|
|
'{min, plural, one{item} other{items}}.', |
|
55
|
|
|
/** |
|
56
|
|
|
* @var string user-defined error message used when the number of items is greater than {@see $max}. |
|
57
|
|
|
*/ |
|
58
|
|
|
public string $tooManyItemsMessage = 'This value must contain at most {max, number} ' . |
|
59
|
|
|
'{max, plural, one{item} other{items}}.', |
|
60
|
|
|
/** |
|
61
|
|
|
* @var string user-defined error message used when the number of items does not equal {@see $exactly}. |
|
62
|
|
|
*/ |
|
63
|
|
|
public string $notExactlyMessage = 'This value must contain exactly {max, number} ' . |
|
64
|
|
|
'{max, plural, one{item} other{items}}.', |
|
65
|
|
|
public bool $skipOnEmpty = false, |
|
66
|
|
|
public bool $skipOnError = false, |
|
67
|
|
|
public ?Closure $when = null, |
|
68
|
|
|
) { |
|
69
|
6 |
|
if (!$this->min && !$this->max && !$this->exactly) { |
|
|
|
|
|
|
70
|
1 |
|
throw new InvalidArgumentException( |
|
71
|
|
|
'At least one of these attributes must be specified: $min, $max, $exactly.' |
|
72
|
|
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
5 |
|
if ($this->exactly && ($this->min || $this->max)) { |
|
|
|
|
|
|
76
|
3 |
|
throw new InvalidArgumentException('$exactly is mutually exclusive with $min and $max.'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
2 |
|
if ($this->min && $this->max && $this->min === $this->max) { |
|
|
|
|
|
|
80
|
1 |
|
throw new InvalidArgumentException('Use $exactly instead.'); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function getOptions(): array |
|
85
|
|
|
{ |
|
86
|
|
|
return [ |
|
87
|
1 |
|
'min' => $this->min, |
|
88
|
1 |
|
'max' => $this->max, |
|
89
|
1 |
|
'exactly' => $this->exactly, |
|
90
|
|
|
'message' => [ |
|
91
|
1 |
|
'message' => $this->message, |
|
92
|
|
|
], |
|
93
|
|
|
'tooFewItemsMessage' => [ |
|
94
|
1 |
|
'message' => $this->tooFewItemsMessage, |
|
95
|
1 |
|
'parameters' => ['min' => $this->min], |
|
96
|
|
|
], |
|
97
|
|
|
'tooManyItemsMessage' => [ |
|
98
|
1 |
|
'message' => $this->tooManyItemsMessage, |
|
99
|
1 |
|
'parameters' => ['max' => $this->max], |
|
100
|
|
|
], |
|
101
|
|
|
'notExactlyMessage' => [ |
|
102
|
1 |
|
'message' => $this->notExactlyMessage, |
|
103
|
1 |
|
'parameters' => ['exactly' => $this->exactly], |
|
104
|
|
|
], |
|
105
|
1 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
|
106
|
1 |
|
'skipOnError' => $this->skipOnError, |
|
107
|
|
|
]; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: