| 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 |
} |