Completed
Pull Request — master (#108)
by Matt
04:36
created

Min::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Constraints;
4
5
use League\JsonGuard\Assert;
6
use League\JsonGuard\ValidationError;
7
use League\JsonGuard\Validator;
8
9
class Min implements Constraint
10
{
11
    const KEYWORD           = 'minimum';
12
    const EXCLUSIVE_KEYWORD = 'exclusiveMinimum';
13
14
    /**
15
     * @var int|null
16
     */
17
    private $precision;
18
19
    /**
20
     * @param int|null $precision
21
     */
22 18
    public function __construct($precision = 10)
23
    {
24 18
        $this->precision = $precision;
25 18
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 18
    public function validate($value, $parameter, Validator $validator)
31
    {
32 18
        Assert::type($parameter, 'number', self::KEYWORD, $validator->getPointer());
33
34 16
        if (isset($validator->getSchema()->exclusiveMinimum) && $validator->getSchema()->exclusiveMinimum === true) {
35 6
            return self::validateExclusiveMin($value, $parameter, $validator->getPointer());
36
        }
37
38 14
        return self::validateMin($value, $parameter, $validator->getPointer());
39
    }
40
41
    /**
42
     * @param mixed       $value
43
     * @param mixed       $parameter
44
     * @param string|null $pointer
45
     *
46
     * @return \League\JsonGuard\ValidationError|null
47
     */
48 14 View Code Duplication
    private function validateMin($value, $parameter, $pointer = null)
0 ignored issues
show
Duplication introduced by
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...
49
    {
50 14
        if (!is_numeric($value) ||
51 14
            bccomp($value, $parameter, $this->precision) === 1 || bccomp($value, $parameter, $this->precision) === 0) {
52 14
            return null;
53
        }
54
55 8
        return new ValidationError(
56 8
            'Number {value} is not at least {min}',
57 8
            self::KEYWORD,
58 8
            $value,
59 8
            $pointer,
60 8
            ['value' => $value, 'min' => $parameter]
61 8
        );
62
    }
63
64
    /**
65
     * @param mixed       $value
66
     * @param mixed       $parameter
67
     * @param string|null $pointer
68
     *
69
     * @return \League\JsonGuard\ValidationError|null
70
     */
71 6 View Code Duplication
    private function validateExclusiveMin($value, $parameter, $pointer = null)
0 ignored issues
show
Duplication introduced by
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...
72
    {
73 6
        if (!is_numeric($value) || bccomp($value, $parameter, $this->precision) === 1) {
74 4
            return null;
75
        }
76
77 4
        return new ValidationError(
78 4
            'Number {value} is not at least greater than {exclusive_min}',
79 4
            self::EXCLUSIVE_KEYWORD,
80 4
            $value,
81 4
            $pointer,
82 4
            ['value' => $value, 'exclusive_min' => $parameter]
83 4
        );
84
    }
85
}
86