| @@ 7-37 (lines=31) @@ | ||
| 4 | ||
| 5 | use Mbright\Validation\Rule\AbstractStringCase; |
|
| 6 | ||
| 7 | class Strlen extends AbstractStringCase implements ValidateRuleInterface |
|
| 8 | { |
|
| 9 | /** @var int */ |
|
| 10 | protected $len; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * @param int $len The minimum valid length. |
|
| 14 | */ |
|
| 15 | public function __construct(int $len) |
|
| 16 | { |
|
| 17 | $this->len = $len; |
|
| 18 | } |
|
| 19 | ||
| 20 | /** |
|
| 21 | * Validates that the length of the value is within a given range. |
|
| 22 | * |
|
| 23 | * @param object $subject The subject to be filtered. |
|
| 24 | * @param string $field The subject field name. |
|
| 25 | * |
|
| 26 | * @return bool True if valid, false if not. |
|
| 27 | */ |
|
| 28 | public function __invoke($subject, string $field): bool |
|
| 29 | { |
|
| 30 | $value = $subject->$field; |
|
| 31 | if (!is_scalar($value)) { |
|
| 32 | return false; |
|
| 33 | } |
|
| 34 | ||
| 35 | return $this->strlen($value) == $this->len; |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||
| @@ 7-37 (lines=31) @@ | ||
| 4 | ||
| 5 | use Mbright\Validation\Rule\AbstractStringCase; |
|
| 6 | ||
| 7 | class StrlenMax extends AbstractStringCase implements ValidateRuleInterface |
|
| 8 | { |
|
| 9 | /** @var int */ |
|
| 10 | protected $max; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * @param int $max The value must have no more than this many characters. |
|
| 14 | */ |
|
| 15 | public function __construct(int $max) |
|
| 16 | { |
|
| 17 | $this->max = $max; |
|
| 18 | } |
|
| 19 | ||
| 20 | /** |
|
| 21 | * Validates that a value is no longer than a certain length. |
|
| 22 | * |
|
| 23 | * @param object $subject The subject to be filtered. |
|
| 24 | * @param string $field The subject field name. |
|
| 25 | * |
|
| 26 | * @return bool True if valid, false if not. |
|
| 27 | */ |
|
| 28 | public function __invoke($subject, string $field): bool |
|
| 29 | { |
|
| 30 | $value = $subject->$field; |
|
| 31 | if (!is_scalar($value)) { |
|
| 32 | return false; |
|
| 33 | } |
|
| 34 | ||
| 35 | return $this->strlen($value) <= $this->max; |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||
| @@ 7-37 (lines=31) @@ | ||
| 4 | ||
| 5 | use Mbright\Validation\Rule\AbstractStringCase; |
|
| 6 | ||
| 7 | class StrlenMin extends AbstractStringCase implements ValidateRuleInterface |
|
| 8 | { |
|
| 9 | /** @var int */ |
|
| 10 | protected $min; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * @param int $min The value must have at least this many characters. |
|
| 14 | */ |
|
| 15 | public function __construct(int $min) |
|
| 16 | { |
|
| 17 | $this->min = $min; |
|
| 18 | } |
|
| 19 | ||
| 20 | /** |
|
| 21 | * Validates that a value is no longer than a certain length. |
|
| 22 | * |
|
| 23 | * @param object $subject The subject to be filtered. |
|
| 24 | * @param string $field The subject field name. |
|
| 25 | * |
|
| 26 | * @return bool True if valid, false if not. |
|
| 27 | */ |
|
| 28 | public function __invoke($subject, string $field): bool |
|
| 29 | { |
|
| 30 | $value = $subject->$field; |
|
| 31 | if (!is_scalar($value)) { |
|
| 32 | return false; |
|
| 33 | } |
|
| 34 | ||
| 35 | return $this->strlen($value) >= $this->min; |
|
| 36 | } |
|
| 37 | } |
|
| 38 | ||