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