Concerns
2 days ago
ArrayRule.php
2 years ago
AuthenticationRule.php
2 years ago
BillingAddressCityRule.php
2 years ago
BillingAddressStateRule.php
2 years ago
BillingAddressZipRule.php
2 years ago
CurrencyRule.php
11 months ago
DonationTypeRule.php
2 years ago
GatewayRule.php
2 years ago
HoneyPotRule.php
1 year ago
Max.php
2 days ago
Min.php
2 days ago
PhoneIntlInputRule.php
2 years ago
Size.php
2 years ago
SubscriptionFrequencyRule.php
2 years ago
SubscriptionInstallmentsRule.php
2 years ago
SubscriptionPeriodRule.php
2 years ago
Max.php
140 lines
| 1 | <?php |
| 2 | namespace Give\DonationForms\Rules; |
| 3 | |
| 4 | |
| 5 | use Closure; |
| 6 | use Give\DonationForms\Rules\Concerns\HasExemptAmounts; |
| 7 | use Give\Vendors\StellarWP\Validation\Config; |
| 8 | use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; |
| 9 | use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 10 | |
| 11 | use function is_numeric; |
| 12 | |
| 13 | /** |
| 14 | * @since 4.16.8 Implement the rule directly instead of extending the vendor rule, whose size is an integer. |
| 15 | * @since 3.0.0 |
| 16 | */ |
| 17 | class Max implements ValidationRule, ValidatesOnFrontEnd |
| 18 | { |
| 19 | use HasExemptAmounts; |
| 20 | |
| 21 | /** |
| 22 | * @var numeric |
| 23 | */ |
| 24 | protected $size; |
| 25 | |
| 26 | /** |
| 27 | * @since 4.16.8 |
| 28 | * |
| 29 | * @param numeric $size |
| 30 | */ |
| 31 | public function __construct($size) |
| 32 | { |
| 33 | if ($size <= 0) { |
| 34 | Config::throwInvalidArgumentException('Max validation rule requires a non-negative value'); |
| 35 | } |
| 36 | |
| 37 | $this->size = $this->sanitize($size); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @inheritDoc |
| 42 | */ |
| 43 | public static function id(): string |
| 44 | { |
| 45 | return 'max'; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @inheritDoc |
| 50 | */ |
| 51 | public static function fromString(?string $options = null): ValidationRule |
| 52 | { |
| 53 | if (!is_numeric($options)) { |
| 54 | Config::throwInvalidArgumentException('Max validation rule requires a numeric value'); |
| 55 | } |
| 56 | |
| 57 | return new self($options); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @since 3.0.0 |
| 62 | */ |
| 63 | public function sanitize($value) |
| 64 | { |
| 65 | if (is_numeric($value)) { |
| 66 | if (strpos((string)$value, '.') !== false) { |
| 67 | return (float)$value; |
| 68 | } |
| 69 | |
| 70 | return (int)$value; |
| 71 | } |
| 72 | |
| 73 | return $value; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @inheritDoc |
| 78 | * |
| 79 | * @since 4.16.8 Skip amounts the admin configured on the form, and report exceeding the maximum instead of |
| 80 | * repeating the minimum wording. |
| 81 | * @since 3.0.0 |
| 82 | **/ |
| 83 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 84 | { |
| 85 | $value = $this->sanitize($value); |
| 86 | |
| 87 | if ($this->isExemptAmount($value)) { |
| 88 | return; |
| 89 | } |
| 90 | |
| 91 | if (is_numeric($value)) { |
| 92 | if ($value > $this->getSize()) { |
| 93 | $fail(sprintf(__('%s must be less than or equal to %s', 'give'), '{field}', $this->getSize())); |
| 94 | } |
| 95 | } elseif (is_string($value)) { |
| 96 | if (mb_strlen($value) > $this->getSize()) { |
| 97 | $fail(sprintf(__('%s must be less than or equal to %d characters', 'give'), '{field}', $this->getSize())); |
| 98 | } |
| 99 | } else { |
| 100 | Config::throwValidationException("Field value must be a number or string"); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * @since 3.0.0 |
| 106 | * |
| 107 | * @return numeric |
| 108 | */ |
| 109 | public function serializeOption() |
| 110 | { |
| 111 | return $this->size; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @since 3.0.0 |
| 116 | * |
| 117 | * @return numeric |
| 118 | */ |
| 119 | public function getSize() |
| 120 | { |
| 121 | return $this->size; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @since 3.0.0 |
| 126 | * |
| 127 | * @param numeric $size |
| 128 | * |
| 129 | * @return void |
| 130 | */ |
| 131 | public function size($size) |
| 132 | { |
| 133 | if ($size <= 0) { |
| 134 | Config::throwInvalidArgumentException('Max validation rule requires a non-negative value'); |
| 135 | } |
| 136 | |
| 137 | $this->size = $this->sanitize($size); |
| 138 | } |
| 139 | } |
| 140 |