Currency.php
5.69 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Class Currency
*
* @package WooCommerce\Payments
*/
namespace WCPay\MultiCurrency;
use WC_Payments_Localization_Service;
use WC_Payments_Utils;
defined( 'ABSPATH' ) || exit;
/**
* Multi-Currency Currency object.
*/
class Currency implements \JsonSerializable {
/**
* Three letter currency code.
*
* @var string
*/
public $code;
/**
* Currency conversion rate.
*
* @var float
*/
public $rate;
/**
* Currency charm rate after conversion and rounding.
*
* @var float|null
*/
private $charm;
/**
* Is currency default for store?
*
* @var bool
*/
private $is_default = false;
/**
* Currency rounding rate after conversion.
*
* @var string|null
*/
private $rounding;
/**
* Is currency zero decimal?
*
* @var bool
*/
private $is_zero_decimal = false;
/**
* A timestamp representing the time this currency was last fetched successfully from the server.
*
* @var int|null
*/
private $last_updated;
/**
* Constructor.
*
* @param string $code Three letter currency code.
* @param float $rate The conversion rate.
* @param int|null $last_updated The time this currency was last updated.
*/
public function __construct( string $code = '', float $rate = 1.0, $last_updated = null ) {
$this->code = $code;
$this->rate = $rate;
if ( get_woocommerce_currency() === $code ) {
$this->is_default = true;
}
if ( in_array( strtolower( $code ), WC_Payments_Utils::zero_decimal_currencies(), true ) ) {
$this->is_zero_decimal = true;
}
if ( ! is_null( $last_updated ) ) {
$this->last_updated = $last_updated;
}
}
/**
* Retrieves the currency's translated name from WooCommerce core.
*
* @param string $code The currency code.
*/
public function get_currency_name_from_code( $code ): string {
$wc_currencies = get_woocommerce_currencies();
return $wc_currencies[ $code ];
}
/**
* Retrieves the currency's code.
*
* @return string Three letter currency code.
*/
public function get_code(): string {
return $this->code;
}
/**
* Retrieves the currency's charm rate.
*
* @return float Charm rate.
*/
public function get_charm(): float {
return is_null( $this->charm ) ? 0.00 : $this->charm;
}
/**
* Retrieves the currency's flag.
*
* @return string Currency flag.
*/
public function get_flag(): string {
// Maybe add param img/emoji to return which you want?
return CountryFlags::get_by_currency( $this->code );
}
/**
* Retrieves the currency code lowercased.
*
* @return string Currency code lowercased.
*/
public function get_id(): string {
return strtolower( $this->code );
}
/**
* Retrieves if the currency is default for the store.
*
* @return bool
*/
public function get_is_default(): bool {
return $this->is_default;
}
/**
* Retrieves the currency's name from WooCommerce core.
*
* @return string Currency name.
*/
public function get_name(): string {
$wc_currencies = get_woocommerce_currencies();
return $wc_currencies[ $this->code ];
}
/**
* Retrieves the currency's conversion rate.
*
* @return float The conversion rate.
*/
public function get_rate(): float {
return $this->rate;
}
/**
* Retrieves the currency's rounding rate.
*
* @return string Rounding rate.
*/
public function get_rounding(): string {
return (string) ( $this->rounding ?? '0' );
}
/**
* Retrieves the currency's symbol from WooCommerce core.
*
* @return string Currency symbol.
*/
public function get_symbol(): string {
return get_woocommerce_currency_symbol( $this->code );
}
/**
* Retrieves the currency's symbol position from Localization Service
*
* @return string Currency position (left/right).
*/
public function get_symbol_position() : string {
$localization_service = new WC_Payments_Localization_Service();
return $localization_service->get_currency_format( $this->code )['currency_pos'];
}
/**
* Retrieves if the currency is zero decimal.
*
* @return bool
*/
public function get_is_zero_decimal(): bool {
return $this->is_zero_decimal;
}
/**
* Get the timestamp reprenting when the currency was last updated.
*
* @return int|null A timestamp representing when the currency was last updated.
*/
public function get_last_updated() {
return $this->last_updated;
}
/**
* Sets the currency's charm rate.
*
* @param float $charm Charm rate.
*/
public function set_charm( $charm ) {
$this->charm = $charm;
}
/**
* Sets the currency's conversion rate.
*
* @param float $rate Conversion rate.
*/
public function set_rate( $rate ) {
$this->rate = $rate;
}
/**
* Sets the currency's rounding rate.
*
* @param string $rounding Rounding rate.
* @return void
*/
public function set_rounding( $rounding ) {
$this->rounding = $rounding;
}
/**
* Set the currency's last updated timestamp.
*
* @param int $last_updated A timestamp representing when the currency was last updated.
*
* @return void
*/
public function set_last_updated( int $last_updated ) {
$this->last_updated = $last_updated;
}
/**
* Specify the data that should be serialized to JSON.
*
* @return array Serialized Currency object.
*/
public function jsonSerialize(): array {
return [
'code' => $this->code,
'rate' => $this->get_rate(),
'name' => html_entity_decode( $this->get_name() ),
'id' => $this->get_id(),
'is_default' => $this->get_is_default(),
'flag' => $this->get_flag(),
'symbol' => html_entity_decode( $this->get_symbol() ),
'symbol_position' => $this->get_symbol_position(),
'is_zero_decimal' => $this->get_is_zero_decimal(),
'last_updated' => $this->get_last_updated(),
];
}
}