1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smoren\Validator\Rules; |
6
|
|
|
|
7
|
|
|
use Smoren\Validator\Interfaces\FloatRuleInterface; |
8
|
|
|
use Smoren\Validator\Structs\Check; |
9
|
|
|
|
10
|
|
|
class FloatRule extends NumericRule implements FloatRuleInterface |
11
|
|
|
{ |
12
|
|
|
public const ERROR_NOT_FLOAT = 'not_float'; |
13
|
|
|
public const ERROR_FRACTIONAL = 'fractional'; |
14
|
|
|
public const ERROR_NOT_FRACTIONAL = 'not_fractional'; |
15
|
|
|
public const ERROR_NOT_INFINITE = 'not_infinite'; |
16
|
|
|
public const ERROR_NOT_FINITE = 'not_finite'; |
17
|
|
|
|
18
|
6 |
|
public function __construct() |
19
|
|
|
{ |
20
|
6 |
|
$this->addCheck(new Check( |
21
|
6 |
|
self::ERROR_NOT_FLOAT, |
22
|
6 |
|
fn ($value) => is_float($value), |
23
|
6 |
|
[] |
24
|
6 |
|
), true); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritDoc} |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
|
|
public function fractional(): self |
33
|
|
|
{ |
34
|
|
|
return $this->addCheck(new Check( |
35
|
|
|
self::ERROR_NOT_FRACTIONAL, |
36
|
|
|
fn ($value) => \abs($value - \round($value)) >= PHP_FLOAT_EPSILON |
37
|
|
|
)); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
* |
43
|
|
|
* @return static |
44
|
|
|
*/ |
45
|
3 |
|
public function nonFractional(): self |
46
|
|
|
{ |
47
|
3 |
|
return $this->addCheck(new Check( |
48
|
3 |
|
self::ERROR_FRACTIONAL, |
49
|
3 |
|
fn ($value) => \abs($value - \round($value)) < PHP_FLOAT_EPSILON |
50
|
3 |
|
)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
* |
56
|
|
|
* @return static |
57
|
|
|
*/ |
58
|
|
|
public function finite(): self |
59
|
|
|
{ |
60
|
|
|
return $this->addCheck(new Check( |
61
|
|
|
self::ERROR_NOT_FINITE, |
62
|
|
|
fn ($value) => $value > -INF && $value < INF, |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritDoc} |
68
|
|
|
* |
69
|
|
|
* @return static |
70
|
|
|
*/ |
71
|
|
|
public function infinite(): self |
72
|
|
|
{ |
73
|
|
|
return$this->addCheck(new Check( |
74
|
|
|
self::ERROR_NOT_INFINITE, |
75
|
|
|
fn ($value) => $value === -INF || $value === INF, |
76
|
|
|
)); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|