1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\currency; |
4
|
|
|
|
5
|
|
|
use EE_Country; |
6
|
|
|
use EE_Error; |
7
|
|
|
use EE_Organization_Config; |
8
|
|
|
use EEM_Country; |
9
|
|
|
use EventEspresso\core\domain\values\currency\Currency; |
10
|
|
|
use EventEspresso\core\entities\Label; |
11
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
12
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
13
|
|
|
use EventEspresso\core\services\loaders\LoaderFactory; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class CreateCurrency |
22
|
|
|
* Factory class for creating Currency objects |
23
|
|
|
* |
24
|
|
|
* @package EventEspresso\core\services\currency |
25
|
|
|
* @author Brent Christensen |
26
|
|
|
* @since $VID:$ |
27
|
|
|
*/ |
28
|
|
|
class CreateCurrency |
29
|
|
|
{ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var EE_Country[] $countries |
33
|
|
|
*/ |
34
|
|
|
protected static $countries_by_iso_code; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var EE_Country[] $countries |
38
|
|
|
*/ |
39
|
|
|
protected static $countries_by_currency; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string $site_country_iso |
43
|
|
|
*/ |
44
|
|
|
protected static $site_country_iso; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return EEM_Country |
49
|
|
|
* @throws InvalidArgumentException |
50
|
|
|
* @throws InvalidDataTypeException |
51
|
|
|
* @throws InvalidInterfaceException |
52
|
|
|
*/ |
53
|
|
|
private static function countryModel() |
54
|
|
|
{ |
55
|
|
|
return LoaderFactory::getLoader()->getShared('EEM_Country'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* returns a Currency object for the supplied country code |
61
|
|
|
* |
62
|
|
|
* @param string $CNT_ISO |
63
|
|
|
* @return Currency |
64
|
|
|
* @throws InvalidDataTypeException |
65
|
|
|
* @throws InvalidInterfaceException |
66
|
|
|
* @throws EE_Error |
67
|
|
|
* @throws InvalidArgumentException |
68
|
|
|
*/ |
69
|
|
|
public static function fromCountryCode($CNT_ISO = null) |
70
|
|
|
{ |
71
|
|
|
$CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : CreateCurrency::getSiteCountryIso(); |
72
|
|
View Code Duplication |
if(isset(CreateCurrency::$countries_by_iso_code[$CNT_ISO])) { |
73
|
|
|
$country = CreateCurrency::$countries_by_iso_code[ $CNT_ISO ]; |
74
|
|
|
} else { |
75
|
|
|
/** @var EE_Country $country */ |
76
|
|
|
$country = CreateCurrency::countryModel()->get_one_by_ID($CNT_ISO); |
77
|
|
|
if (! $country instanceof EE_Country) { |
78
|
|
|
throw new InvalidArgumentException( |
79
|
|
|
sprintf( |
80
|
|
|
esc_html__( |
81
|
|
|
'A valid country could not be found for the "%1$s" country code;', |
82
|
|
|
'event_espresso' |
83
|
|
|
), |
84
|
|
|
$CNT_ISO |
85
|
|
|
) |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
CreateCurrency::$countries_by_iso_code[ $CNT_ISO ] = $country; |
89
|
|
|
CreateCurrency::$countries_by_currency[ $country->currency_code() ] = $country; |
90
|
|
|
} |
91
|
|
|
return new Currency( |
92
|
|
|
$country->currency_code(), |
93
|
|
|
new Label( |
94
|
|
|
$country->currency_name_single(), |
95
|
|
|
$country->currency_name_plural() |
96
|
|
|
), |
97
|
|
|
$country->currency_sign(), |
98
|
|
|
$country->currency_sign_before(), |
99
|
|
|
$country->currency_decimal_places(), |
100
|
|
|
$country->currency_decimal_mark(), |
101
|
|
|
$country->currency_thousands_separator() |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* returns a Currency object for the supplied currency code |
109
|
|
|
* PLZ NOTE: variations may exist between how different countries display the same currency, |
110
|
|
|
* so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
111
|
|
|
* |
112
|
|
|
* @param string $code |
113
|
|
|
* @return Currency |
114
|
|
|
* @throws InvalidInterfaceException |
115
|
|
|
* @throws InvalidDataTypeException |
116
|
|
|
* @throws InvalidArgumentException |
117
|
|
|
* @throws EE_Error |
118
|
|
|
*/ |
119
|
|
|
public static function fromCode($code) |
120
|
|
|
{ |
121
|
|
View Code Duplication |
if (isset(CreateCurrency::$countries_by_currency[ $code ])) { |
122
|
|
|
$country = CreateCurrency::$countries_by_currency[ $code ]; |
123
|
|
|
} else { |
124
|
|
|
/** @var EE_Country $country */ |
125
|
|
|
$country = CreateCurrency::countryModel()->get_one(array(array('CNT_cur_code' => $code))); |
126
|
|
|
if (! $country instanceof EE_Country) { |
127
|
|
|
throw new InvalidArgumentException( |
128
|
|
|
sprintf( |
129
|
|
|
esc_html__( |
130
|
|
|
'A valid currency could not be found for the "%1$s" currency code;', |
131
|
|
|
'event_espresso' |
132
|
|
|
), |
133
|
|
|
$code |
134
|
|
|
) |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
CreateCurrency::$countries_by_iso_code[ $country->ID() ] = $country; |
138
|
|
|
CreateCurrency::$countries_by_currency[$code ] = $country; |
139
|
|
|
} |
140
|
|
|
return new Currency( |
141
|
|
|
$country->currency_code(), |
142
|
|
|
new Label( |
143
|
|
|
$country->currency_name_single(), |
144
|
|
|
$country->currency_name_plural() |
145
|
|
|
), |
146
|
|
|
$country->currency_sign(), |
147
|
|
|
$country->currency_sign_before(), |
148
|
|
|
$country->currency_decimal_places(), |
149
|
|
|
$country->currency_decimal_mark(), |
150
|
|
|
$country->currency_thousands_separator() |
151
|
|
|
); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
|
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return string |
158
|
|
|
* @throws InvalidArgumentException |
159
|
|
|
* @throws InvalidDataTypeException |
160
|
|
|
* @throws InvalidInterfaceException |
161
|
|
|
*/ |
162
|
|
|
public static function getSiteCountryIso() |
163
|
|
|
{ |
164
|
|
|
if (empty(CreateCurrency::$site_country_iso)) { |
165
|
|
|
$config = LoaderFactory::getLoader()->getShared('EE_Config'); |
166
|
|
|
CreateCurrency::$site_country_iso = $config->organization !== null |
167
|
|
|
&& $config->organization instanceof EE_Organization_Config |
168
|
|
|
? $config->organization->CNT_ISO |
169
|
|
|
: 'US'; |
170
|
|
|
} |
171
|
|
|
return CreateCurrency::$site_country_iso; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
// Location: CreateCurrency.php |
178
|
|
|
|