| @@ 20-54 (lines=35) @@ | ||
| 17 | /** |
|
| 18 | * Constraint for the "maxLength" keyword. |
|
| 19 | */ |
|
| 20 | class MaxLengthConstraint extends AbstractCountConstraint |
|
| 21 | { |
|
| 22 | /** |
|
| 23 | * {@inheritdoc} |
|
| 24 | */ |
|
| 25 | public function keywords() |
|
| 26 | { |
|
| 27 | return ['maxLength']; |
|
| 28 | } |
|
| 29 | ||
| 30 | /** |
|
| 31 | * {@inheritdoc} |
|
| 32 | */ |
|
| 33 | public function supports($type) |
|
| 34 | { |
|
| 35 | return $type === Types::TYPE_STRING; |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * {@inheritdoc} |
|
| 40 | */ |
|
| 41 | public function apply($instance, stdClass $schema, Context $context, Walker $walker) |
|
| 42 | { |
|
| 43 | $length = extension_loaded('mbstring') ? |
|
| 44 | mb_strlen($instance, mb_detect_encoding($instance)) : |
|
| 45 | strlen($instance); |
|
| 46 | ||
| 47 | if ($length > $schema->maxLength) { |
|
| 48 | $context->addViolation( |
|
| 49 | 'should be lesser than or equal to %s characters', |
|
| 50 | [$schema->maxLength] |
|
| 51 | ); |
|
| 52 | } |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||
| @@ 20-54 (lines=35) @@ | ||
| 17 | /** |
|
| 18 | * Constraint for the "minLength" keyword. |
|
| 19 | */ |
|
| 20 | class MinLengthConstraint extends AbstractCountConstraint |
|
| 21 | { |
|
| 22 | /** |
|
| 23 | * {@inheritdoc} |
|
| 24 | */ |
|
| 25 | public function keywords() |
|
| 26 | { |
|
| 27 | return ['minLength']; |
|
| 28 | } |
|
| 29 | ||
| 30 | /** |
|
| 31 | * {@inheritdoc} |
|
| 32 | */ |
|
| 33 | public function supports($type) |
|
| 34 | { |
|
| 35 | return $type === Types::TYPE_STRING; |
|
| 36 | } |
|
| 37 | ||
| 38 | /** |
|
| 39 | * {@inheritdoc} |
|
| 40 | */ |
|
| 41 | public function apply($instance, stdClass $schema, Context $context, Walker $walker) |
|
| 42 | { |
|
| 43 | $length = extension_loaded('mbstring') ? |
|
| 44 | mb_strlen($instance, mb_detect_encoding($instance)) : |
|
| 45 | strlen($instance); |
|
| 46 | ||
| 47 | if ($length < $schema->minLength) { |
|
| 48 | $context->addViolation( |
|
| 49 | 'should be greater than or equal to %s characters', |
|
| 50 | [$schema->minLength] |
|
| 51 | ); |
|
| 52 | } |
|
| 53 | } |
|
| 54 | } |
|
| 55 | ||