Code Duplication    Length = 62-62 lines in 2 locations

src/Constraints/Max.php 1 location

@@ 8-69 (lines=62) @@
5
use League\JsonGuard;
6
use League\JsonGuard\ValidationError;
7
8
class Max implements ParentSchemaAwarePropertyConstraint
9
{
10
    const KEYWORD           = 'max';
11
    const EXCLUSIVE_KEYWORD = 'exclusiveMax';
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public static function validate($value, $schema, $parameter, $pointer = null)
17
    {
18
        if (isset($schema->exclusiveMaximum) && $schema->exclusiveMaximum === true) {
19
            return self::validateExclusiveMax($value, $parameter, $pointer);
20
        }
21
22
        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
    public static function validateMax($value, $parameter, $pointer = null)
33
    {
34
        if (!is_numeric($value) ||
35
            JsonGuard\compare($value, $parameter) === -1 || JsonGuard\compare($value, $parameter) === 0) {
36
            return null;
37
        }
38
39
        return new ValidationError(
40
            'Value {value} is not at most {max}',
41
            self::KEYWORD,
42
            $value,
43
            $pointer,
44
            ['value' => $value, 'max' => $parameter]
45
        );
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
    public static function validateExclusiveMax($value, $parameter, $pointer = null)
56
    {
57
        if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === -1) {
58
            return null;
59
        }
60
61
        return new ValidationError(
62
            'Value {value} is not less than {exclusive_max}',
63
            self::EXCLUSIVE_KEYWORD,
64
            $value,
65
            $pointer,
66
            ['value' => $value, 'exclusive_max' => $parameter]
67
        );
68
    }
69
}
70

src/Constraints/Min.php 1 location

@@ 8-69 (lines=62) @@
5
use League\JsonGuard;
6
use League\JsonGuard\ValidationError;
7
8
class Min implements ParentSchemaAwarePropertyConstraint
9
{
10
    const KEYWORD           = 'min';
11
    const EXCLUSIVE_KEYWORD = 'exclusiveMin';
12
13
    /**
14
     * {@inheritdoc}
15
     */
16
    public static function validate($value, $schema, $parameter, $pointer = null)
17
    {
18
        if (isset($schema->exclusiveMinimum) && $schema->exclusiveMinimum === true) {
19
            return self::validateExclusiveMin($value, $parameter, $pointer);
20
        }
21
22
        return self::validateMin($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
    public static function validateMin($value, $parameter, $pointer = null)
33
    {
34
        if (!is_numeric($value) ||
35
            JsonGuard\compare($value, $parameter) === 1 || JsonGuard\compare($value, $parameter) === 0) {
36
            return null;
37
        }
38
39
        return new ValidationError(
40
            'Number {value} is not at least {min}',
41
            self::KEYWORD,
42
            $value,
43
            $pointer,
44
            ['value' => $value, 'min' => $parameter]
45
        );
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
    public static function validateExclusiveMin($value, $parameter, $pointer = null)
56
    {
57
        if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === 1) {
58
            return null;
59
        }
60
61
        return new ValidationError(
62
            'Number {value} is not at least greater than {exclusive_min}',
63
            self::EXCLUSIVE_KEYWORD,
64
            $value,
65
            $pointer,
66
            ['value' => $value, 'exclusive_min' => $parameter]
67
        );
68
    }
69
}
70