← Back
Editing: CurrencyRule.php
<?php namespace Give\DonationForms\Rules; use Closure; use Give\Framework\Support\Currencies\GiveCurrencies; use Give\Vendors\StellarWP\Validation\Contracts\ValidationRule; use Money\Currency; /** * Custom currency validation rule that uses GiveWP's currency list and filter. * Replaces the generic stellar validation library currency rule to ensure consistency * with GiveWP's configured currencies and filters. * * @since 4.19.0.1 Require the currency to be one of the form's supported currencies when they are provided. * @since 4.10.0 */ class CurrencyRule implements ValidationRule { /** * @var string[] */ protected $allowedCurrencies; /** * @since 4.19.0.1 * * @param string[] $allowedCurrencies Currency codes the form accepts. An empty array applies no form-level restriction. */ public function __construct(array $allowedCurrencies = []) { $this->allowedCurrencies = $allowedCurrencies; } /** * @since 4.10.0 */ public static function id(): string { return 'giveCurrency'; } /** * @since 4.17.0 Declare the nullable parameter explicitly. * @since 4.10.0 */ public static function fromString(?string $options = null): ValidationRule { return new self(); } /** * Validates that the currency code is in GiveWP's supported currency list * and, when supported currencies are set, is one of those currencies. * Uses give_get_currencies_list() to get the current supported currencies * and provides clear error messages with valid currency options. * * @since 4.19.1 Number the placeholders and add translators comments. * @since 4.19.0.1 Require the currency to be one of the form's supported currencies when they are provided. * @since 4.10.0 */ public function __invoke($value, Closure $fail, string $key, array $values) { // Check format first for better error messaging if (!$this->isValidFormat($value)) { $fail( sprintf( /* translators: %s: Field label */ __('%s must be a valid 3-letter currency code in uppercase format (example: USD)', 'give'), '{field}' ) ); } elseif (!give(GiveCurrencies::class)->contains(new Currency($value))) { $fail( sprintf( /* translators: 1: Field label, 2: Submitted value */ __('%1$s must be a valid currency. Provided: %2$s', 'give'), '{field}', $value ) ); } elseif (!empty($this->allowedCurrencies) && !in_array($value, $this->allowedCurrencies, true)) { $fail( sprintf( /* translators: %s: field name */ __('%s must be a currency supported by this donation form', 'give'), '{field}' ) ); } } /** * Checks if a currency code is in the correct ISO 4217 format. * Valid format: exactly 3 uppercase alphabetic characters (not empty). * * @since 4.10.0 * * @param mixed $value The currency code to validate * @return bool True if the format is valid, false otherwise */ private function isValidFormat($value): bool { return is_string($value) && !empty($value) && strlen($value) === 3 && ctype_alpha($value) && $value === strtoupper($value); } }
Save File
Cancel