Passed
Pull Request — master (#333)
by Sergei
02:38
created

Number::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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\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 27
    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
        /**
50
         * @var string user-defined error message used when the value is smaller than {@link $min}.
51
         */
52
        private string $tooSmallMessage = 'Value must be no less than {min}.',
53
        /**
54
         * @var string user-defined error message used when the value is bigger than {@link $max}.
55
         */
56
        private string $tooBigMessage = 'Value must be no greater than {max}.',
57
        /**
58
         * @var string the regular expression for matching integers.
59
         */
60
        private string $integerPattern = '/^\s*[+-]?\d+\s*$/',
61
        /**
62
         * @var string 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
         */
65
        private string $numberPattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
66
67
        /**
68
         * @var bool|callable|null
69
         */
70
        private $skipOnEmpty = null,
71
        private bool $skipOnError = false,
72
        /**
73
         * @var Closure(mixed, ValidationContext):bool|null
74
         */
75
        private ?Closure $when = null,
76
    ) {
77
    }
78
79 7
    public function getName(): string
80
    {
81 7
        return 'number';
82
    }
83
84 111
    public function isAsInteger(): bool
85
    {
86 111
        return $this->asInteger;
87
    }
88
89 76
    public function getMin(): float|int|null
90
    {
91 76
        return $this->min;
92
    }
93
94 54
    public function getMax(): float|int|null
95
    {
96 54
        return $this->max;
97
    }
98
99 34
    public function getTooSmallMessage(): string
100
    {
101 34
        return $this->tooSmallMessage;
102
    }
103
104 18
    public function getTooBigMessage(): string
105
    {
106 18
        return $this->tooBigMessage;
107
    }
108
109 38
    public function getIntegerPattern(): string
110
    {
111 38
        return $this->integerPattern;
112
    }
113
114 61
    public function getNumberPattern(): string
115
    {
116 61
        return $this->numberPattern;
117
    }
118
119 11
    public function getNotANumberMessage(): string
120
    {
121 11
        return $this->asInteger ? 'Value must be an integer.' : 'Value must be a number.';
122
    }
123
124 11
    public function getOptions(): array
125
    {
126
        return [
127 11
            'asInteger' => $this->asInteger,
128 11
            'min' => $this->min,
129 11
            'max' => $this->max,
130
            'notANumberMessage' => [
131 11
                'message' => $this->getNotANumberMessage(),
132
            ],
133
            'tooSmallMessage' => [
134 11
                'message' => $this->tooSmallMessage,
135 11
                'parameters' => ['min' => $this->min],
136
            ],
137
            'tooBigMessage' => [
138 11
                'message' => $this->tooBigMessage,
139 11
                'parameters' => ['max' => $this->max],
140
            ],
141 11
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
142 11
            'skipOnError' => $this->skipOnError,
143 11
            'integerPattern' => $this->integerPattern,
144 11
            'numberPattern' => $this->numberPattern,
145
        ];
146
    }
147
148 50
    public function getHandlerClassName(): string
149
    {
150 50
        return NumberHandler::class;
151
    }
152
}
153