Completed
Push — add/changelog-910 ( c277c8...7fd9c0 )
by Jeremy
19:06 queued 09:10
created

Jetpack_Currencies   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 165
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 0

1 Method

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