Abstracts
1 year ago
Boolean.php
1 year ago
Currency.php
1 year ago
DateTime.php
1 year ago
Email.php
1 year ago
Exclude.php
1 year ago
ExcludeIf.php
1 year ago
ExcludeUnless.php
1 year ago
In.php
1 year ago
InStrict.php
1 year ago
Integer.php
1 year ago
Max.php
1 year ago
Min.php
1 year ago
Nullable.php
1 year ago
NullableIf.php
1 year ago
NullableUnless.php
1 year ago
Numeric.php
1 year ago
Optional.php
1 year ago
OptionalIf.php
1 year ago
OptionalUnless.php
1 year ago
Required.php
1 year ago
Size.php
1 year ago
Min.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Give\Vendors\StellarWP\Validation\Rules; |
| 6 | |
| 7 | use Closure; |
| 8 | use Give\Vendors\StellarWP\Validation\Config; |
| 9 | use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; |
| 10 | use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 11 | use Give\Vendors\StellarWP\Validation\Exceptions\ValidationException; |
| 12 | |
| 13 | /** |
| 14 | * @since 1.0.0 |
| 15 | */ |
| 16 | class Min implements ValidationRule, ValidatesOnFrontEnd |
| 17 | { |
| 18 | /** |
| 19 | * @var int |
| 20 | */ |
| 21 | private $size; |
| 22 | |
| 23 | /** |
| 24 | * @since 1.0.0 |
| 25 | */ |
| 26 | public function __construct(int $size) |
| 27 | { |
| 28 | if ($size <= 0) { |
| 29 | Config::throwInvalidArgumentException('Min validation rule requires a non-negative value'); |
| 30 | } |
| 31 | |
| 32 | $this->size = $size; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @inheritDoc |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | public static function id(): string |
| 41 | { |
| 42 | return 'min'; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @inheritDoc |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public static function fromString(?string $options = null): ValidationRule |
| 51 | { |
| 52 | if (!is_numeric($options)) { |
| 53 | Config::throwInvalidArgumentException('Min validation rule requires a numeric value'); |
| 54 | } |
| 55 | |
| 56 | return new self((int)$options); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @inheritDoc |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | * |
| 64 | * @throws ValidationException |
| 65 | */ |
| 66 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 67 | { |
| 68 | if (is_int($value) || is_float($value)) { |
| 69 | if ($value < $this->size) { |
| 70 | $fail(sprintf(__('%s must be greater than or equal to %d', 'give'), '{field}', $this->size)); |
| 71 | } |
| 72 | } elseif (is_string($value)) { |
| 73 | if (mb_strlen($value) < $this->size) { |
| 74 | $fail(sprintf(__('%s must be more than or equal to %d characters', 'give'), '{field}', $this->size)); |
| 75 | } |
| 76 | } else { |
| 77 | Config::throwValidationException("Field value must be a number or string"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @inheritDoc |
| 83 | * |
| 84 | * @since 1.0.0 |
| 85 | */ |
| 86 | public function serializeOption(): int |
| 87 | { |
| 88 | return $this->size; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * @since 1.0.0 |
| 93 | */ |
| 94 | public function getSize(): int |
| 95 | { |
| 96 | return $this->size; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @since 1.0.0 |
| 101 | * |
| 102 | * @return void |
| 103 | */ |
| 104 | public function size(int $size) |
| 105 | { |
| 106 | if ($size <= 0) { |
| 107 | Config::throwInvalidArgumentException('Min validation rule requires a non-negative value'); |
| 108 | } |
| 109 | |
| 110 | $this->size = $size; |
| 111 | } |
| 112 | } |
| 113 |