Completed
Push — master ( 5927eb...1e13db )
by
unknown
04:04
created

Min::validateMin()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 3
Metric Value
cc 3
eloc 8
nc 2
nop 3
dl 13
loc 13
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
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
Duplication introduced by
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 12
    public static function validate($value, $schema, $parameter, $pointer = null)
15
    {
16 12
        if (isset($schema->exclusiveMinimum) && $schema->exclusiveMinimum === true) {
17 4
            return self::validateExclusiveMin($value, $parameter, $pointer);
18
        }
19
20 10
        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 10
    public static function validateMin($value, $parameter, $pointer = null)
31
    {
32 10
        if (!is_numeric($value) || $value >= $parameter) {
33 10
            return null;
34
        }
35
36 8
        $message = sprintf(
37 8
            'Number "%s" is not at least "%d"',
38 8
            JsonGuard\asString($value),
39 8
            JsonGuard\asString($parameter)
40 8
        );
41 8
        return new ValidationError($message, ErrorCode::INVALID_MIN, $value, $pointer, ['min' => $parameter]);
42
    }
43
44
    /**
45
     * @param mixed       $value
46
     * @param mixed       $parameter
47
     * @param string|null $pointer
48
     *
49
     * @return \League\JsonGuard\ValidationError|null
50
     */
51 4
    public static function validateExclusiveMin($value, $parameter, $pointer = null)
52
    {
53 4
        if (!is_numeric($value) || $value > $parameter) {
54 4
            return null;
55
        }
56
57 2
        $message = sprintf(
58 2
            'Number "%s" is not at least greater than "%d"',
59 2
            JsonGuard\asString($value),
60 2
            JsonGuard\asString($parameter)
61 2
        );
62 2
        return new ValidationError(
63 2
            $message,
64 2
            ErrorCode::INVALID_EXCLUSIVE_MIN,
65 2
            $value,
66 2
            $pointer,
67 2
            ['exclusive_min' => $parameter]
68 2
        );
69
    }
70
}
71