/var/www/html_us/wp-content/plugins/woocommerce/src/StoreApi/Formatters/MoneyFormatter.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
namespace Automattic\WooCommerce\StoreApi\Formatters;

/**
 * Money Formatter.
 *
 * Formats monetary values using store settings.
 */
class MoneyFormatter implements FormatterInterface {
    
/**
     * Format a given price value and return the result as a string without decimals.
     *
     * @param int|float|string $value Value to format. Int is allowed, as it may also represent a valid price.
     * @param array            $options Options that influence the formatting.
     * @return string
     */
    
public function format$value, array $options = [] ) {

        if ( ! 
is_int$value ) && ! is_string$value ) && ! is_float$value ) ) {
            
wc_doing_it_wrong(
                
__FUNCTION__,
                
'Function expects a $value arg of type INT, STRING or FLOAT.',
                
'9.2'
            
);

            return 
'';
        }

        
$options wp_parse_args(
            
$options,
            [
                
'decimals'      => wc_get_price_decimals(),
                
'rounding_mode' => PHP_ROUND_HALF_UP,
            ]
        );

        
// Ensure rounding mode is valid.
        
$rounding_modes           = [ PHP_ROUND_HALF_UPPHP_ROUND_HALF_DOWNPHP_ROUND_HALF_EVENPHP_ROUND_HALF_ODD ];
        
$options['rounding_mode'] = absint$options['rounding_mode'] );
        if ( ! 
in_array$options['rounding_mode'], $rounding_modestrue ) ) {
            
$options['rounding_mode'] = PHP_ROUND_HALF_UP;
        }

        
$value floatval$value );

        
// Remove the price decimal points for rounding purposes.
        
$value $value pow10absint$options['decimals'] ) );
        
$value round$value0$options['rounding_mode'] );

        
// This ensures returning the value as a string without decimal points ready for price parsing.
        
return wc_format_decimal$value0true );
    }
}