@@ 9-29 (lines=21) @@ | ||
6 | use League\JsonGuard\ErrorCode; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class MaxProperties implements PropertyConstraint |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public static function validate($value, $parameter, $pointer = null) |
|
15 | { |
|
16 | if (!is_object($value) || count(get_object_vars($value)) <= $parameter) { |
|
17 | return null; |
|
18 | } |
|
19 | ||
20 | $message = sprintf('Object does not contain less than "%d" properties', JsonGuard\as_string($parameter)); |
|
21 | return new ValidationError( |
|
22 | $message, |
|
23 | ErrorCode::MAX_PROPERTIES_EXCEEDED, |
|
24 | $value, |
|
25 | $pointer, |
|
26 | ['max_properties' => $parameter] |
|
27 | ); |
|
28 | } |
|
29 | } |
|
30 |
@@ 9-29 (lines=21) @@ | ||
6 | use League\JsonGuard\ErrorCode; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class MinItems implements PropertyConstraint |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public static function validate($value, $parameter, $pointer = null) |
|
15 | { |
|
16 | if (!is_array($value) || count($value) >= $parameter) { |
|
17 | return null; |
|
18 | } |
|
19 | ||
20 | $message = sprintf('Array does not contain more than "%d" items', JsonGuard\as_string($parameter)); |
|
21 | return new ValidationError( |
|
22 | $message, |
|
23 | ErrorCode::INVALID_MIN_COUNT, |
|
24 | $value, |
|
25 | $pointer, |
|
26 | ['min_items' => $parameter] |
|
27 | ); |
|
28 | } |
|
29 | } |
|
30 |
@@ 9-29 (lines=21) @@ | ||
6 | use League\JsonGuard\ErrorCode; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class MinProperties implements PropertyConstraint |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public static function validate($value, $min, $pointer = null) |
|
15 | { |
|
16 | if (!is_object($value) || count(get_object_vars($value)) >= $min) { |
|
17 | return null; |
|
18 | } |
|
19 | ||
20 | $message = sprintf('Object does not contain at least "%d" properties', JsonGuard\as_string($min)); |
|
21 | return new ValidationError( |
|
22 | $message, |
|
23 | ErrorCode::INVALID_MIN_COUNT, |
|
24 | $value, |
|
25 | $pointer, |
|
26 | ['min_properties' => $min] |
|
27 | ); |
|
28 | } |
|
29 | } |
|
30 |
@@ 9-29 (lines=21) @@ | ||
6 | use League\JsonGuard\ErrorCode; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class MaxItems implements PropertyConstraint |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public static function validate($value, $parameter, $pointer = null) |
|
15 | { |
|
16 | if (!is_array($value) || count($value) <= $parameter) { |
|
17 | return null; |
|
18 | } |
|
19 | ||
20 | $message = sprintf('Array does not contain less than "%d" items', JsonGuard\as_string($parameter)); |
|
21 | return new ValidationError( |
|
22 | $message, |
|
23 | ErrorCode::MAX_ITEMS_EXCEEDED, |
|
24 | $value, |
|
25 | $pointer, |
|
26 | ['max_items' => $parameter] |
|
27 | ); |
|
28 | } |
|
29 | } |
|
30 |
@@ 9-29 (lines=21) @@ | ||
6 | use League\JsonGuard\ErrorCode; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class MaxLength implements PropertyConstraint |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public static function validate($value, $parameter, $pointer = null) |
|
15 | { |
|
16 | if (!is_string($value) || JsonGuard\strlen($value) <= $parameter) { |
|
17 | return null; |
|
18 | } |
|
19 | ||
20 | $message = sprintf('String is not at most "%s" characters long', JsonGuard\as_string($parameter)); |
|
21 | return new ValidationError( |
|
22 | $message, |
|
23 | ErrorCode::INVALID_MAX_LENGTH, |
|
24 | $value, |
|
25 | $pointer, |
|
26 | ['max_length' => $parameter] |
|
27 | ); |
|
28 | } |
|
29 | } |
|
30 |
@@ 9-30 (lines=22) @@ | ||
6 | use League\JsonGuard\ErrorCode; |
|
7 | use League\JsonGuard\ValidationError; |
|
8 | ||
9 | class MinLength implements PropertyConstraint |
|
10 | { |
|
11 | /** |
|
12 | * {@inheritdoc} |
|
13 | */ |
|
14 | public static function validate($value, $parameter, $pointer = null) |
|
15 | { |
|
16 | if (!is_string($value) || JsonGuard\strlen($value) >= $parameter) { |
|
17 | return null; |
|
18 | } |
|
19 | ||
20 | $message = sprintf('String is not at least "%s" characters long', JsonGuard\as_string($parameter)); |
|
21 | ||
22 | return new ValidationError( |
|
23 | $message, |
|
24 | ErrorCode::INVALID_MIN_LENGTH, |
|
25 | $value, |
|
26 | $pointer, |
|
27 | ['min_length' => $parameter] |
|
28 | ); |
|
29 | } |
|
30 | } |
|
31 |
@@ 8-33 (lines=26) @@ | ||
5 | use League\JsonGuard\ErrorCode; |
|
6 | use League\JsonGuard\ValidationError; |
|
7 | ||
8 | class Required implements PropertyConstraint |
|
9 | { |
|
10 | /** |
|
11 | * {@inheritdoc} |
|
12 | */ |
|
13 | public static function validate($data, $parameter, $pointer = null) |
|
14 | { |
|
15 | if (!is_object($data)) { |
|
16 | return null; |
|
17 | } |
|
18 | ||
19 | $actualProperties = array_keys(get_object_vars($data)); |
|
20 | $missing = array_diff($parameter, $actualProperties); |
|
21 | if (count($missing)) { |
|
22 | return new ValidationError( |
|
23 | 'Required properties missing.', |
|
24 | ErrorCode::MISSING_REQUIRED, |
|
25 | $data, |
|
26 | $pointer, |
|
27 | ['required' => $parameter] |
|
28 | ); |
|
29 | } |
|
30 | ||
31 | return null; |
|
32 | } |
|
33 | } |
|
34 |