CurrencyFormatter.php
2 years ago
DefaultFormatter.php
2 years ago
FormatterInterface.php
2 years ago
HtmlFormatter.php
2 years ago
MoneyFormatter.php
1 year ago
CurrencyFormatter.php
52 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Formatters; |
| 3 | |
| 4 | /** |
| 5 | * Currency Formatter. |
| 6 | * |
| 7 | * Formats an array of monetary values by inserting currency data. |
| 8 | */ |
| 9 | class CurrencyFormatter implements FormatterInterface { |
| 10 | /** |
| 11 | * Format a given value and return the result. |
| 12 | * |
| 13 | * @param array $value Value to format. |
| 14 | * @param array $options Options that influence the formatting. |
| 15 | * @return array |
| 16 | */ |
| 17 | public function format( $value, array $options = [] ) { |
| 18 | $position = get_option( 'woocommerce_currency_pos' ); |
| 19 | $symbol = html_entity_decode( get_woocommerce_currency_symbol() ); |
| 20 | $prefix = ''; |
| 21 | $suffix = ''; |
| 22 | |
| 23 | switch ( $position ) { |
| 24 | case 'left_space': |
| 25 | $prefix = $symbol . ' '; |
| 26 | break; |
| 27 | case 'left': |
| 28 | $prefix = $symbol; |
| 29 | break; |
| 30 | case 'right_space': |
| 31 | $suffix = ' ' . $symbol; |
| 32 | break; |
| 33 | case 'right': |
| 34 | $suffix = $symbol; |
| 35 | break; |
| 36 | } |
| 37 | |
| 38 | return array_merge( |
| 39 | (array) $value, |
| 40 | [ |
| 41 | 'currency_code' => get_woocommerce_currency(), |
| 42 | 'currency_symbol' => $symbol, |
| 43 | 'currency_minor_unit' => wc_get_price_decimals(), |
| 44 | 'currency_decimal_separator' => wc_get_price_decimal_separator(), |
| 45 | 'currency_thousand_separator' => wc_get_price_thousand_separator(), |
| 46 | 'currency_prefix' => $prefix, |
| 47 | 'currency_suffix' => $suffix, |
| 48 | ] |
| 49 | ); |
| 50 | } |
| 51 | } |
| 52 |