Passed
Push — master ( 46bdcb...724f49 )
by Smoren
02:43
created

NumericCheckFactory::getInRightOpenIntervalCheck()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 1
nop 2
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Smoren\Validator\Factories\Checks;
4
5
use Smoren\Validator\Factories\CheckBuilder;
6
use Smoren\Validator\Interfaces\CheckInterface;
7
use Smoren\Validator\Structs\CheckName;
8
use Smoren\Validator\Structs\Param;
9
10
class NumericCheckFactory
11
{
12 91
    public static function getNumericCheck(): CheckInterface
13
    {
14 91
        return CheckBuilder::create(CheckName::NUMERIC)
15 91
            ->withPredicate(fn ($value) => \is_numeric($value))
16 91
            ->build();
17
    }
18
19 2
    public static function getNumberCheck(): CheckInterface
20
    {
21 2
        return CheckBuilder::create(CheckName::NUMBER)
22 2
            ->withPredicate(fn ($value) => \is_int($value) || \is_float($value))
23 2
            ->build();
24
    }
25
26 2
    public static function getStringCheck(): CheckInterface
27
    {
28 2
        return CheckBuilder::create(CheckName::STRING)
29 2
            ->withPredicate(fn ($value) => \is_string($value))
30 2
            ->build();
31
    }
32
33 69
    public static function getIntegerCheck(): CheckInterface
34
    {
35 69
        return CheckBuilder::create(CheckName::INTEGER)
36 69
            ->withPredicate(fn ($value) => \is_int($value))
37 69
            ->build();
38
    }
39
40 48
    public static function getFloatCheck(): CheckInterface
41
    {
42 48
        return CheckBuilder::create(CheckName::FLOAT)
43 48
            ->withPredicate(fn ($value) => \is_float($value))
44 48
            ->build();
45
    }
46
47 6
    public static function getTruthyCheck(): CheckInterface
48
    {
49 6
        return CheckBuilder::create(CheckName::TRUTHY)
50 6
            ->withPredicate(fn ($value) => \boolval(floatval($value)))
51 6
            ->build();
52
    }
53
54 6
    public static function getFalsyCheck(): CheckInterface
55
    {
56 6
        return CheckBuilder::create(CheckName::FALSY)
57 6
            ->withPredicate(fn ($value) => !\boolval(floatval($value)))
58 6
            ->build();
59
    }
60
61 56
    public static function getPositiveCheck(): CheckInterface
62
    {
63 56
        return CheckBuilder::create(CheckName::POSITIVE)
64 56
            ->withPredicate(fn($value) => $value > 0)
65 56
            ->build();
66
    }
67
68 6
    public static function getNonPositiveCheck(): CheckInterface
69
    {
70 6
        return CheckBuilder::create(CheckName::NON_POSITIVE)
71 6
            ->withPredicate(fn($value) => $value <= 0)
72 6
            ->build();
73
    }
74
75 13
    public static function getNonNegativeCheck(): CheckInterface
76
    {
77 13
        return CheckBuilder::create(CheckName::NON_NEGATIVE)
78 13
            ->withPredicate(fn($value) => $value >= 0)
79 13
            ->build();
80
    }
81
82 6
    public static function getNegativeCheck(): CheckInterface
83
    {
84 6
        return CheckBuilder::create(CheckName::NEGATIVE)
85 6
            ->withPredicate(fn($value) => $value < 0)
86 6
            ->build();
87
    }
88
89
    /**
90
     * @param numeric $number
91
     * @return CheckInterface
92
     */
93 11
    public static function getGreaterThanCheck($number): CheckInterface
94
    {
95 11
        return CheckBuilder::create(CheckName::GREATER)
96 11
            ->withPredicate(fn($value, $number) => $value > $number)
97 11
            ->withParams([Param::EXPECTED => $number])
98 11
            ->build();
99
    }
100
101
    /**
102
     * @param numeric $number
103
     * @return CheckInterface
104
     */
105 6
    public static function getGreaterOrEqualCheck($number): CheckInterface
106
    {
107 6
        return CheckBuilder::create(CheckName::GREATER_OR_EQUEAL)
108 6
            ->withPredicate(fn($value, $number) => $value >= $number)
109 6
            ->withParams([Param::EXPECTED => $number])
110 6
            ->build();
111
    }
112
113
    /**
114
     * @param numeric $number
115
     * @return CheckInterface
116
     */
117 13
    public static function getLessThanCheck($number): CheckInterface
118
    {
119 13
        return CheckBuilder::create(CheckName::LESS)
120 13
            ->withPredicate(fn($value, $number) => $value < $number)
121 13
            ->withParams([Param::EXPECTED => $number])
122 13
            ->build();
123
    }
124
125
    /**
126
     * @param numeric $number
127
     * @return CheckInterface
128
     */
129 6
    public static function getLessOrEqualCheck($number): CheckInterface
130
    {
131 6
        return CheckBuilder::create(CheckName::LESS_OR_EQUEAL)
132 6
            ->withPredicate(fn($value, $number) => $value <= $number)
133 6
            ->withParams([Param::EXPECTED => $number])
134 6
            ->build();
135
    }
136
137
    /**
138
     * @param numeric $start
139
     * @param numeric $end
140
     * @return CheckInterface
141
     */
142 25
    public static function getBetweenCheck($start, $end): CheckInterface
143
    {
144 25
        return CheckBuilder::create(CheckName::BETWEEN)
145 25
            ->withPredicate(fn($value, $start, $end) => $value >= $start && $value <= $end)
146 25
            ->withParams(['start' => $start, 'end' => $end])
147 25
            ->build();
148
    }
149
150
    /**
151
     * @param numeric $start
152
     * @param numeric $end
153
     * @return CheckInterface
154
     */
155 24
    public static function getInOpenIntervalCheck($start, $end): CheckInterface
156
    {
157 24
        return CheckBuilder::create(CheckName::IN_OPEN_INTERVAL)
158 24
            ->withPredicate(fn($value, $start, $end) => $value > $start && $value < $end)
159 24
            ->withParams(['start' => $start, 'end' => $end])
160 24
            ->build();
161
    }
162
163
    /**
164
     * @param numeric $start
165
     * @param numeric $end
166
     * @return CheckInterface
167
     */
168 2
    public static function getInLeftOpenIntervalCheck($start, $end): CheckInterface
169
    {
170 2
        return CheckBuilder::create(CheckName::IN_LEFT_HALF_OPEN_INTERVAL)
171 2
            ->withPredicate(fn($value, $start, $end) => $value > $start && $value <= $end)
172 2
            ->withParams(['start' => $start, 'end' => $end])
173 2
            ->build();
174
    }
175
176
    /**
177
     * @param numeric $start
178
     * @param numeric $end
179
     * @return CheckInterface
180
     */
181 2
    public static function getInRightOpenIntervalCheck($start, $end): CheckInterface
182
    {
183 2
        return CheckBuilder::create(CheckName::IN_RIGHT_HALF_OPEN_INTERVAL)
184 2
            ->withPredicate(fn($value, $start, $end) => $value >= $start && $value < $end)
185 2
            ->withParams(['start' => $start, 'end' => $end])
186 2
            ->build();
187
    }
188
189 4
    public static function getFractionalCheck(): CheckInterface
190
    {
191 4
        return CheckBuilder::create(CheckName::FRACTIONAL)
192 4
            ->withPredicate(fn($value) => \abs($value - \round(\floatval($value))) >= \PHP_FLOAT_EPSILON)
193 4
            ->build();
194
    }
195
196 7
    public static function getNonFractionalCheck(): CheckInterface
197
    {
198 7
        return CheckBuilder::create(CheckName::NON_FRACTIONAL)
199 7
            ->withPredicate(fn($value) => \abs($value - \round(\floatval($value))) < \PHP_FLOAT_EPSILON)
200 7
            ->build();
201
    }
202
203 4
    public static function getFiniteCheck(): CheckInterface
204
    {
205 4
        return CheckBuilder::create(CheckName::FINITE)
206 4
            ->withPredicate(fn($value) => $value > -INF && $value < INF)
207 4
            ->build();
208
    }
209
210 4
    public static function getInfiniteCheck(): CheckInterface
211
    {
212 4
        return CheckBuilder::create(CheckName::INFINITE)
213 4
            ->withPredicate(fn($value) => $value === -INF || $value === INF)
214 4
            ->build();
215
    }
216
217 20
    public static function getEvenCheck(): CheckInterface
218
    {
219 20
        return CheckBuilder::create(CheckName::EVEN)
220 20
            ->withPredicate(fn($value) => $value % 2 === 0)
221 20
            ->build();
222
    }
223
224 22
    public static function getOddCheck(): CheckInterface
225
    {
226 22
        return CheckBuilder::create(CheckName::ODD)
227 22
            ->withPredicate(fn($value) => $value % 2 !== 0)
228 22
            ->build();
229
    }
230
231 2
    public static function getNanCheck(): CheckInterface
232
    {
233 2
        return CheckBuilder::create(CheckName::NAN)
234 2
            ->withPredicate(fn($value) => \is_nan(\floatval($value)))
235 2
            ->withDependOnChecks([NumericCheckFactory::getNumericCheck()])
236 2
            ->build();
237
    }
238
239 2
    public static function getNotNanCheck(): CheckInterface
240
    {
241 2
        return CheckBuilder::create(CheckName::NOT_NAN)
242 2
            ->withPredicate(fn($value) => !\is_nan(\floatval($value)))
243 2
            ->withDependOnChecks([NumericCheckFactory::getNumericCheck()])
244 2
            ->build();
245
    }
246
}
247