Passed
Pull Request — master (#364)
by
unknown
02:49
created

Number::getIncorrectInputMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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\Rule\Trait\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\SerializableRuleInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\ValidationContext;
16
use Yiisoft\Validator\WhenInterface;
17
18
/**
19
 * Validates that the value is a number.
20
 *
21
 * The format of the number must match the regular expression specified in {@see Number::$integerPattern}
22
 * or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()}
23
 * to ensure the number is within certain range.
24
 */
25
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
26
final class Number implements SerializableRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
27
{
28
    use SkipOnEmptyTrait;
29
    use SkipOnErrorTrait;
30
    use WhenTrait;
31
32 34
    public function __construct(
33
        /**
34
         * @var bool whether the value can only be an integer. Defaults to false.
35
         */
36
        private bool $asInteger = false,
37
        /**
38
         * @var float|int lower limit of the number. Defaults to null, meaning no lower limit.
39
         *
40
         * @see tooSmallMessage for the customized message used when the number is too small.
41
         */
42
        private $min = null,
43
        /**
44
         * @var float|int upper limit of the number. Defaults to null, meaning no upper limit.
45
         *
46
         * @see tooBigMessage for the customized message used when the number is too big.
47
         */
48
        private $max = null,
49
        private string $incorrectInputMessage = 'The allowed types are integer, float and string.',
50
        /**
51
         * @var string user-defined error message used when the value is smaller than {@link $min}.
52
         */
53
        private string $tooSmallMessage = 'Value must be no less than {min}.',
54
        /**
55
         * @var string user-defined error message used when the value is bigger than {@link $max}.
56
         */
57
        private string $tooBigMessage = 'Value must be no greater than {max}.',
58
        /**
59
         * @var string the regular expression for matching integers.
60
         */
61
        private string $integerPattern = '/^\s*[+-]?\d+\s*$/',
62
        /**
63
         * @var string the regular expression for matching numbers. It defaults to a pattern
64
         * that matches floating numbers with optional exponential part (e.g. -1.23e-10).
65
         */
66
        private string $numberPattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
67
68
        /**
69
         * @var bool|callable|null
70
         */
71
        private $skipOnEmpty = null,
72
        private bool $skipOnError = false,
73
        /**
74
         * @var Closure(mixed, ValidationContext):bool|null
75
         */
76
        private ?Closure $when = null,
77
    ) {
78
    }
79
80 8
    public function getName(): string
81
    {
82 8
        return 'number';
83
    }
84
85 99
    public function isAsInteger(): bool
86
    {
87 99
        return $this->asInteger;
88
    }
89
90 76
    public function getMin(): float|int|null
91
    {
92 76
        return $this->min;
93
    }
94
95 53
    public function getMax(): float|int|null
96
    {
97 53
        return $this->max;
98
    }
99
100 14
    public function getIncorrectInputMessage(): string
101
    {
102 14
        return $this->incorrectInputMessage;
103
    }
104
105 35
    public function getTooSmallMessage(): string
106
    {
107 35
        return $this->tooSmallMessage;
108
    }
109
110 17
    public function getTooBigMessage(): string
111
    {
112 17
        return $this->tooBigMessage;
113
    }
114
115 38
    public function getIntegerPattern(): string
116
    {
117 38
        return $this->integerPattern;
118
    }
119
120 61
    public function getNumberPattern(): string
121
    {
122 61
        return $this->numberPattern;
123
    }
124
125 35
    public function getNotANumberMessage(): string
126
    {
127 35
        return $this->asInteger ? 'Value must be an integer.' : 'Value must be a number.';
128
    }
129
130 12
    public function getOptions(): array
131
    {
132
        return [
133 12
            'asInteger' => $this->asInteger,
134 12
            'min' => $this->min,
135 12
            'max' => $this->max,
136
            'incorrectInputMessage' => [
137 12
                'message' => $this->incorrectInputMessage,
138
            ],
139
            'notANumberMessage' => [
140 12
                'message' => $this->getNotANumberMessage(),
141
            ],
142
            'tooSmallMessage' => [
143 12
                'message' => $this->tooSmallMessage,
144 12
                'parameters' => ['min' => $this->min],
145
            ],
146
            'tooBigMessage' => [
147 12
                'message' => $this->tooBigMessage,
148 12
                'parameters' => ['max' => $this->max],
149
            ],
150 12
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
151 12
            'skipOnError' => $this->skipOnError,
152 12
            'integerPattern' => $this->integerPattern,
153 12
            'numberPattern' => $this->numberPattern,
154
        ];
155
    }
156
157 113
    public function getHandlerClassName(): string
158
    {
159 113
        return NumberHandler::class;
160
    }
161
}
162