@@ -13,6 +13,9 @@ class FormattedNumber
13
13
14
14
private const STRING_REGEXP_PERCENT = '~^(?:(?: *(?<PrefixedSign>[-+])? *\% *(?<PrefixedSign2>[-+])? *(?<PrefixedValue>[0-9]+\.?[0-9*]*(?:E[-+]?[0-9]*)?) *)|(?: *(?<PostfixedSign>[-+])? *(?<PostfixedValue>[0-9]+\.?[0-9]*(?:E[-+]?[0-9]*)?) *\% *))$~i ' ;
15
15
16
+ // preg_quoted string for major currency symbols, with a %s for locale currency
17
+ private const CURRENCY_CONVERSION_LIST = '\$€£¥%s ' ;
18
+
16
19
private const STRING_CONVERSION_LIST = [
17
20
[self ::class, 'convertToNumberIfNumeric ' ],
18
21
[self ::class, 'convertToNumberIfFraction ' ],
@@ -106,13 +109,11 @@ public static function convertToNumberIfPercent(string &$operand): bool
106
109
*/
107
110
public static function convertToNumberIfCurrency (string &$ operand ): bool
108
111
{
109
- $ quotedCurrencyCode = preg_quote (StringHelper::getCurrencyCode ());
110
-
112
+ $ currencyRegexp = self ::currencyMatcherRegexp ();
111
113
$ value = preg_replace ('/(\d),(\d)/u ' , '$1$2 ' , $ operand );
112
- $ regExp = '~^(?:(?: *(?<PrefixedSign>[-+])? * ' . $ quotedCurrencyCode . ' *(?<PrefixedSign2>[-+])? *(?<PrefixedValue>[0-9]+\.?[0-9*]*(?:E[-+]?[0-9]*)?) *)|(?: *(?<PostfixedSign>[-+])? *(?<PostfixedValue>[0-9]+\.?[0-9]*(?:E[-+]?[0-9]*)?) * ' . $ quotedCurrencyCode . ' *))$~ui ' ;
113
114
114
115
$ match = [];
115
- if ($ value !== null && preg_match ($ regExp , $ value , $ match , PREG_UNMATCHED_AS_NULL )) {
116
+ if ($ value !== null && preg_match ($ currencyRegexp , $ value , $ match , PREG_UNMATCHED_AS_NULL )) {
116
117
//Determine the sign
117
118
$ sign = ($ match ['PrefixedSign ' ] ?? $ match ['PrefixedSign2 ' ] ?? $ match ['PostfixedSign ' ]) ?? '' ;
118
119
//Cast to a float
@@ -123,4 +124,11 @@ public static function convertToNumberIfCurrency(string &$operand): bool
123
124
124
125
return false ;
125
126
}
127
+
128
+ public static function currencyMatcherRegexp (): string
129
+ {
130
+ $ quotedCurrencyCode = sprintf (self ::CURRENCY_CONVERSION_LIST , preg_quote (StringHelper::getCurrencyCode ()));
131
+
132
+ return '~^(?:(?: *(?<PrefixedSign>[-+])? *(?<PrefixedCurrency>[ ' . $ quotedCurrencyCode . ']) *(?<PrefixedSign2>[-+])? *(?<PrefixedValue>[0-9]+\.?[0-9*]*(?:E[-+]?[0-9]*)?) *)|(?: *(?<PostfixedSign>[-+])? *(?<PostfixedValue>[0-9]+\.?[0-9]*(?:E[-+]?[0-9]*)?) *(?<PostCurrency>[ ' . $ quotedCurrencyCode . ']) *))$~ui ' ;
133
+ }
126
134
}
0 commit comments