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
9 months 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
CurrencyRule.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\DonationForms\Rules; |
| 4 | |
| 5 | use Closure; |
| 6 | use Give\Framework\Support\Currencies\GiveCurrencies; |
| 7 | use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; |
| 8 | use Money\Currency; |
| 9 | |
| 10 | /** |
| 11 | * Custom currency validation rule that uses GiveWP's currency list and filter. |
| 12 | * Replaces the generic stellar validation library currency rule to ensure consistency |
| 13 | * with GiveWP's configured currencies and filters. |
| 14 | * |
| 15 | * @since 4.10.0 |
| 16 | */ |
| 17 | class CurrencyRule implements ValidationRule |
| 18 | { |
| 19 | /** |
| 20 | * @since 4.10.0 |
| 21 | */ |
| 22 | public static function id(): string |
| 23 | { |
| 24 | return 'giveCurrency'; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @since 4.10.0 |
| 29 | */ |
| 30 | public static function fromString(string $options = null): ValidationRule |
| 31 | { |
| 32 | return new self(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Validates that the currency code is in GiveWP's supported currency list. |
| 37 | * Uses give_get_currencies_list() to get the current supported currencies |
| 38 | * and provides clear error messages with valid currency options. |
| 39 | * |
| 40 | * @since 4.10.0 |
| 41 | */ |
| 42 | public function __invoke($value, Closure $fail, string $key, array $values) |
| 43 | { |
| 44 | // Check format first for better error messaging |
| 45 | if (!$this->isValidFormat($value)) { |
| 46 | $fail( |
| 47 | sprintf( |
| 48 | __('%s must be a valid 3-letter currency code in uppercase format (example: USD)', 'give'), |
| 49 | '{field}' |
| 50 | ) |
| 51 | ); |
| 52 | } elseif (!give(GiveCurrencies::class)->contains(new Currency($value))) { |
| 53 | $fail( |
| 54 | sprintf( |
| 55 | __('%s must be a valid currency. Provided: %s', 'give'), |
| 56 | '{field}', |
| 57 | $value |
| 58 | ) |
| 59 | ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Checks if a currency code is in the correct ISO 4217 format. |
| 65 | * Valid format: exactly 3 uppercase alphabetic characters (not empty). |
| 66 | * |
| 67 | * @since 4.10.0 |
| 68 | * |
| 69 | * @param mixed $value The currency code to validate |
| 70 | * @return bool True if the format is valid, false otherwise |
| 71 | */ |
| 72 | private function isValidFormat($value): bool |
| 73 | { |
| 74 | return is_string($value) && !empty($value) && strlen($value) === 3 && ctype_alpha($value) && $value === strtoupper($value); |
| 75 | } |
| 76 | } |
| 77 |