Passed
Push — master ( 070df3...782cd4 )
by
unknown
02:35
created

AbstractNumber::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

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

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} to ensure the number is within a
21
 * 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. See
39
     * {@see $lessThanMinMessage} 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. See
41
     * {@see $greaterThanMaxMessage} 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 $notNumberMessage Error message used when the value does not match {@see $pattern}.
49
     *
50
     * You may use the following placeholders in the message:
51
     *
52
     * - `{attribute}`: the translated label of the attribute being validated.
53
     * - `{value}`: actual value.
54
     * @param string $lessThanMinMessage Error message used when the value is smaller than {@link $min}.
55
     *
56
     * You may use the following placeholders in the message:
57
     *
58
     * - `{attribute}`: the translated label of the attribute being validated.
59
     * - `{min}`: minimum value.
60
     * - `{value}`: actual value.
61
     * @param string $greaterThanMaxMessage Error message used when the value is bigger than {@link $max}.
62
     *
63
     * You may use the following placeholders in the message:
64
     *
65
     * - `{attribute}`: the translated label of the attribute being validated.
66
     * - `{max}`: maximum value.
67
     * - `{value}`: actual value.
68
     * @param string $pattern The regular expression for matching numbers. It defaults to a pattern that matches
69
     * floating numbers with optional exponential part (e.g. -1.23e-10).
70
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty. See
71
     * {@see SkipOnEmptyInterface}.
72
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See
73
     * {@see SkipOnErrorInterface}.
74
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
75
     * @psalm-param WhenType $when
76
     */
77
    public function __construct(
78
        private float|int|null $min = null,
79
        private float|int|null $max = null,
80
        private string $incorrectInputMessage = 'The allowed types are integer, float and string.',
81
        private string $notNumberMessage = 'Value must be a number.',
82
        private string $lessThanMinMessage = 'Value must be no less than {min}.',
83
        private string $greaterThanMaxMessage = 'Value must be no greater than {max}.',
84
        private string $pattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
85
        private mixed $skipOnEmpty = null,
86
        private bool $skipOnError = false,
87
        private Closure|null $when = null,
88
    ) {
89
    }
90
91
    /**
92
     * Get lower limit of the number. `null` means no lower limit.
93
     *
94
     * @return float|int|null Lower limit of the number.
95
     *
96
     * @see $min
97
     */
98
    public function getMin(): float|int|null
99
    {
100
        return $this->min;
101
    }
102
103
    /**
104
     * Get upper limit of the number. `null` means no upper limit.
105
     *
106
     * @return float|int|null Upper limit of the number.
107
     *
108
     * @see $max
109
     */
110
    public function getMax(): float|int|null
111
    {
112
        return $this->max;
113
    }
114
115
    /**
116
     * Get error message used when the value is not numeric.
117
     *
118
     * @return string Error message.
119
     *
120
     * @see $incorrectInputMessage
121
     */
122
    public function getIncorrectInputMessage(): string
123
    {
124
        return $this->incorrectInputMessage;
125
    }
126
127
    /**
128
     * Get error message used when the value does not match {@see $pattern}
129
     *
130
     * @return string Error message.
131
     *
132
     * @see $notNumberMessage
133
     */
134
    public function getNotNumberMessage(): string
135
    {
136
        return $this->notNumberMessage;
137
    }
138
139
    /**
140
     * Get error message used when the value is smaller than {@link $min}.
141
     *
142
     * @return string Error message.
143
     *
144
     * @see $lessThanMinMessage
145
     */
146
    public function getLessThanMinMessage(): string
147
    {
148
        return $this->lessThanMinMessage;
149
    }
150
151
    /**
152
     * Get error message used when the value is bigger than {@link $max}.
153
     *
154
     * @return string Error message.
155
     *
156
     * @see $greaterThanMaxMessage
157
     */
158
    public function getGreaterThanMaxMessage(): string
159
    {
160
        return $this->greaterThanMaxMessage;
161
    }
162
163
    /**
164
     * The regular expression for matching numbers.
165
     *
166
     * @return string The regular expression.
167
     *
168
     * @see $pattern
169
     */
170
    public function getPattern(): string
171
    {
172
        return $this->pattern;
173
    }
174
175
    public function getOptions(): array
176
    {
177
        return [
178
            'min' => $this->min,
179
            'max' => $this->max,
180
            'incorrectInputMessage' => [
181
                'template' => $this->incorrectInputMessage,
182
                'parameters' => [],
183
            ],
184
            'notNumberMessage' => [
185
                'template' => $this->notNumberMessage,
186
                'parameters' => [],
187
            ],
188
            'lessThanMinMessage' => [
189
                'template' => $this->lessThanMinMessage,
190
                'parameters' => ['min' => $this->min],
191
            ],
192
            'greaterThanMaxMessage' => [
193
                'template' => $this->greaterThanMaxMessage,
194
                'parameters' => ['max' => $this->max],
195
            ],
196
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
197
            'skipOnError' => $this->skipOnError,
198
            'pattern' => $this->pattern,
199
        ];
200
    }
201
202
    public function getHandler(): string
203
    {
204
        return NumberHandler::class;
205
    }
206
}
207