Completed
Push — master ( 5927eb...1e13db )
by
unknown
04:04
created

MultipleOf::validate()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 3
Metric Value
cc 3
eloc 17
nc 3
nop 3
dl 0
loc 26
ccs 19
cts 19
cp 1
crap 3
rs 8.8571
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard;
6
use League\JsonGuard\ErrorCode;
7
use League\JsonGuard\ValidationError;
8
9
class MultipleOf implements PropertyConstraint
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 2
    public static function validate($value, $multiple, $pointer = null)
15
    {
16 2
        if (!is_numeric($value)) {
17 2
            return null;
18
        }
19
20
        // for some reason fmod does not return 0 for cases like fmod(0.0075,0.0001) so I'm doing this manually.
21 2
        $quotient = $value / $multiple;
22 2
        $mod      = $quotient - floor($quotient);
23 2
        if ($mod == 0) {
24 2
            return null;
25
        }
26
27 2
        $message = sprintf(
28 2
            'Number "%d" is not a multiple of "%d"',
29 2
            JsonGuard\asString($value),
30 2
            JsonGuard\asString($multiple)
31 2
        );
32 2
        return new ValidationError(
33 2
            $message,
34 2
            ErrorCode::INVALID_MULTIPLE,
35 2
            $value,
36 2
            $pointer,
37 2
            ['multiple_of' => $multiple]
38 2
        );
39
    }
40
}
41