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