1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\currency; |
4
|
|
|
|
5
|
|
|
use EE_Error; |
6
|
|
|
use EEH_File; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class CountryCurrencyDao |
15
|
|
|
* retrieves country currency data from the country-currencies.json |
16
|
|
|
* in order to provide an accurate and uneditable data source |
17
|
|
|
* For user configurable currency details, use EE_Currency_Config |
18
|
|
|
* |
19
|
|
|
* @package EventEspresso\core\services\currency |
20
|
|
|
* @author Brent Christensen |
21
|
|
|
* @since $VID:$ |
22
|
|
|
*/ |
23
|
|
|
class CountryCurrencyDao |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array[] $country_currency_data |
28
|
|
|
*/ |
29
|
|
|
private $country_currency_data; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array[] $country_currencies_by_iso_code |
33
|
|
|
*/ |
34
|
|
|
private $country_currencies_by_iso_code; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array[] $country_currencies_by_currency |
38
|
|
|
*/ |
39
|
|
|
private $country_currencies_by_currency; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array[] |
44
|
|
|
* @throws EE_Error |
45
|
|
|
*/ |
46
|
|
|
private function initializeCountryCurrencyData() |
47
|
|
|
{ |
48
|
|
|
if ($this->country_currency_data === null) { |
49
|
|
|
$country_currency_data = json_decode( |
50
|
|
|
EEH_File::get_file_contents(__DIR__ . DS . 'country-currencies.json'), |
51
|
|
|
true |
52
|
|
|
); |
53
|
|
|
$this->parseCountryCurrencyData($country_currency_data); |
54
|
|
|
} |
55
|
|
|
return $this->country_currency_data; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array[] $country_currency_data |
61
|
|
|
*/ |
62
|
|
|
private function parseCountryCurrencyData($country_currency_data) |
63
|
|
|
{ |
64
|
|
|
foreach ($country_currency_data as $country_currency) { |
65
|
|
|
$this->country_currencies_by_iso_code[ $country_currency['CountryISO'] ] = $country_currency; |
66
|
|
|
$this->country_currencies_by_currency[ $country_currency['CurrencyCode'] ] = $country_currency; |
67
|
|
|
} |
68
|
|
|
$this->country_currency_data = $country_currency_data; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $CNT_ISO |
74
|
|
|
* @return array |
75
|
|
|
* @throws EE_Error |
76
|
|
|
* @throws InvalidArgumentException |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function getCountryCurrencyByIsoCode($CNT_ISO = '') |
79
|
|
|
{ |
80
|
|
|
$this->initializeCountryCurrencyData(); |
81
|
|
|
if (! isset($this->country_currencies_by_iso_code[ $CNT_ISO ])) { |
82
|
|
|
throw new InvalidArgumentException( |
83
|
|
|
sprintf( |
84
|
|
|
esc_html__( |
85
|
|
|
'Valid country currency data could not be found for the "%1$s" country code;', |
86
|
|
|
'event_espresso' |
87
|
|
|
), |
88
|
|
|
$CNT_ISO |
89
|
|
|
) |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
return $this->country_currencies_by_iso_code[ $CNT_ISO ]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param string $code |
98
|
|
|
* @return array |
99
|
|
|
* @throws EE_Error |
100
|
|
|
* @throws InvalidArgumentException |
101
|
|
|
*/ |
102
|
|
View Code Duplication |
public function getCountryCurrencyByCurrencyCode($code = '') |
103
|
|
|
{ |
104
|
|
|
$this->initializeCountryCurrencyData(); |
105
|
|
|
if (! isset($this->country_currencies_by_currency[ $code ])) { |
106
|
|
|
throw new InvalidArgumentException( |
107
|
|
|
sprintf( |
108
|
|
|
esc_html__( |
109
|
|
|
'A valid currency could not be found for the "%1$s" currency code;', |
110
|
|
|
'event_espresso' |
111
|
|
|
), |
112
|
|
|
$code |
113
|
|
|
) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
return $this->country_currencies_by_currency[ $code ]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|