@@ 10-45 (lines=36) @@ | ||
7 | use League\JsonGuard\Validator; |
|
8 | use function League\JsonGuard\error; |
|
9 | ||
10 | final class ExclusiveMaximum implements ConstraintInterface |
|
11 | { |
|
12 | const KEYWORD = 'exclusiveMaximum'; |
|
13 | ||
14 | /** |
|
15 | * @var int|null |
|
16 | */ |
|
17 | private $precision; |
|
18 | ||
19 | /** |
|
20 | * @param int|null $precision |
|
21 | */ |
|
22 | public function __construct($precision = 10) |
|
23 | { |
|
24 | $this->precision = $precision; |
|
25 | } |
|
26 | ||
27 | /** |
|
28 | * {@inheritdoc} |
|
29 | */ |
|
30 | public function validate($value, $parameter, Validator $validator) |
|
31 | { |
|
32 | Assert::type($parameter, 'boolean', self::KEYWORD, $validator->getSchemaPath()); |
|
33 | Assert::hasProperty($validator->getSchema(), 'maximum', self::KEYWORD, $validator->getSchemaPath()); |
|
34 | ||
35 | if ($parameter !== true) { |
|
36 | return null; |
|
37 | } |
|
38 | ||
39 | if (!is_numeric($value) || bccomp($value, $validator->getSchema()->maximum, $this->precision) === -1) { |
|
40 | return null; |
|
41 | } |
|
42 | ||
43 | return error('The number must be less than {parameter}.', $validator); |
|
44 | } |
|
45 | } |
|
46 |
@@ 10-49 (lines=40) @@ | ||
7 | use League\JsonGuard\Validator; |
|
8 | use function League\JsonGuard\error; |
|
9 | ||
10 | final class ExclusiveMinimum implements ConstraintInterface |
|
11 | { |
|
12 | const KEYWORD = 'exclusiveMinimum'; |
|
13 | ||
14 | /** |
|
15 | * @var int|null |
|
16 | */ |
|
17 | private $precision; |
|
18 | ||
19 | /** |
|
20 | * @param int|null $precision |
|
21 | */ |
|
22 | public function __construct($precision = 10) |
|
23 | { |
|
24 | $this->precision = $precision; |
|
25 | } |
|
26 | ||
27 | /** |
|
28 | * @param mixed $value |
|
29 | * @param mixed $parameter |
|
30 | * @param Validator $validator |
|
31 | * |
|
32 | * @return \League\JsonGuard\ValidationError|null |
|
33 | */ |
|
34 | public function validate($value, $parameter, Validator $validator) |
|
35 | { |
|
36 | Assert::type($parameter, 'boolean', self::KEYWORD, $validator->getSchemaPath()); |
|
37 | Assert::hasProperty($validator->getSchema(), 'minimum', self::KEYWORD, $validator->getSchemaPath()); |
|
38 | ||
39 | if ($parameter !== true) { |
|
40 | return null; |
|
41 | } |
|
42 | ||
43 | if (!is_numeric($value) || bccomp($value, $validator->getSchema()->minimum, $this->precision) === 1) { |
|
44 | return null; |
|
45 | } |
|
46 | ||
47 | return error('The number must be greater than {parameter}.', $validator); |
|
48 | } |
|
49 | } |
|
50 |