Passed
Push — master ( 0363dc...b69f59 )
by Rustam
02:19
created

Number::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 41
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
c 0
b 0
f 0
dl 0
loc 41
ccs 1
cts 1
cp 1
rs 10
cc 1
nc 1
nop 10
crap 1

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