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