Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
PayPal_Payments_Currencies
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 1
 format_price
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
1<?php
2/**
3 * PayPal_Payments_Currencies: Utils for displaying and managing currencies.
4 *
5 * @package    automattic/jetpack-paypal-payments
6 */
7
8/**
9 * General currencies specific functionality
10 */
11class PayPal_Payments_Currencies {
12    /**
13     * Currencies definition
14     */
15    const CURRENCIES = array(
16        'USD' => array(
17            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
18            'symbol'  => '$',
19            'decimal' => 2,
20        ),
21        'GBP' => array(
22            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
23            'symbol'  => '&#163;',
24            'decimal' => 2,
25        ),
26        'JPY' => array(
27            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
28            'symbol'  => '&#165;',
29            'decimal' => 0,
30        ),
31        'BRL' => array(
32            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
33            'symbol'  => 'R$',
34            'decimal' => 2,
35        ),
36        'EUR' => array(
37            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
38            'symbol'  => '&#8364;',
39            'decimal' => 2,
40        ),
41        'NZD' => array(
42            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
43            'symbol'  => 'NZ$',
44            'decimal' => 2,
45        ),
46        'AUD' => array(
47            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
48            'symbol'  => 'A$',
49            'decimal' => 2,
50        ),
51        'CAD' => array(
52            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
53            'symbol'  => 'C$',
54            'decimal' => 2,
55        ),
56        'ILS' => array(
57            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
58            'symbol'  => '₪',
59            'decimal' => 2,
60        ),
61        'RUB' => array(
62            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
63            'symbol'  => '₽',
64            'decimal' => 2,
65        ),
66        'MXN' => array(
67            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
68            'symbol'  => 'MX$',
69            'decimal' => 2,
70        ),
71        'MYR' => array(
72            'format'  => '%2$s%1$s', // 1: Symbol 2: currency value
73            'symbol'  => 'RM',
74            'decimal' => 2,
75        ),
76        'SEK' => array(
77            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
78            'symbol'  => 'Skr',
79            'decimal' => 2,
80        ),
81        'HUF' => array(
82            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
83            'symbol'  => 'Ft',
84            'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
85        ),
86        'CHF' => array(
87            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
88            'symbol'  => 'CHF',
89            'decimal' => 2,
90        ),
91        'CZK' => array(
92            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
93            'symbol'  => 'Kč',
94            'decimal' => 2,
95        ),
96        'DKK' => array(
97            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
98            'symbol'  => 'Dkr',
99            'decimal' => 2,
100        ),
101        'HKD' => array(
102            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
103            'symbol'  => 'HK$',
104            'decimal' => 2,
105        ),
106        'NOK' => array(
107            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
108            'symbol'  => 'Kr',
109            'decimal' => 2,
110        ),
111        'PHP' => array(
112            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
113            'symbol'  => '₱',
114            'decimal' => 2,
115        ),
116        'PLN' => array(
117            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
118            'symbol'  => 'PLN',
119            'decimal' => 2,
120        ),
121        'SGD' => array(
122            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
123            'symbol'  => 'S$',
124            'decimal' => 2,
125        ),
126        'TWD' => array(
127            'format'  => '%1$s%2$s', // 1: Symbol 2: currency value
128            'symbol'  => 'NT$',
129            'decimal' => 0, // Decimals are supported by Stripe but not by PayPal.
130        ),
131        'THB' => array(
132            'format'  => '%2$s%1$s', // 1: Symbol 2: currency value
133            'symbol'  => '฿',
134            'decimal' => 2,
135        ),
136        'INR' => array(
137            'format'  => '%2$s %1$s', // 1: Symbol 2: currency value
138            'symbol'  => '₹',
139            'decimal' => 0,
140        ),
141    );
142
143    /**
144     * Format a price with currency.
145     *
146     * Uses currency-aware formatting to output a formatted price with a simple fallback.
147     *
148     * Largely inspired by WordPress.com's Store_Price::display_currency
149     *
150     * @param  string $price    Price.
151     * @param  string $currency Currency.
152     * @param  bool   $symbol   Whether to display the currency symbol.
153     * @return string           Formatted price.
154     */
155    public static function format_price( $price, $currency, $symbol = true ) {
156        $price = floatval( $price );
157
158        // Try to parse using NumberFormatter if available
159        if ( class_exists( 'NumberFormatter' ) ) {
160            $formatter = new NumberFormatter( get_locale(), NumberFormatter::DECIMAL );
161            $parsed    = $formatter->parse( (string) $price );
162            if ( false !== $parsed ) {
163                $price = (float) $parsed;
164            }
165        }
166
167        // Fall back to unspecified currency symbol like `¤1,234.05`.
168        // @link https://en.wikipedia.org/wiki/Currency_sign_(typography).
169        if ( ! array_key_exists( $currency, self::CURRENCIES ) ) {
170            return ( $symbol ? '¤' : '' ) . number_format_i18n( $price, 2 );
171        }
172
173        $currency_details = self::CURRENCIES[ $currency ];
174
175        // Ensure USD displays as 1234.56 even in non-US locales.
176        $amount = 'USD' === $currency
177            ? number_format( $price, $currency_details['decimal'], '.', ',' )
178            : number_format_i18n( $price, $currency_details['decimal'] );
179
180        return sprintf(
181            $currency_details['format'],
182            $symbol ? $currency_details['symbol'] : '',
183            $amount
184        );
185    }
186}