PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / DonationForms / Rules / CurrencyRule.php

CurrencyRule.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/DonationForms/Rules/CurrencyRule.php

77 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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