Passed
Pull Request — master (#534)
by
unknown
02:44
created

AbstractNumber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
nc 1
nop 10
dl 0
loc 12
cc 1
rs 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Closure;
8
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
9
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
10
use Yiisoft\Validator\Rule\Trait\WhenTrait;
11
use Yiisoft\Validator\RuleWithOptionsInterface;
12
use Yiisoft\Validator\SkipOnEmptyInterface;
13
use Yiisoft\Validator\SkipOnErrorInterface;
14
use Yiisoft\Validator\WhenInterface;
15
16
/**
17
 * Defines validation options to check that the value is a number.
18
 *
19
 * The format of the number must match the regular expression specified in {@see AbstractNumber::$pattern}. Optionally,
20
 * you may configure the {@see AbstractNumber::$min} and {@see AbstractNumber::$max}.
21
 * to ensure the number is within certain range.
22
 *
23
 * @see NumberHandler
24
 *
25
 * @psalm-import-type WhenType from WhenInterface
26
 */
27
abstract class AbstractNumber implements
28
    RuleWithOptionsInterface,
29
    SkipOnErrorInterface,
30
    WhenInterface,
31
    SkipOnEmptyInterface
32
{
33
    use SkipOnEmptyTrait;
34
    use SkipOnErrorTrait;
35
    use WhenTrait;
36
37
    /**
38
     * @param float|int|null $min Lower limit of the number. Defaults to `null`, meaning no lower limit.
39
     * See {@see $tooSmallMessage} for the customized message used when the number is too small.
40
     * @param float|int|null $max Upper limit of the number. Defaults to `null`, meaning no upper limit.
41
     * See {@see $tooBigMessage} for the customized message used when the number is too big.
42
     * @param string $incorrectInputMessage Error message used when the value is not numeric.
43
     *
44
     * You may use the following placeholders in the message:
45
     *
46
     * - `{attribute}`: the translated label of the attribute being validated.
47
     * - `{type}`: the type of the value being validated.
48
     * @param string $tooSmallMessage Error message used when the value is smaller than {@link $min}.
49
     *
50
     * You may use the following placeholders in the message:
51
     *
52
     * - `{attribute}`: the translated label of the attribute being validated.
53
     * - `{min}`: minimum value.
54
     * - `{value}`: actual value.
55
     * @param string $tooBigMessage Error message used when the value is bigger than {@link $max}.
56
     *
57
     * You may use the following placeholders in the message:
58
     *
59
     * - `{attribute}`: the translated label of the attribute being validated.
60
     * - `{max}`: maximum value.
61
     * - `{value}`: actual value.
62
     * @param string $pattern The regular expression for matching numbers. It defaults to a pattern
63
     * that matches floating numbers with optional exponential part (e.g. -1.23e-10).
64
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
65
     * See {@see SkipOnEmptyInterface}.
66
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
67
     * See {@see SkipOnErrorInterface}.
68
     * @param Closure|null $when A callable to define a condition for applying the rule.
69
     * See {@see WhenInterface}.
70
     * @psalm-param WhenType $when
71
     */
72
    public function __construct(
73
        private float|int|null $min = null,
74
        private float|int|null $max = null,
75
        private string $incorrectInputMessage = 'The allowed types are integer, float and string.',
76
        private string $notNumberMessage = 'Value must be a number.',
77
        private string $tooSmallMessage = 'Value must be no less than {min}.',
78
        private string $tooBigMessage = 'Value must be no greater than {max}.',
79
        private string $pattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
80
        private mixed $skipOnEmpty = null,
81
        private bool $skipOnError = false,
82
        private Closure|null $when = null,
83
    ) {
84
    }
85
86
    /**
87
     * Get lower limit of the number. `null` means no lower limit.
88
     *
89
     * @return float|int|null Lower limit of the number.
90
     *
91
     * @see $min
92
     */
93
    public function getMin(): float|int|null
94
    {
95
        return $this->min;
96
    }
97
98
    /**
99
     * Get upper limit of the number. `null` means no upper limit.
100
     *
101
     * @return float|int|null Upper limit of the number.
102
     *
103
     * @see $max
104
     */
105
    public function getMax(): float|int|null
106
    {
107
        return $this->max;
108
    }
109
110
    /**
111
     * Get error message used when the value is not numeric.
112
     *
113
     * @return string Error message.
114
     *
115
     * @see $incorrectInputMessage
116
     */
117
    public function getIncorrectInputMessage(): string
118
    {
119
        return $this->incorrectInputMessage;
120
    }
121
122
    /**
123
     * Get error message used when value type does not match.
124
     *
125
     * @return string Error message.
126
     */
127
    public function getNotNumberMessage(): string
128
    {
129
        return $this->notNumberMessage;
130
    }
131
132
    /**
133
     * Get error message used when the value is smaller than {@link $min}.
134
     *
135
     * @return string Error message.
136
     *
137
     * @see $tooSmallMessage
138
     */
139
    public function getTooSmallMessage(): string
140
    {
141
        return $this->tooSmallMessage;
142
    }
143
144
    /**
145
     * Get error message used when the value is bigger than {@link $max}.
146
     *
147
     * @return string Error message.
148
     *
149
     * @see $tooBigMessage
150
     */
151
    public function getTooBigMessage(): string
152
    {
153
        return $this->tooBigMessage;
154
    }
155
156
    /**
157
     * The regular expression for matching numbers.
158
     *
159
     * @return string The regular expression.
160
     *
161
     * @see $pattern
162
     */
163
    public function getPattern(): string
164
    {
165
        return $this->pattern;
166
    }
167
168
    public function getOptions(): array
169
    {
170
        return [
171
            'min' => $this->min,
172
            'max' => $this->max,
173
            'incorrectInputMessage' => [
174
                'template' => $this->incorrectInputMessage,
175
                'parameters' => [],
176
            ],
177
            'notNumberMessage' => [
178
                'template' => $this->notNumberMessage,
179
                'parameters' => [],
180
            ],
181
            'tooSmallMessage' => [
182
                'template' => $this->tooSmallMessage,
183
                'parameters' => ['min' => $this->min],
184
            ],
185
            'tooBigMessage' => [
186
                'template' => $this->tooBigMessage,
187
                'parameters' => ['max' => $this->max],
188
            ],
189
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
190
            'skipOnError' => $this->skipOnError,
191
            'pattern' => $this->pattern,
192
        ];
193
    }
194
195
    public function getHandler(): string
196
    {
197
        return NumberHandler::class;
198
    }
199
}
200