@@ 9-31 (lines=23) @@ | ||
6 | use League\JsonGuard\ErrorCode; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class Enum implements PropertyConstraint |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public static function validate($value, $parameter, $pointer = null) |
|
15 | { |
|
16 | if (!is_array($parameter)) { |
|
17 | return null; |
|
18 | } |
|
19 | ||
20 | if (in_array($value, $parameter, true)) { |
|
21 | return null; |
|
22 | } |
|
23 | ||
24 | $message = sprintf( |
|
25 | 'Value "%s" is not one of: %s', |
|
26 | JsonGuard\asString($value), |
|
27 | implode(', ', array_map('League\JsonGuard\asString', $parameter)) |
|
28 | ); |
|
29 | return new ValidationError($message, ErrorCode::INVALID_ENUM, $value, $pointer, ['choices' => $parameter]); |
|
30 | } |
|
31 | } |
|
32 |
@@ 9-27 (lines=19) @@ | ||
6 | use League\JsonGuard\ErrorCode; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class Pattern implements PropertyConstraint |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public static function validate($value, $pattern, $pointer = null) |
|
15 | { |
|
16 | if (!is_string($value)) { |
|
17 | return null; |
|
18 | } |
|
19 | ||
20 | if (preg_match(JsonGuard\delimitPattern($pattern), $value) === 1) { |
|
21 | return null; |
|
22 | } |
|
23 | ||
24 | $message = sprintf('Value "%s" does not match the given pattern.', JsonGuard\asString($value)); |
|
25 | return new ValidationError($message, ErrorCode::INVALID_PATTERN, $value, $pointer, ['pattern' => $pattern]); |
|
26 | } |
|
27 | } |
|
28 |