Decimal::check()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 5
eloc 12
c 2
b 0
f 0
nc 5
nop 1
dl 0
loc 24
ccs 0
cts 9
cp 0
crap 30
rs 9.5555
1
<?php
2
3
/**
4
 * This file is part of Dimtrovich/Validation.
5
 *
6
 * (c) 2023 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace Dimtrovich\Validation\Rules;
13
14
class Decimal extends AbstractRule
15
{
16
    /**
17
     * @var array
18
     */
19
    protected $fillableParams = ['min', 'max'];
20
21
    /**
22
     * {@inheritDoc}
23
     */
24
    public function check($value): bool
25
    {
26
        if (! is_numeric($value)) {
27
            return false;
28
        }
29
30
        $this->requireParameters(['min']);
31
32
        $min = $this->parameter('min');
33
        $max = $this->parameter('max');
34
35
        $matches = [];
36
37
        if (preg_match('/^[+-]?\d*\.?(\d*)$/', $value, $matches) !== 1) {
38
            return false;
39
        }
40
41
        $decimals = strlen(end($matches));
42
43
        if (empty($max)) {
44
            return (int) $decimals === (int) $min;
45
        }
46
47
        return $decimals >= $min && $decimals <= $max;
48
    }
49
}
50