@@ 9-72 (lines=64) @@ | ||
6 | use League\JsonGuard\Assert; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class Max implements ParentSchemaAwarePropertyConstraint |
|
10 | { |
|
11 | const KEYWORD = 'maximum'; |
|
12 | const EXCLUSIVE_KEYWORD = 'exclusiveMaximum'; |
|
13 | ||
14 | /** |
|
15 | * {@inheritdoc} |
|
16 | */ |
|
17 | public static function validate($value, $schema, $parameter, $pointer = null) |
|
18 | { |
|
19 | Assert::type($parameter, 'number', self::KEYWORD, $pointer); |
|
20 | ||
21 | if (isset($schema->exclusiveMaximum) && $schema->exclusiveMaximum === true) { |
|
22 | return self::validateExclusiveMax($value, $parameter, $pointer); |
|
23 | } |
|
24 | ||
25 | return self::validateMax($value, $parameter, $pointer); |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param mixed $value |
|
30 | * @param mixed $parameter |
|
31 | * @param string|null $pointer |
|
32 | * |
|
33 | * @return \League\JsonGuard\ValidationError|null |
|
34 | */ |
|
35 | public static function validateMax($value, $parameter, $pointer = null) |
|
36 | { |
|
37 | if (!is_numeric($value) || |
|
38 | JsonGuard\compare($value, $parameter) === -1 || JsonGuard\compare($value, $parameter) === 0) { |
|
39 | return null; |
|
40 | } |
|
41 | ||
42 | return new ValidationError( |
|
43 | 'Value {value} is not at most {max}', |
|
44 | self::KEYWORD, |
|
45 | $value, |
|
46 | $pointer, |
|
47 | ['value' => $value, 'max' => $parameter] |
|
48 | ); |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * @param mixed $value |
|
53 | * @param mixed $parameter |
|
54 | * @param string|null $pointer |
|
55 | * |
|
56 | * @return \League\JsonGuard\ValidationError|null |
|
57 | */ |
|
58 | public static function validateExclusiveMax($value, $parameter, $pointer = null) |
|
59 | { |
|
60 | if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === -1) { |
|
61 | return null; |
|
62 | } |
|
63 | ||
64 | return new ValidationError( |
|
65 | 'Value {value} is not less than {exclusive_max}', |
|
66 | self::EXCLUSIVE_KEYWORD, |
|
67 | $value, |
|
68 | $pointer, |
|
69 | ['value' => $value, 'exclusive_max' => $parameter] |
|
70 | ); |
|
71 | } |
|
72 | } |
|
73 |
@@ 9-72 (lines=64) @@ | ||
6 | use League\JsonGuard\Assert; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class Min implements ParentSchemaAwarePropertyConstraint |
|
10 | { |
|
11 | const KEYWORD = 'minimum'; |
|
12 | const EXCLUSIVE_KEYWORD = 'exclusiveMinimum'; |
|
13 | ||
14 | /** |
|
15 | * {@inheritdoc} |
|
16 | */ |
|
17 | public static function validate($value, $schema, $parameter, $pointer = null) |
|
18 | { |
|
19 | Assert::type($parameter, 'number', self::KEYWORD, $pointer); |
|
20 | ||
21 | if (isset($schema->exclusiveMinimum) && $schema->exclusiveMinimum === true) { |
|
22 | return self::validateExclusiveMin($value, $parameter, $pointer); |
|
23 | } |
|
24 | ||
25 | return self::validateMin($value, $parameter, $pointer); |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * @param mixed $value |
|
30 | * @param mixed $parameter |
|
31 | * @param string|null $pointer |
|
32 | * |
|
33 | * @return \League\JsonGuard\ValidationError|null |
|
34 | */ |
|
35 | public static function validateMin($value, $parameter, $pointer = null) |
|
36 | { |
|
37 | if (!is_numeric($value) || |
|
38 | JsonGuard\compare($value, $parameter) === 1 || JsonGuard\compare($value, $parameter) === 0) { |
|
39 | return null; |
|
40 | } |
|
41 | ||
42 | return new ValidationError( |
|
43 | 'Number {value} is not at least {min}', |
|
44 | self::KEYWORD, |
|
45 | $value, |
|
46 | $pointer, |
|
47 | ['value' => $value, 'min' => $parameter] |
|
48 | ); |
|
49 | } |
|
50 | ||
51 | /** |
|
52 | * @param mixed $value |
|
53 | * @param mixed $parameter |
|
54 | * @param string|null $pointer |
|
55 | * |
|
56 | * @return \League\JsonGuard\ValidationError|null |
|
57 | */ |
|
58 | public static function validateExclusiveMin($value, $parameter, $pointer = null) |
|
59 | { |
|
60 | if (!is_numeric($value) || JsonGuard\compare($value, $parameter) === 1) { |
|
61 | return null; |
|
62 | } |
|
63 | ||
64 | return new ValidationError( |
|
65 | 'Number {value} is not at least greater than {exclusive_min}', |
|
66 | self::EXCLUSIVE_KEYWORD, |
|
67 | $value, |
|
68 | $pointer, |
|
69 | ['value' => $value, 'exclusive_min' => $parameter] |
|
70 | ); |
|
71 | } |
|
72 | } |
|
73 |