| @@ -2,21 +2,69 @@ | ||
| 2 | 2 | namespace Give\DonationForms\Rules; |
| 3 | 3 | |
| 4 | 4 | |
| 5 | 5 | use Closure; |
| 6 | +use Give\DonationForms\Rules\Concerns\HasExemptAmounts; | |
| 6 | 7 | use Give\Vendors\StellarWP\Validation\Config; |
| 8 | +use Give\Vendors\StellarWP\Validation\Contracts\ValidatesOnFrontEnd; | |
| 9 | +use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; | |
| 7 | 10 | |
| 8 | 11 | use function is_numeric; |
| 9 | 12 | |
| 10 | -class Max extends \Give\Vendors\StellarWP\Validation\Rules\Max | |
| 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 | |
| 11 | 18 | { |
| 19 | + use HasExemptAmounts; | |
| 20 | + | |
| 12 | 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 | + /** | |
| 13 | 61 | * @since 3.0.0 |
| 14 | 62 | */ |
| 15 | 63 | public function sanitize($value) |
| 16 | 64 | { |
| 17 | 65 | if (is_numeric($value)) { |
| 18 | - if (strpos($value, '.') !== false) { | |
| 66 | + if (strpos((string)$value, '.') !== false) { | |
| 19 | 67 | return (float)$value; |
| 20 | 68 | } |
| 21 | 69 | |
| 22 | 70 | return (int)$value; |
| @@ -27,8 +75,10 @@ | ||
| 27 | 75 | |
| 28 | 76 | /** |
| 29 | 77 | * @inheritDoc |
| 30 | 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. | |
| 31 | 81 | * @since 3.0.0 |
| 32 | 82 | **/ |
| 33 | 83 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 34 | 84 | { |
| @@ -33,17 +83,57 @@ | ||
| 33 | 83 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 34 | 84 | { |
| 35 | 85 | $value = $this->sanitize($value); |
| 36 | 86 | |
| 87 | + if ($this->isExemptAmount($value)) { | |
| 88 | + return; | |
| 89 | + } | |
| 90 | + | |
| 37 | 91 | if (is_numeric($value)) { |
| 38 | 92 | if ($value > $this->getSize()) { |
| 39 | - $fail(sprintf(__('%s must be greater than or equal to %s', 'give'), '{field}', $this->getSize())); | |
| 93 | + $fail(sprintf(__('%s must be less than or equal to %s', 'give'), '{field}', $this->getSize())); | |
| 40 | 94 | } |
| 41 | 95 | } elseif (is_string($value)) { |
| 42 | 96 | if (mb_strlen($value) > $this->getSize()) { |
| 43 | - $fail(sprintf(__('%s must be more than or equal to %d characters', 'give'), '{field}', $this->getSize())); | |
| 97 | + $fail(sprintf(__('%s must be less than or equal to %d characters', 'give'), '{field}', $this->getSize())); | |
| 44 | 98 | } |
| 45 | 99 | } else { |
| 46 | 100 | Config::throwValidationException("Field value must be a number or string"); |
| 47 | 101 | } |
| 48 | 102 | } |
| 49 | -} | |
| 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 | +} | |