CurrencyFormatter.php
2 years ago
DefaultFormatter.php
2 years ago
FormatterInterface.php
2 years ago
HtmlFormatter.php
2 years ago
MoneyFormatter.php
2 years ago
MoneyFormatter.php
35 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Formatters; |
| 3 | |
| 4 | /** |
| 5 | * Money Formatter. |
| 6 | * |
| 7 | * Formats monetary values using store settings. |
| 8 | */ |
| 9 | class MoneyFormatter implements FormatterInterface { |
| 10 | /** |
| 11 | * Format a given value and return the result. |
| 12 | * |
| 13 | * @param mixed $value Value to format. |
| 14 | * @param array $options Options that influence the formatting. |
| 15 | * @return mixed |
| 16 | */ |
| 17 | public function format( $value, array $options = [] ) { |
| 18 | $options = wp_parse_args( |
| 19 | $options, |
| 20 | [ |
| 21 | 'decimals' => wc_get_price_decimals(), |
| 22 | 'rounding_mode' => PHP_ROUND_HALF_UP, |
| 23 | ] |
| 24 | ); |
| 25 | |
| 26 | return (string) intval( |
| 27 | round( |
| 28 | ( (float) wc_format_decimal( $value ) ) * ( 10 ** absint( $options['decimals'] ) ), |
| 29 | 0, |
| 30 | absint( $options['rounding_mode'] ) |
| 31 | ) |
| 32 | ); |
| 33 | } |
| 34 | } |
| 35 |