Completed
Push — master ( feafc2...e07893 )
by Matt
12s
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\Assert;
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
    const KEYWORD           = 'minimum';
12
    const EXCLUSIVE_KEYWORD = 'exclusiveMinimum';
13
14
    /**
15
     * {@inheritdoc}
16
     */
17 28
    public static function validate($value, $schema, $parameter, $pointer = null)
18
    {
19 28
        Assert::type($parameter, 'number', self::KEYWORD, $pointer);
20
21 26
        if (isset($schema->exclusiveMinimum) && $schema->exclusiveMinimum === true) {
22 8
            return self::validateExclusiveMin($value, $parameter, $pointer);
23
        }
24
25 24
        return self::validateMin($value, $parameter, $pointer);
26
    }
27
28
    /**
29
     * @param mixed       $value
30
     * @param mixed       $parameter
31
     * @param string|null $pointer
32
     *
33
     * @return \League\JsonGuard\ValidationError|null
34
     */
35 24
    public static function validateMin($value, $parameter, $pointer = null)
36
    {
37 24
        if (!is_numeric($value) ||
38 24
            JsonGuard\compare($value, $parameter) === 1 || JsonGuard\compare($value, $parameter) === 0) {
39 24
            return null;
40
        }
41
42 16
        return new ValidationError(
43 16
            'Number {value} is not at least {min}',
44 16
            self::KEYWORD,
45 16
            $value,
46 16
            $pointer,
47 16
            ['value' => $value, 'min' => $parameter]
48 16
        );
49
    }
50
51
    /**
52
     * @param mixed       $value
53
     * @param mixed       $parameter
54
     * @param string|null $pointer
55
     *
56
     * @return \League\JsonGuard\ValidationError|null
57
     */
58 8
    public static function validateExclusiveMin($value, $parameter, $pointer = null)
59
    {
60 8
        if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === 1) {
61 6
            return null;
62
        }
63
64 6
        return new ValidationError(
65 6
            'Number {value} is not at least greater than {exclusive_min}',
66 6
            self::EXCLUSIVE_KEYWORD,
67 6
            $value,
68 6
            $pointer,
69 6
            ['value' => $value, 'exclusive_min' => $parameter]
70 6
        );
71
    }
72
}
73