Completed
Push — master ( 929034...adc5e6 )
by Marcus
02:23
created

PointNumber::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
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