Passed
Push — master ( cdfa93...96b6af )
by Smoren
02:36
created

NumberRule::positive()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Smoren\Validator\Rules;
4
5
use Smoren\Validator\Interfaces\NumberRuleInterface;
6
use Smoren\Validator\Structs\Check;
7
8
abstract class NumberRule extends Rule implements NumberRuleInterface
9
{
10
    public const ERROR_NOT_POSITIVE = 'not_positive';
11
    public const ERROR_NOT_NON_POSITIVE = 'not_non_positive';
12
    public const ERROR_NOT_NON_NEGATIVE = 'not_non_negative';
13
    public const ERROR_NOT_NEGATIVE = 'not_negative';
14
    public const ERROR_NOT_GREATER = 'not_greater';
15
    public const ERROR_NOT_GREATER_OR_EQUEAL = 'not_greater_or_equal';
16
    public const ERROR_NOT_LESS = 'not_less';
17
    public const ERROR_NOT_LESS_OR_EQUEAL = 'not_less_or_equal';
18
    public const ERROR_NOT_IN_SEGMENT = 'not_in_segment';
19
    public const ERROR_NOT_IN_INTERVAL = 'not_in_interval';
20
21
    /**
22
     * {@inheritDoc}
23
     *
24
     * @return static
25
     */
26 21
    public function positive(): self
27
    {
28 21
        return $this->add(new Check(
29 21
            self::ERROR_NOT_POSITIVE,
30 21
            fn ($value) => $value > 0
31 21
        ));
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     *
37
     * @return static
38
     */
39 2
    public function nonPositive(): self
40
    {
41 2
        return $this->add(new Check(
42 2
            self::ERROR_NOT_NON_POSITIVE,
43 2
            fn ($value) => $value <= 0
44 2
        ));
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     *
50
     * @return static
51
     */
52 3
    public function nonNegative(): self
53
    {
54 3
        return $this->add(new Check(
55 3
            self::ERROR_NOT_NON_NEGATIVE,
56 3
            fn ($value) => $value >= 0
57 3
        ));
58
    }
59
60
    /**
61
     * {@inheritDoc}
62
     *
63
     * @return static
64
     */
65 2
    public function negative(): self
66
    {
67 2
        return $this->add(new Check(
68 2
            self::ERROR_NOT_NEGATIVE,
69 2
            fn ($value) => $value < 0
70 2
        ));
71
    }
72
73
74
    /**
75
     * {@inheritDoc}
76
     *
77
     * @return static
78
     */
79 2
    public function greaterTran($number): NumberRuleInterface
80
    {
81 2
        return $this->add(new Check(
82 2
            self::ERROR_NOT_GREATER,
83 2
            fn ($value) => $value > $number,
84 2
            ['number' => $number]
85 2
        ));
86
    }
87
88
89
    /**
90
     * {@inheritDoc}
91
     *
92
     * @return static
93
     */
94 2
    public function greaterOrEqual($number): NumberRuleInterface
95
    {
96 2
        return $this->add(new Check(
97 2
            self::ERROR_NOT_GREATER_OR_EQUEAL,
98 2
            fn ($value) => $value >= $number,
99 2
            ['number' => $number]
100 2
        ));
101
    }
102
103
104
    /**
105
     * {@inheritDoc}
106
     *
107
     * @return static
108
     */
109 2
    public function lessTran($number): NumberRuleInterface
110
    {
111 2
        return $this->add(new Check(
112 2
            self::ERROR_NOT_LESS,
113 2
            fn ($value) => $value < $number,
114 2
            ['number' => $number]
115 2
        ));
116
    }
117
118
119
    /**
120
     * {@inheritDoc}
121
     *
122
     * @return static
123
     */
124 2
    public function lessOrEqual($number): NumberRuleInterface
125
    {
126 2
        return $this->add(new Check(
127 2
            self::ERROR_NOT_LESS_OR_EQUEAL,
128 2
            fn ($value) => $value <= $number,
129 2
            ['number' => $number]
130 2
        ));
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     *
136
     * @return static
137
     */
138 8
    public function between($start, $end): self
139
    {
140 8
        return $this->add(new Check(
141 8
            self::ERROR_NOT_IN_SEGMENT,
142 8
            fn ($value) => $value >= $start && $value <= $end,
143 8
            ['start' => $start, 'end' => $end]
144 8
        ));
145
    }
146
147
    /**
148
     * {@inheritDoc}
149
     */
150 7
    public function inInterval($start, $end): self
151
    {
152 7
        return $this->add(new Check(
153 7
            self::ERROR_NOT_IN_INTERVAL,
154 7
            fn ($value) => $value > $start && $value < $end,
155 7
            ['start' => $start, 'end' => $end]
156 7
        ));
157
    }
158
}
159