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