Passed
Pull Request — master (#222)
by Alexander
04:33 queued 02:13
created

Number::validateValue()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 9

Importance

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