| @@ 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\asString($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\asString($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-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\asString($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\asString($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-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\asString($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 | ||
| @@ 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\asString($min)); |
|
| 21 | return new ValidationError( |
|
| 22 | $message, |
|
| 23 | ErrorCode::INVALID_MIN_COUNT, |
|
| 24 | $value, |
|
| 25 | $pointer, |
|
| 26 | ['min_properties' => $min] |
|
| 27 | ); |
|
| 28 | } |
|
| 29 | } |
|
| 30 | ||
| @@ 8-29 (lines=22) @@ | ||
| 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 | $actualProperties = array_keys(get_object_vars($data)); |
|
| 16 | $missing = array_diff($parameter, $actualProperties); |
|
| 17 | if (count($missing)) { |
|
| 18 | return new ValidationError( |
|
| 19 | 'Required properties missing.', |
|
| 20 | ErrorCode::MISSING_REQUIRED, |
|
| 21 | $data, |
|
| 22 | $pointer, |
|
| 23 | ['required' => $parameter] |
|
| 24 | ); |
|
| 25 | } |
|
| 26 | ||
| 27 | return null; |
|
| 28 | } |
|
| 29 | } |
|
| 30 | ||