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
DonationTypeRule.php
2 years ago
GatewayRule.php
2 years ago
HoneyPotRule.php
1 year ago
Max.php
2 years ago
Min.php
2 years 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
49 lines
| 1 | <?php |
| 2 | namespace Give\DonationForms\Rules; |
| 3 | |
| 4 | |
| 5 | use Closure; |
| 6 | use Give\Vendors\StellarWP\Validation\Config; |
| 7 | |
| 8 | use function is_numeric; |
| 9 | |
| 10 | class Max extends \Give\Vendors\StellarWP\Validation\Rules\Max |
| 11 | { |
| 12 | /** |
| 13 | * @since 3.0.0 |
| 14 | */ |
| 15 | public function sanitize($value) |
| 16 | { |
| 17 | if (is_numeric($value)) { |
| 18 | if (strpos($value, '.') !== false) { |
| 19 | return (float)$value; |
| 20 | } |
| 21 | |
| 22 | return (int)$value; |
| 23 | } |
| 24 | |
| 25 | return $value; |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @inheritDoc |
| 30 | * |
| 31 | * @since 3.0.0 |
| 32 | **/ |
| 33 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 34 | { |
| 35 | $value = $this->sanitize($value); |
| 36 | |
| 37 | if (is_numeric($value)) { |
| 38 | if ($value > $this->getSize()) { |
| 39 | $fail(sprintf(__('%s must be greater than or equal to %s', 'give'), '{field}', $this->getSize())); |
| 40 | } |
| 41 | } elseif (is_string($value)) { |
| 42 | if (mb_strlen($value) > $this->getSize()) { |
| 43 | $fail(sprintf(__('%s must be more than or equal to %d characters', 'give'), '{field}', $this->getSize())); |
| 44 | } |
| 45 | } else { |
| 46 | Config::throwValidationException("Field value must be a number or string"); |
| 47 | } |
| 48 | } |
| 49 | } |