|
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\WhenInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Defines validation options to check that the value is an integer number. |
|
13
|
|
|
* |
|
14
|
|
|
* The format of the number must match the regular expression specified in {@see Integer::$pattern}. Optionally, you may |
|
15
|
|
|
* configure the {@see Integer::$min} and {@see Integer::$max} to ensure the number is within a certain range. |
|
16
|
|
|
* |
|
17
|
|
|
* @see NumberHandler |
|
18
|
|
|
* @see AbstractNumber |
|
19
|
|
|
* |
|
20
|
|
|
* @psalm-import-type WhenType from WhenInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
|
23
|
|
|
final class Integer extends AbstractNumber |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param float|int|null $min Lower limit of the number. Defaults to `null`, meaning no lower limit. See |
|
27
|
|
|
* {@see $lessThanMinMessage} for the customized message used when the number is too small. |
|
28
|
|
|
* @param float|int|null $max Upper limit of the number. Defaults to `null`, meaning no upper limit. See |
|
29
|
|
|
* {@see $greaterThanMaxMessage} for the customized message used when the number is too big. |
|
30
|
|
|
* @param string $incorrectInputMessage Error message used when the value is not numeric. |
|
31
|
|
|
* |
|
32
|
|
|
* You may use the following placeholders in the message: |
|
33
|
|
|
* |
|
34
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
|
35
|
|
|
* - `{type}`: the type of the value being validated. |
|
36
|
|
|
* @param string $notNumberMessage Error message used when the value does not match {@see $pattern}. |
|
37
|
|
|
* |
|
38
|
|
|
* You may use the following placeholders in the message: |
|
39
|
|
|
* |
|
40
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
|
41
|
|
|
* - `{value}`: actual value. |
|
42
|
|
|
* @param string $lessThanMinMessage Error message used when the value is smaller than {@link $min}. |
|
43
|
|
|
* |
|
44
|
|
|
* You may use the following placeholders in the message: |
|
45
|
|
|
* |
|
46
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
|
47
|
|
|
* - `{min}`: minimum value. |
|
48
|
|
|
* - `{value}`: actual value. |
|
49
|
|
|
* @param string $greaterThanMaxMessage Error message used when the value is bigger than {@link $max}. |
|
50
|
|
|
* |
|
51
|
|
|
* You may use the following placeholders in the message: |
|
52
|
|
|
* |
|
53
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
|
54
|
|
|
* - `{max}`: maximum value. |
|
55
|
|
|
* - `{value}`: actual value. |
|
56
|
|
|
* @param string $pattern The regular expression for matching numbers. It defaults to a pattern that matches integer |
|
57
|
|
|
* numbers with optional leading zero part (e.g. 01). |
|
58
|
|
|
* @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty. See |
|
59
|
|
|
* {@see SkipOnEmptyInterface}. |
|
60
|
|
|
* @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See |
|
61
|
|
|
* {@see SkipOnErrorInterface}. |
|
62
|
|
|
* @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}. |
|
63
|
|
|
* |
|
64
|
|
|
* @psalm-param WhenType $when |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct( |
|
67
|
|
|
float|int|null $min = null, |
|
68
|
|
|
float|int|null $max = null, |
|
69
|
|
|
string $incorrectInputMessage = self::DEFAULT_INCORRECT_INPUT_MESSAGE, |
|
70
|
|
|
string $notNumberMessage = 'Value must be an integer.', |
|
71
|
|
|
string $lessThanMinMessage = self::DEFAULT_LESS_THAN_MIN_MESSAGE, |
|
72
|
|
|
string $greaterThanMaxMessage = self::DEFAULT_GREATER_THAN_MAX_MESSAGE, |
|
73
|
|
|
string $pattern = '/^\s*[+-]?\d+\s*$/', |
|
74
|
|
|
mixed $skipOnEmpty = null, |
|
75
|
|
|
bool $skipOnError = false, |
|
76
|
|
|
Closure|null $when = null, |
|
77
|
|
|
) { |
|
78
|
|
|
parent::__construct( |
|
79
|
|
|
min: $min, |
|
80
|
|
|
max: $max, |
|
81
|
|
|
incorrectInputMessage: $incorrectInputMessage, |
|
82
|
|
|
notNumberMessage: $notNumberMessage, |
|
83
|
|
|
lessThanMinMessage: $lessThanMinMessage, |
|
84
|
|
|
greaterThanMaxMessage: $greaterThanMaxMessage, |
|
85
|
|
|
pattern: $pattern, |
|
86
|
|
|
skipOnEmpty: $skipOnEmpty, |
|
87
|
|
|
skipOnError: $skipOnError, |
|
88
|
|
|
when: $when, |
|
89
|
|
|
); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|