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

src/Constraints/Max.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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