Completed
Push — master ( feafc2...e07893 )
by Matt
12s
created

src/Constraints/Max.php (2 issues)

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\Assert;
7
use League\JsonGuard\ValidationError;
8
use League\JsonGuard\Validator;
9
10
class Max implements Constraint
11
{
12
    const KEYWORD           = 'maximum';
13
    const EXCLUSIVE_KEYWORD = 'exclusiveMaximum';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 16
    public function validate($value, $parameter, Validator $validator)
19
    {
20 16
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getPointer());
21
22 14
        if (isset($validator->getSchema()->exclusiveMaximum) && $validator->getSchema()->exclusiveMaximum === true) {
23 6
            return self::validateExclusiveMax($value, $parameter, $validator->getPointer());
24
        }
25
26 14
        return self::validateMax($value, $parameter, $validator->getPointer());
27
    }
28
29
    /**
30
     * @param mixed       $value
31
     * @param mixed       $parameter
32
     * @param string|null $pointer
33
     *
34
     * @return \League\JsonGuard\ValidationError|null
35
     */
36 14 View Code Duplication
    public static function validateMax($value, $parameter, $pointer)
0 ignored issues
show
This method 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...
37
    {
38 14
        if (!is_numeric($value) ||
39 14
            JsonGuard\compare($value, $parameter) === -1 || JsonGuard\compare($value, $parameter) === 0) {
40 14
            return null;
41
        }
42
43 12
        return new ValidationError(
44 12
            'Value {value} is not at most {max}',
45 12
            self::KEYWORD,
46 12
            $value,
47 12
            $pointer,
48 12
            ['value' => $value, 'max' => $parameter]
49 12
        );
50
    }
51
52
    /**
53
     * @param mixed       $value
54
     * @param mixed       $parameter
55
     * @param string|null $pointer
56
     *
57
     * @return \League\JsonGuard\ValidationError|null
58
     */
59 6 View Code Duplication
    public static function validateExclusiveMax($value, $parameter, $pointer)
0 ignored issues
show
This method 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...
60
    {
61 6
        if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === -1) {
62 4
            return null;
63
        }
64
65 6
        return new ValidationError(
66 6
            'Value {value} is not less than {exclusive_max}',
67 6
            self::EXCLUSIVE_KEYWORD,
68 6
            $value,
69 6
            $pointer,
70 6
            ['value' => $value, 'exclusive_max' => $parameter]
71 6
        );
72
    }
73
}
74