1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\currency; |
4
|
|
|
|
5
|
|
|
use EE_Error; |
6
|
|
|
use EEH_File; |
7
|
|
|
use EventEspresso\core\exceptions\InvalidIdentifierException; |
8
|
|
|
|
9
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit('No direct script access allowed'); |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class CurrencySubunits |
15
|
|
|
* Class for retrieving a currency's decimal fractions, |
16
|
|
|
* or difference in order of magnitude between a currency's |
17
|
|
|
* super unit and subunit. For example, for USD, 1 penny is 1/100th of a dollar. |
18
|
|
|
* So the order of magnitude, or decimal fractions is 2 |
19
|
|
|
* |
20
|
|
|
* @package Event Espresso |
21
|
|
|
* @author Mike Nelson |
22
|
|
|
* @since 4.9.52.p |
23
|
|
|
*/ |
24
|
|
|
class CurrencySubunits |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* array keys are currency codes, values are a sub-array with the following values: |
29
|
|
|
* "decimals" : the decimal fraction, or order of magnitude difference, |
30
|
|
|
* between that currency's main units and subunits. |
31
|
|
|
* E.g. for USD the decimal fraction is 2. |
32
|
|
|
* |
33
|
|
|
* @var array $currency_subunits |
34
|
|
|
*/ |
35
|
|
|
private $currency_subunits; |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns all the currency subunit data |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
* @throws EE_Error |
44
|
|
|
*/ |
45
|
|
|
public function getAll() |
46
|
|
|
{ |
47
|
|
|
$this->ensureInitialized(); |
48
|
|
|
return $this->currency_subunits; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the decimal fraction, or order of magnitude difference between the currency's main units and subunits. |
55
|
|
|
* |
56
|
|
|
* @param $currency_code |
57
|
|
|
* @return mixed |
58
|
|
|
* @throws EE_Error |
59
|
|
|
* @throws InvalidIdentifierException |
60
|
|
|
*/ |
61
|
|
|
public function decimalsForCode($currency_code) |
62
|
|
|
{ |
63
|
|
|
$this->ensureInitialized(); |
64
|
|
|
if (isset($this->currency_subunits[ $currency_code]['decimals'])) { |
65
|
|
|
return $this->currency_subunits[ $currency_code]['decimals']; |
66
|
|
|
} |
67
|
|
|
throw new InvalidIdentifierException( |
68
|
|
|
'', |
69
|
|
|
$currency_code, |
70
|
|
|
sprintf( |
71
|
|
|
esc_html__('There is no currency with code %1$s', 'event_espresso'), |
72
|
|
|
$currency_code |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Just verifies this object's data from the filesystem has been loaded |
80
|
|
|
* |
81
|
|
|
* @throws EE_Error |
82
|
|
|
*/ |
83
|
|
|
private function ensureInitialized() |
84
|
|
|
{ |
85
|
|
|
if ($this->currency_subunits === null) { |
86
|
|
|
$this->currency_subunits = json_decode( |
87
|
|
|
EEH_File::get_file_contents(__DIR__ . DS . 'currency-subunits.json'), |
88
|
|
|
true |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
|
94
|
|
|
} |
95
|
|
|
|