Passed
Pull Request — master (#407)
by Sergei
02:48
created

Number::getNotANumberMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
rs 10
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 36
    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 10
    public function getName(): string
81
    {
82 10
        return 'number';
83
    }
84
85 106
    public function isAsInteger(): bool
86
    {
87 106
        return $this->asInteger;
88
    }
89
90 83
    public function getMin(): float|int|null
91
    {
92 83
        return $this->min;
93
    }
94
95 57
    public function getMax(): float|int|null
96
    {
97 57
        return $this->max;
98
    }
99
100 14
    public function getIncorrectInputMessage(): string
101
    {
102 14
        return $this->incorrectInputMessage;
103
    }
104
105 39
    public function getTooSmallMessage(): string
106
    {
107 39
        return $this->tooSmallMessage;
108
    }
109
110 20
    public function getTooBigMessage(): string
111
    {
112 20
        return $this->tooBigMessage;
113
    }
114
115 42
    public function getIntegerPattern(): string
116
    {
117 42
        return $this->integerPattern;
118
    }
119
120 64
    public function getNumberPattern(): string
121
    {
122 64
        return $this->numberPattern;
123
    }
124
125 37
    public function getNotNumberMessage(): string
126
    {
127 37
        return $this->asInteger ? 'Value must be an integer.' : 'Value must be a number.';
128
    }
129
130 14
    public function getOptions(): array
131
    {
132
        return [
133 14
            'asInteger' => $this->asInteger,
134 14
            'min' => $this->min,
135 14
            'max' => $this->max,
136
            'incorrectInputMessage' => [
137 14
                'template' => $this->incorrectInputMessage,
138
                'parameters' => [],
139
            ],
140
            'notNumberMessage' => [
141 14
                'template' => $this->getNotNumberMessage(),
142
                'parameters' => [],
143
            ],
144
            'tooSmallMessage' => [
145 14
                'template' => $this->tooSmallMessage,
146 14
                'parameters' => ['min' => $this->min],
147
            ],
148
            'tooBigMessage' => [
149 14
                'template' => $this->tooBigMessage,
150 14
                'parameters' => ['max' => $this->max],
151
            ],
152 14
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
153 14
            'skipOnError' => $this->skipOnError,
154 14
            'integerPattern' => $this->integerPattern,
155 14
            'numberPattern' => $this->numberPattern,
156
        ];
157
    }
158
159 120
    public function getHandlerClassName(): string
160
    {
161 120
        return NumberHandler::class;
162
    }
163
}
164