| @@ 5-28 (lines=24) @@ | ||
| 2 | ||
| 3 | namespace Albert221\Validation\Rule; |
|
| 4 | ||
| 5 | class MaxLength implements RuleInterface |
|
| 6 | { |
|
| 7 | use RuleTrait; |
|
| 8 | ||
| 9 | protected $maxLength; |
|
| 10 | ||
| 11 | public function __construct($maxLength) |
|
| 12 | { |
|
| 13 | $this->message = 'This field exceeds maximum length.'; |
|
| 14 | ||
| 15 | if (! is_int($maxLength)) { |
|
| 16 | throw new \InvalidArgumentException( |
|
| 17 | sprintf('Maximum length must be type of int, %s given.', gettype($maxLength)) |
|
| 18 | ); |
|
| 19 | } |
|
| 20 | ||
| 21 | $this->maxLength = $maxLength; |
|
| 22 | } |
|
| 23 | ||
| 24 | public function test($value) |
|
| 25 | { |
|
| 26 | return mb_strlen($value) <= $this->maxLength; |
|
| 27 | } |
|
| 28 | } |
|
| 29 | ||
| @@ 5-28 (lines=24) @@ | ||
| 2 | ||
| 3 | namespace Albert221\Validation\Rule; |
|
| 4 | ||
| 5 | class MinLength implements RuleInterface |
|
| 6 | { |
|
| 7 | use RuleTrait; |
|
| 8 | ||
| 9 | protected $minLength; |
|
| 10 | ||
| 11 | public function __construct($minLength) |
|
| 12 | { |
|
| 13 | $this->message = 'This field does not exceed minimum length.'; |
|
| 14 | ||
| 15 | if (! is_int($minLength)) { |
|
| 16 | throw new \InvalidArgumentException( |
|
| 17 | sprintf('Minimum length must be type of int, %s given.', gettype($minLength)) |
|
| 18 | ); |
|
| 19 | } |
|
| 20 | ||
| 21 | $this->minLength = $minLength; |
|
| 22 | } |
|
| 23 | ||
| 24 | public function test($value) |
|
| 25 | { |
|
| 26 | return mb_strlen($value) >= $this->minLength; |
|
| 27 | } |
|
| 28 | } |
|
| 29 | ||