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 Max implements Constraint |
10
|
|
|
{ |
11
|
|
|
const KEYWORD = 'maximum'; |
12
|
|
|
const EXCLUSIVE_KEYWORD = 'exclusiveMaximum'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var int|null |
16
|
|
|
*/ |
17
|
|
|
private $precision; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param int|null $precision |
21
|
|
|
*/ |
22
|
10 |
|
public function __construct($precision = 10) |
23
|
|
|
{ |
24
|
10 |
|
$this->precision = $precision; |
25
|
10 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
10 |
|
public function validate($value, $parameter, Validator $validator) |
31
|
|
|
{ |
32
|
10 |
|
Assert::type($parameter, 'number', self::KEYWORD, $validator->getPointer()); |
33
|
|
|
|
34
|
8 |
|
if (isset($validator->getSchema()->exclusiveMaximum) && $validator->getSchema()->exclusiveMaximum === true) { |
35
|
4 |
|
return self::validateExclusiveMax($value, $parameter, $validator->getPointer()); |
36
|
|
|
} |
37
|
|
|
|
38
|
8 |
|
return self::validateMax($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
|
8 |
View Code Duplication |
private function validateMax($value, $parameter, $pointer) |
|
|
|
|
49
|
|
|
{ |
50
|
8 |
|
if (!is_numeric($value) || |
51
|
8 |
|
bccomp($value, $parameter, $this->precision) === -1 || bccomp($value, $parameter, $this->precision) === 0) { |
52
|
8 |
|
return null; |
53
|
|
|
} |
54
|
|
|
|
55
|
6 |
|
return new ValidationError( |
56
|
6 |
|
'Value {value} is not at most {max}', |
57
|
6 |
|
self::KEYWORD, |
58
|
6 |
|
$value, |
59
|
6 |
|
$pointer, |
60
|
6 |
|
['value' => $value, 'max' => $parameter] |
61
|
6 |
|
); |
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
|
4 |
View Code Duplication |
private function validateExclusiveMax($value, $parameter, $pointer) |
|
|
|
|
72
|
|
|
{ |
73
|
4 |
|
if (!is_numeric($value) || bccomp($value, $parameter, $this->precision) === -1) { |
74
|
2 |
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
return new ValidationError( |
78
|
4 |
|
'Value {value} is not less than {exclusive_max}', |
79
|
4 |
|
self::EXCLUSIVE_KEYWORD, |
80
|
4 |
|
$value, |
81
|
4 |
|
$pointer, |
82
|
4 |
|
['value' => $value, 'exclusive_max' => $parameter] |
83
|
4 |
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
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.