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 a number. |
13
|
|
|
* |
14
|
|
|
* The format of the number must match the regular expression specified in {@see Number::$pattern}. Optionally, you may |
15
|
|
|
* configure the {@see Number::$min} and {@see Number::$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 Number 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
|
36 |
|
* 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 |
57
|
|
|
* floating numbers with optional exponential part (e.g. -1.23e-10). |
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 = 'The allowed types are integer, float and string.', |
70
|
|
|
string $notNumberMessage = 'Value must be a number.', |
71
|
|
|
string $lessThanMinMessage = 'Value must be no less than {min}.', |
72
|
|
|
string $greaterThanMaxMessage = 'Value must be no greater than {max}.', |
73
|
|
|
string $pattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/', |
74
|
|
|
mixed $skipOnEmpty = null, |
75
|
|
|
bool $skipOnError = false, |
76
|
|
|
Closure|null $when = null, |
77
|
|
|
) { |
78
|
|
|
parent::__construct( |
79
|
|
|
min: $min, |
80
|
10 |
|
max: $max, |
81
|
|
|
incorrectInputMessage: $incorrectInputMessage, |
82
|
10 |
|
notNumberMessage: $notNumberMessage, |
83
|
|
|
lessThanMinMessage: $lessThanMinMessage, |
84
|
|
|
greaterThanMaxMessage: $greaterThanMaxMessage, |
85
|
106 |
|
pattern: $pattern, |
86
|
|
|
skipOnEmpty: $skipOnEmpty, |
87
|
106 |
|
skipOnError: $skipOnError, |
88
|
|
|
when: $when, |
89
|
|
|
); |
90
|
83 |
|
} |
91
|
|
|
} |
92
|
|
|
|