Completed
Push — master ( e24db7...5dd27b )
by Matt
02:58
created

Max::validateExclusiveMax()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 9

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 2
nop 3
dl 14
loc 14
ccs 10
cts 10
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard;
6
use League\JsonGuard\ValidationError;
7
8 View Code Duplication
class Max implements ParentSchemaAwarePropertyConstraint
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
9
{
10
    const KEYWORD           = 'max';
11
    const EXCLUSIVE_KEYWORD = 'exclusiveMax';
12
13
    /**
14
     * {@inheritdoc}
15
     */
16 14
    public static function validate($value, $schema, $parameter, $pointer = null)
17
    {
18 14
        if (isset($schema->exclusiveMaximum) && $schema->exclusiveMaximum === true) {
19 6
            return self::validateExclusiveMax($value, $parameter, $pointer);
20
        }
21
22 14
        return self::validateMax($value, $parameter, $pointer);
23
    }
24
25
    /**
26
     * @param mixed       $value
27
     * @param mixed       $parameter
28
     * @param string|null $pointer
29
     *
30
     * @return \League\JsonGuard\ValidationError|null
31
     */
32 14
    public static function validateMax($value, $parameter, $pointer = null)
33
    {
34 14
        if (!is_numeric($value) ||
35 14
            JsonGuard\compare($value, $parameter) === -1 || JsonGuard\compare($value, $parameter) === 0) {
36 14
            return null;
37
        }
38
39 12
        return new ValidationError(
40 12
            'Value {value} is not at most {max}',
41 12
            self::KEYWORD,
42 12
            $value,
43 12
            $pointer,
44 12
            ['value' => $value, 'max' => $parameter]
45 12
        );
46
    }
47
48
    /**
49
     * @param mixed       $value
50
     * @param mixed       $parameter
51
     * @param string|null $pointer
52
     *
53
     * @return \League\JsonGuard\ValidationError|null
54
     */
55 6
    public static function validateExclusiveMax($value, $parameter, $pointer = null)
56
    {
57 6
        if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === -1) {
58 4
            return null;
59
        }
60
61 6
        return new ValidationError(
62 6
            'Value {value} is not less than {exclusive_max}',
63 6
            self::EXCLUSIVE_KEYWORD,
64 6
            $value,
65 6
            $pointer,
66 6
            ['value' => $value, 'exclusive_max' => $parameter]
67 6
        );
68
    }
69
}
70