MultipleOf::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraint\DraftFour;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\ConstraintInterface;
7
use League\JsonGuard\Validator;
8
use function League\JsonGuard\error;
9
10
final class MultipleOf implements ConstraintInterface
11
{
12
    const KEYWORD = 'multipleOf';
13
14
    /**
15
     * @var int|null
16
     */
17
    private $precision;
18
19
    /**
20
     * @param int|null $precision
21
     */
22 8
    public function __construct($precision = 10)
23
    {
24 8
        $this->precision = $precision;
25 8
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 8
    public function validate($value, $parameter, Validator $validator)
31
    {
32 8
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath());
33 6
        Assert::nonNegative($parameter, self::KEYWORD, $validator->getSchemaPath());
34
35 4
        if (!is_numeric($value)) {
36 2
            return null;
37
        }
38
39 4
        $quotient = bcdiv($value, $parameter, $this->precision);
40 4
        $mod      = bcsub($quotient, floor($quotient), $this->precision);
41 4
        if (bccomp($mod, 0, $this->precision) === 0) {
42 4
            return null;
43
        }
44
45 2
        return error('The number must be a multiple of {parameter}.', $validator);
46
    }
47
}
48