@@ 11-43 (lines=33) @@ | ||
8 | use League\JsonGuard\Validator; |
|
9 | use function League\JsonGuard\error; |
|
10 | ||
11 | final class MaxLength implements ConstraintInterface |
|
12 | { |
|
13 | const KEYWORD = 'maxLength'; |
|
14 | ||
15 | /** |
|
16 | * @var string |
|
17 | */ |
|
18 | private $charset; |
|
19 | ||
20 | /** |
|
21 | * @param string $charset |
|
22 | */ |
|
23 | public function __construct($charset = 'UTF-8') |
|
24 | { |
|
25 | $this->charset = $charset; |
|
26 | } |
|
27 | ||
28 | ||
29 | /** |
|
30 | * {@inheritdoc} |
|
31 | */ |
|
32 | public function validate($value, $parameter, Validator $validator) |
|
33 | { |
|
34 | Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath()); |
|
35 | Assert::nonNegative($parameter, self::KEYWORD, $validator->getSchemaPath()); |
|
36 | ||
37 | if (!is_string($value) || JsonGuard\strlen($value, $this->charset) <= $parameter) { |
|
38 | return null; |
|
39 | } |
|
40 | ||
41 | return error('The string must be less than {parameter} characters long.', $validator); |
|
42 | } |
|
43 | } |
|
44 |
@@ 11-42 (lines=32) @@ | ||
8 | use League\JsonGuard\Validator; |
|
9 | use function League\JsonGuard\error; |
|
10 | ||
11 | final class MinLength implements ConstraintInterface |
|
12 | { |
|
13 | const KEYWORD = 'minLength'; |
|
14 | ||
15 | /** |
|
16 | * @var string |
|
17 | */ |
|
18 | private $charset; |
|
19 | ||
20 | /** |
|
21 | * @param string $charset |
|
22 | */ |
|
23 | public function __construct($charset = 'UTF-8') |
|
24 | { |
|
25 | $this->charset = $charset; |
|
26 | } |
|
27 | ||
28 | /** |
|
29 | * {@inheritdoc} |
|
30 | */ |
|
31 | public function validate($value, $parameter, Validator $validator) |
|
32 | { |
|
33 | Assert::type($parameter, 'number', self::KEYWORD, $validator->getSchemaPath()); |
|
34 | Assert::nonNegative($parameter, self::KEYWORD, $validator->getSchemaPath()); |
|
35 | ||
36 | if (!is_string($value) || JsonGuard\strlen($value, $this->charset) >= $parameter) { |
|
37 | return null; |
|
38 | } |
|
39 | ||
40 | return error('The string must be at least {parameter} characters long.', $validator); |
|
41 | } |
|
42 | } |
|
43 |