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
use League\JsonGuard\Validator;
9
10
class Min implements Constraint
11
{
12
    const KEYWORD           = 'minimum';
13
    const EXCLUSIVE_KEYWORD = 'exclusiveMinimum';
14
15
    /**
16
     * {@inheritdoc}
17
     */
18 28
    public function validate($value, $parameter, Validator $validator)
19
    {
20 28
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getPointer());
21
22 26
        if (isset($validator->getSchema()->exclusiveMinimum) && $validator->getSchema()->exclusiveMinimum === true) {
23 8
            return self::validateExclusiveMin($value, $parameter, $validator->getPointer());
24
        }
25
26 24
        return self::validateMin($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 24
    public static function validateMin($value, $parameter, $pointer = null)
37
    {
38 24
        if (!is_numeric($value) ||
39 24
            JsonGuard\compare($value, $parameter) === 1 || JsonGuard\compare($value, $parameter) === 0) {
40 24
            return null;
41
        }
42
43 16
        return new ValidationError(
44 16
            'Number {value} is not at least {min}',
45 16
            self::KEYWORD,
46 16
            $value,
47 16
            $pointer,
48 16
            ['value' => $value, 'min' => $parameter]
49 16
        );
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 8 View Code Duplication
    public static function validateExclusiveMin($value, $parameter, $pointer = null)
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 8
        if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === 1) {
62 6
            return null;
63
        }
64
65 6
        return new ValidationError(
66 6
            'Number {value} is not at least greater than {exclusive_min}',
67 6
            self::EXCLUSIVE_KEYWORD,
68 6
            $value,
69 6
            $pointer,
70 6
            ['value' => $value, 'exclusive_min' => $parameter]
71 6
        );
72
    }
73
}
74