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

MultipleOf   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A validate() 0 21 3
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