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

src/Constraints/Min.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\ErrorCode;
7
use League\JsonGuard\ValidationError;
8
9 View Code Duplication
class Min 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...
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14 26
    public static function validate($value, $schema, $parameter, $pointer = null)
15
    {
16 26
        if (isset($schema->exclusiveMinimum) && $schema->exclusiveMinimum === true) {
17 8
            return self::validateExclusiveMin($value, $parameter, $pointer);
18
        }
19
20 24
        return self::validateMin($value, $parameter, $pointer);
21
    }
22
23
    /**
24
     * @param mixed       $value
25
     * @param mixed       $parameter
26
     * @param string|null $pointer
27
     *
28
     * @return \League\JsonGuard\ValidationError|null
29
     */
30 24
    public static function validateMin($value, $parameter, $pointer = null)
31
    {
32 24
        if (!is_numeric($value) ||
33 24
            JsonGuard\compare($value, $parameter) === 1 || JsonGuard\compare($value, $parameter) === 0) {
34 24
            return null;
35
        }
36
37 16
        $message = sprintf(
38 16
            'Number "%s" is not at least "%d"',
39 16
            JsonGuard\as_string($value),
40 16
            JsonGuard\as_string($parameter)
41 16
        );
42
43 16
        return new ValidationError($message, ErrorCode::INVALID_MIN, $value, $pointer, ['min' => $parameter]);
44
    }
45
46
    /**
47
     * @param mixed       $value
48
     * @param mixed       $parameter
49
     * @param string|null $pointer
50
     *
51
     * @return \League\JsonGuard\ValidationError|null
52
     */
53 8
    public static function validateExclusiveMin($value, $parameter, $pointer = null)
54
    {
55 8
        if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === 1) {
56 6
            return null;
57
        }
58
59 6
        $message = sprintf(
60 6
            'Number "%s" is not at least greater than "%d"',
61 6
            JsonGuard\as_string($value),
62 6
            JsonGuard\as_string($parameter)
63 6
        );
64
65 6
        return new ValidationError(
66 6
            $message,
67 6
            ErrorCode::INVALID_EXCLUSIVE_MIN,
68 6
            $value,
69 6
            $pointer,
70 6
            ['exclusive_min' => $parameter]
71 6
        );
72
    }
73
}
74