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