@@ 10-45 (lines=36) @@ | ||
7 | use League\JsonGuard\Validator; |
|
8 | use function League\JsonGuard\error; |
|
9 | ||
10 | final class Maximum implements ConstraintInterface |
|
11 | { |
|
12 | const KEYWORD = 'maximum'; |
|
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, 'number', self::KEYWORD, $validator->getSchemaPath()); |
|
33 | ||
34 | if (isset($validator->getSchema()->exclusiveMaximum) && $validator->getSchema()->exclusiveMaximum === true) { |
|
35 | return; |
|
36 | } |
|
37 | ||
38 | if (!is_numeric($value) || |
|
39 | bccomp($value, $parameter, $this->precision) === -1 || bccomp($value, $parameter, $this->precision) === 0) { |
|
40 | return null; |
|
41 | } |
|
42 | ||
43 | return error('The number must be less than {parameter}.', $validator); |
|
44 | } |
|
45 | } |
|
46 |
@@ 10-45 (lines=36) @@ | ||
7 | use League\JsonGuard\Validator; |
|
8 | use function League\JsonGuard\error; |
|
9 | ||
10 | final class Minimum implements ConstraintInterface |
|
11 | { |
|
12 | const KEYWORD = 'minimum'; |
|
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, 'number', self::KEYWORD, $validator->getSchemaPath()); |
|
33 | ||
34 | if (isset($validator->getSchema()->exclusiveMinimum) && $validator->getSchema()->exclusiveMinimum === true) { |
|
35 | return null; |
|
36 | } |
|
37 | ||
38 | if (!is_numeric($value) || |
|
39 | bccomp($value, $parameter, $this->precision) === 1 || bccomp($value, $parameter, $this->precision) === 0) { |
|
40 | return null; |
|
41 | } |
|
42 | ||
43 | return error('The number must be at least {parameter}.', $validator); |
|
44 | } |
|
45 | } |
|
46 |