| @@ 5-35 (lines=31) @@ | ||
| 2 | ||
| 3 | namespace Mbright\Validation\Rule\Validate; |
|
| 4 | ||
| 5 | class Max implements ValidateRuleInterface |
|
| 6 | { |
|
| 7 | /** @var int */ |
|
| 8 | protected $max; |
|
| 9 | ||
| 10 | /** |
|
| 11 | * @param int $max The maximum valid value. |
|
| 12 | */ |
|
| 13 | public function __construct(int $max) |
|
| 14 | { |
|
| 15 | $this->max = $max; |
|
| 16 | } |
|
| 17 | ||
| 18 | /** |
|
| 19 | * Validates that the value is less than than or equal to a maximum. |
|
| 20 | * |
|
| 21 | * @param object $subject The subject to be filtered. |
|
| 22 | * @param string $field The subject field name. |
|
| 23 | * |
|
| 24 | * @return bool True if valid, false if not. |
|
| 25 | */ |
|
| 26 | public function __invoke($subject, string $field): bool |
|
| 27 | { |
|
| 28 | $value = $subject->$field; |
|
| 29 | if (!is_scalar($value)) { |
|
| 30 | return false; |
|
| 31 | } |
|
| 32 | ||
| 33 | return $value <= $this->max; |
|
| 34 | } |
|
| 35 | } |
|
| 36 | ||
| @@ 5-35 (lines=31) @@ | ||
| 2 | ||
| 3 | namespace Mbright\Validation\Rule\Validate; |
|
| 4 | ||
| 5 | class Min implements ValidateRuleInterface |
|
| 6 | { |
|
| 7 | /** @var int */ |
|
| 8 | protected $min; |
|
| 9 | ||
| 10 | /** |
|
| 11 | * @param int $min The minimum valid value. |
|
| 12 | */ |
|
| 13 | public function __construct(int $min) |
|
| 14 | { |
|
| 15 | $this->min = $min; |
|
| 16 | } |
|
| 17 | ||
| 18 | /** |
|
| 19 | * Validates that the value is greater than or equal to a minimum. |
|
| 20 | * |
|
| 21 | * @param object $subject The subject to be filtered. |
|
| 22 | * @param string $field The subject field name. |
|
| 23 | * |
|
| 24 | * @return bool True if valid, false if not. |
|
| 25 | */ |
|
| 26 | public function __invoke($subject, string $field): bool |
|
| 27 | { |
|
| 28 | $value = $subject->$field; |
|
| 29 | if (!is_scalar($value)) { |
|
| 30 | return false; |
|
| 31 | } |
|
| 32 | ||
| 33 | return $value >= $this->min; |
|
| 34 | } |
|
| 35 | } |
|
| 36 | ||
| @@ 5-35 (lines=31) @@ | ||
| 2 | ||
| 3 | namespace Mbright\Validation\Rule\Validate; |
|
| 4 | ||
| 5 | class Regex implements ValidateRuleInterface |
|
| 6 | { |
|
| 7 | /** @var string */ |
|
| 8 | protected $expr; |
|
| 9 | ||
| 10 | /** |
|
| 11 | * @param string $expr The regular expression to validate against. |
|
| 12 | */ |
|
| 13 | public function __construct(string $expr) |
|
| 14 | { |
|
| 15 | $this->expr = $expr; |
|
| 16 | } |
|
| 17 | ||
| 18 | /** |
|
| 19 | * Validates the value against a regular expression. |
|
| 20 | * |
|
| 21 | * @param object $subject The subject to be filtered. |
|
| 22 | * @param string $field The subject field name. |
|
| 23 | * |
|
| 24 | * @return bool True if the value matches the expression, false if not. |
|
| 25 | */ |
|
| 26 | public function __invoke($subject, string $field): bool |
|
| 27 | { |
|
| 28 | $value = $subject->$field; |
|
| 29 | if (!is_scalar($value)) { |
|
| 30 | return false; |
|
| 31 | } |
|
| 32 | ||
| 33 | return (bool) preg_match($this->expr, $value); |
|
| 34 | } |
|
| 35 | } |
|
| 36 | ||
| @@ 5-35 (lines=31) @@ | ||
| 2 | ||
| 3 | namespace Mbright\Validation\Rule\Validate; |
|
| 4 | ||
| 5 | class Trim implements ValidateRuleInterface |
|
| 6 | { |
|
| 7 | /** @var string */ |
|
| 8 | protected $chars; |
|
| 9 | ||
| 10 | /** |
|
| 11 | * @param string $chars The characters to strip. |
|
| 12 | */ |
|
| 13 | public function __construct(string $chars = " \t\n\r\0\x0B") |
|
| 14 | { |
|
| 15 | $this->chars = $chars; |
|
| 16 | } |
|
| 17 | ||
| 18 | /** |
|
| 19 | * Validates that a value is already trimmed. |
|
| 20 | * |
|
| 21 | * @param object $subject The subject to be filtered. |
|
| 22 | * @param string $field The subject field name. |
|
| 23 | * |
|
| 24 | * @return bool True if valid, false if not. |
|
| 25 | */ |
|
| 26 | public function __invoke($subject, string $field): bool |
|
| 27 | { |
|
| 28 | $value = $subject->$field; |
|
| 29 | if (!is_scalar($value)) { |
|
| 30 | return false; |
|
| 31 | } |
|
| 32 | ||
| 33 | return trim($value, $this->chars) == $value; |
|
| 34 | } |
|
| 35 | } |
|
| 36 | ||