|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mbright\Validation\Rule\Validate\MySql; |
|
4
|
|
|
|
|
5
|
|
|
use Mbright\Validation\Rule\Validate\ValidateRuleInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Validates that data can be sent into one of the following column types: |
|
9
|
|
|
* - Decimal |
|
10
|
|
|
* - Numeric |
|
11
|
|
|
* - Float |
|
12
|
|
|
* - Double |
|
13
|
|
|
*/ |
|
14
|
|
|
class PointNumber implements ValidateRuleInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Represents the precision of the float. |
|
18
|
|
|
* |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $precision; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Represents the scale for the float which determines the range of accepted values. |
|
25
|
|
|
* |
|
26
|
|
|
* @var int |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $scale; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param int $precision |
|
32
|
|
|
* @param int $scale |
|
33
|
|
|
*/ |
|
34
|
12 |
|
public function __construct(int $precision = 10, int $scale = 0) |
|
35
|
|
|
{ |
|
36
|
12 |
|
$this->precision = $precision; |
|
37
|
12 |
|
$this->scale = $scale; |
|
38
|
12 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Check that the value in the given field is a valid fixed point with the appropriate precision and scale. |
|
42
|
|
|
* |
|
43
|
|
|
* @param object $subject |
|
44
|
|
|
* @param string $field |
|
45
|
|
|
* |
|
46
|
|
|
* @return bool |
|
47
|
|
|
*/ |
|
48
|
12 |
|
public function __invoke($subject, string $field): bool |
|
49
|
|
|
{ |
|
50
|
12 |
|
$value = $subject->$field; |
|
51
|
|
|
|
|
52
|
12 |
|
if (!is_float($value)) { |
|
53
|
6 |
|
return false; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// separate the digits based on if they come before or after the decimal |
|
57
|
6 |
|
$segments = explode('.', (string) $value); |
|
58
|
6 |
|
$numBeforeDecimal = count($segments[0]); |
|
59
|
6 |
|
$numAfterDecimal = count($segments[1] ?? []); |
|
60
|
|
|
|
|
61
|
6 |
|
return $this->checkPrecision($numBeforeDecimal, $numAfterDecimal) && $this->checkScale($numAfterDecimal); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Ensures the correct precision. |
|
66
|
|
|
* |
|
67
|
|
|
* @param int $numBeforeDecimal |
|
68
|
|
|
* @param int $numAfterDecimal |
|
69
|
|
|
* |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
6 |
|
protected function checkPrecision(int $numBeforeDecimal, int $numAfterDecimal) |
|
73
|
|
|
{ |
|
74
|
6 |
|
return $numAfterDecimal + $numBeforeDecimal <= $this->precision; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Ensures the correct scale. |
|
79
|
|
|
* |
|
80
|
|
|
* @param int $numAfterDecimal |
|
81
|
|
|
* |
|
82
|
|
|
* @return bool |
|
83
|
|
|
*/ |
|
84
|
6 |
|
protected function checkScale(int $numAfterDecimal) |
|
85
|
|
|
{ |
|
86
|
6 |
|
return $numAfterDecimal <= $this->scale; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|