AbstractMySqlIntegerCase::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Mbright\Validation\Rule\Validate\MySql;
4
5
use Mbright\Validation\Rule\Validate\AbstractIntegerCase;
6
7
abstract class AbstractMySqlIntegerCase extends AbstractIntegerCase
8
{
9
    /**
10
     * Indicates if the integer is signed.
11
     *
12
     * @var bool
13
     */
14
    protected $signed;
15
16
    /**
17
     * @param bool $signed Indicates if the value is signed. For MySql 5.7, integers are signed by default.
18
     */
19 183
    public function __construct(bool $signed = true)
20
    {
21 183
        $this->signed = $signed;
22 183
    }
23
24
    /**
25
     * Return the minimum allowed Signed value.
26
     *
27
     * @return int|float
28
     */
29
    abstract protected function minSignedValue();
30
31
    /**
32
     * Return the maximum allowed Signed value.
33
     *
34
     * @return int|float
35
     */
36
    abstract protected function maxSignedValue();
37
38
    /**
39
     * Return the minimum allowed Unsigned value.
40
     *
41
     * @return int|float
42
     */
43
    abstract protected function minUnSignedValue();
44
45
    /**
46
     * Return the maximum allowed Unsigned value.
47
     *
48
     * @return int|float
49
     */
50
    abstract protected function maxUnsignedValue();
51
52
    /**
53
     * Ensures that the field's value is a valid Mysql standard int.
54
     *
55
     * @param object $subject
56
     * @param string $field
57
     *
58
     * @return bool
59
     */
60 156
    public function __invoke($subject, string $field): bool
61
    {
62 156
        $value = $subject->$field;
63
64 156
        return $this->isInteger($value) && $this->isWithinBounds($value);
65
    }
66
67
    /**
68
     * Indicates if the given value is within
69
     *
70
     * @param int $value
71
     *
72
     * @return bool
73
     */
74 174
    protected function isWithinBounds($value): bool
75
    {
76 174
        if ($this->signed) {
77 93
            return $value >= $this->minSignedValue() && $value <= $this->maxSignedValue();
78
        }
79
80 81
        return $value >= $this->minUnSignedValue() && $value <= $this->maxUnsignedValue();
81
    }
82
}
83