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