Completed
Push — master ( be7319...157c74 )
by Matt
01:48
created

MultipleOf::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 3
dl 0
loc 21
ccs 15
cts 15
cp 1
crap 3
rs 9.3142
c 0
b 0
f 0
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 4
    public static function validate($value, $multiple, $pointer = null)
15
    {
16 4
        if (!is_numeric($value)) {
17 4
            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 4
        $quotient = $value / $multiple;
22 4
        $mod      = $quotient - floor($quotient);
23 4
        if ($mod == 0) {
24 4
            return null;
25
        }
26
27 4
        return new ValidationError(
28 4
            'Number {value} is not a multiple of {multiple_of}',
29 4
            ErrorCode::INVALID_MULTIPLE,
30 4
            $value,
31 4
            $pointer,
32 4
            ['value' => $value, 'multiple_of' => $multiple]
33 4
        );
34 4
    }
35
}
36