Passed
Pull Request — master (#222)
by Dmitriy
12:29
created

Number   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 86
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 13
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\RuleNameTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 9 at column 27
Loading history...
10
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
11
use Yiisoft\Validator\RuleInterface;
12
13
/**
14
 * Validates that the value is a number.
15
 *
16
 * The format of the number must match the regular expression specified in {@see Number::$integerPattern}
17
 * or {@see Number::$numberPattern}. Optionally, you may configure the {@see Number::min()} and {@see Number::max()}
18
 * to ensure the number is within certain range.
19
 */
20
#[Attribute(Attribute::TARGET_PROPERTY)]
21
final class Number implements RuleInterface
22
{
23
    use HandlerClassNameTrait;
24
    use RuleNameTrait;
25
26 2
    public function __construct(
27
        /**
28
         * @var bool whether the value can only be an integer. Defaults to false.
29
         */
30
        public bool $asInteger = false,
31
        /**
32
         * @var float|int lower limit of the number. Defaults to null, meaning no lower limit.
33
         *
34
         * @see tooSmallMessage for the customized message used when the number is too small.
35
         */
36
        public           $min = null,
37
        /**
38
         * @var float|int upper limit of the number. Defaults to null, meaning no upper limit.
39
         *
40
         * @see tooBigMessage for the customized message used when the number is too big.
41
         */
42
        public           $max = null,
43
        /**
44
         * @var string user-defined error message used when the value is smaller than {@link $min}.
45
         */
46
        public string $tooSmallMessage = 'Value must be no less than {min}.',
47
        /**
48
         * @var string user-defined error message used when the value is bigger than {@link $max}.
49
         */
50
        public string $tooBigMessage = 'Value must be no greater than {max}.',
51
        /**
52
         * @var string the regular expression for matching integers.
53
         */
54
        public string $integerPattern = '/^\s*[+-]?\d+\s*$/',
55
        /**
56
         * @var string the regular expression for matching numbers. It defaults to a pattern
57
         * that matches floating numbers with optional exponential part (e.g. -1.23e-10).
58
         */
59
        public string $numberPattern = '/^\s*[-+]?\d*\.?\d+([eE][-+]?\d+)?\s*$/',
60
        public bool $skipOnEmpty = false,
61
        public bool $skipOnError = false,
62
        public ?Closure $when = null,
63
    ) {
64
    }
65
66 9
    public function getNotANumberMessage(): string
67
    {
68 9
        return $this->asInteger ? 'Value must be an integer.' : 'Value must be a number.';
69
    }
70
71 9
    public function getOptions(): array
72
    {
73
        return [
74 9
            'asInteger' => $this->asInteger,
75 9
            'min' => $this->min,
76 9
            'max' => $this->max,
77
            'notANumberMessage' => [
78 9
                'message' => $this->getNotANumberMessage(),
79
            ],
80
            'tooSmallMessage' => [
81 9
                'message' => $this->tooSmallMessage,
82 9
                'parameters' => ['min' => $this->min],
83
            ],
84
            'tooBigMessage' => [
85 9
                'message' => $this->tooBigMessage,
86 9
                'parameters' => ['max' => $this->max],
87
            ],
88 9
            'skipOnEmpty' => $this->skipOnEmpty,
89 9
            'skipOnError' => $this->skipOnError,
90 9
            'integerPattern' => $this->integerPattern,
91 9
            'numberPattern' => $this->numberPattern,
92
        ];
93
    }
94
}
95