1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\services\currency; |
4
|
|
|
|
5
|
|
|
use EE_Error; |
6
|
|
|
use EventEspresso\core\domain\values\currency\Money; |
7
|
|
|
use EventEspresso\core\exceptions\InvalidDataTypeException; |
8
|
|
|
use EventEspresso\core\exceptions\InvalidInterfaceException; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class MoneyFactory |
17
|
|
|
* Factory class for creating Money objects |
18
|
|
|
* |
19
|
|
|
* @package Event Espresso |
20
|
|
|
* @author Brent Christensen |
21
|
|
|
* @since $VID:$ |
22
|
|
|
*/ |
23
|
|
|
class MoneyFactory |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var CurrencyFactory $currency_factory |
28
|
|
|
*/ |
29
|
|
|
protected $currency_factory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Calculator $calculator |
33
|
|
|
*/ |
34
|
|
|
protected $calculator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var MoneyFormatter[] $formatters |
38
|
|
|
*/ |
39
|
|
|
protected $formatters; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* CreateMoney constructor. |
44
|
|
|
* |
45
|
|
|
* @param CurrencyFactory $currency_factory |
46
|
|
|
* @param Calculator $calculator |
47
|
|
|
* @param MoneyFormatter[] $formatters |
48
|
|
|
*/ |
49
|
|
|
public function __construct( |
50
|
|
|
CurrencyFactory $currency_factory, |
51
|
|
|
Calculator $calculator = null, |
52
|
|
|
array $formatters = array() |
53
|
|
|
) { |
54
|
|
|
$this->calculator = $calculator; |
55
|
|
|
$this->formatters = $formatters; |
56
|
|
|
$this->currency_factory = $currency_factory; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* factory method that returns a Money object using amount specified in the currency's subunits |
63
|
|
|
* example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD') |
64
|
|
|
* |
65
|
|
|
* @param int $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents |
66
|
|
|
* example: $12.50 USD would equate to a subunits amount of 1250 |
67
|
|
|
* @param string $currency_code |
68
|
|
|
* @return Money |
69
|
|
|
* @throws EE_Error |
70
|
|
|
* @throws InvalidArgumentException |
71
|
|
|
* @throws InvalidDataTypeException |
72
|
|
|
* @throws InvalidInterfaceException |
73
|
|
|
*/ |
74
|
|
|
public function createFromSubUnits($subunits_amount, $currency_code = '') |
75
|
|
|
{ |
76
|
|
|
$currency = $this->currency_factory->createFromCode($currency_code); |
77
|
|
|
return new Money( |
78
|
|
|
// shift decimal BACK by number of places for currency |
79
|
|
|
$subunits_amount * pow(10, $currency->decimalPlaces() * -1), |
80
|
|
|
$currency, |
81
|
|
|
$this->calculator(), |
82
|
|
|
$this->formatters() |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* factory method that returns a Money object using the currency corresponding to the site's country |
90
|
|
|
* example: CreateMoney::forSite(12.5) |
91
|
|
|
* |
92
|
|
|
* @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
93
|
|
|
* example: $12.5 USD would equate to a standard amount of 12.50 |
94
|
|
|
* @return Money |
95
|
|
|
* @throws EE_Error |
96
|
|
|
* @throws InvalidArgumentException |
97
|
|
|
* @throws InvalidDataTypeException |
98
|
|
|
* @throws InvalidInterfaceException |
99
|
|
|
*/ |
100
|
|
|
public function createForSite($amount) |
101
|
|
|
{ |
102
|
|
|
return new Money( |
103
|
|
|
$amount, |
104
|
|
|
$this->currency_factory->createFromCountryCode(), |
105
|
|
|
$this->calculator(), |
106
|
|
|
$this->formatters() |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* factory method that returns a Money object using the currency as specified by the supplied ISO country code |
114
|
|
|
* example: CreateMoney::forCountry(12.5,'US') |
115
|
|
|
* |
116
|
|
|
* @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
117
|
|
|
* example: $12.5 USD would equate to a value amount of 12.50 |
118
|
|
|
* @param string $CNT_ISO |
119
|
|
|
* @return Money |
120
|
|
|
* @throws EE_Error |
121
|
|
|
* @throws InvalidArgumentException |
122
|
|
|
* @throws InvalidDataTypeException |
123
|
|
|
* @throws InvalidInterfaceException |
124
|
|
|
*/ |
125
|
|
|
public function createForCountry($amount, $CNT_ISO) |
126
|
|
|
{ |
127
|
|
|
return new Money( |
128
|
|
|
$amount, |
129
|
|
|
$this->currency_factory->createFromCountryCode($CNT_ISO), |
130
|
|
|
$this->calculator(), |
131
|
|
|
$this->formatters() |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* factory method that returns a Money object using the currency as specified by the supplied currency code |
139
|
|
|
* example: CreateMoney::forCurrency(12.5, 'USD') |
140
|
|
|
* |
141
|
|
|
* @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
142
|
|
|
* example: $12.5 USD would equate to a value amount of 12.50 |
143
|
|
|
* @param string $currency_code |
144
|
|
|
* @return Money |
145
|
|
|
* @throws EE_Error |
146
|
|
|
* @throws InvalidArgumentException |
147
|
|
|
* @throws InvalidDataTypeException |
148
|
|
|
* @throws InvalidInterfaceException |
149
|
|
|
*/ |
150
|
|
|
public function createForCurrency($amount, $currency_code) |
151
|
|
|
{ |
152
|
|
|
return new Money( |
153
|
|
|
$amount, |
154
|
|
|
$this->currency_factory->createFromCode($currency_code), |
155
|
|
|
$this->calculator(), |
156
|
|
|
$this->formatters() |
157
|
|
|
); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
|
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return Calculator |
165
|
|
|
*/ |
166
|
|
|
public function calculator() |
167
|
|
|
{ |
168
|
|
|
$this->initializeCalculators(); |
169
|
|
|
return $this->calculator; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* loops through a filterable array of Calculator services |
176
|
|
|
* and selects the first one that is supported by the current server |
177
|
|
|
*/ |
178
|
|
|
protected function initializeCalculators() |
179
|
|
|
{ |
180
|
|
|
if ($this->calculator instanceof Calculator) { |
181
|
|
|
return; |
182
|
|
|
} |
183
|
|
|
$calculators = apply_filters( |
184
|
|
|
'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeCalculators__Calculators_array', |
185
|
|
|
array( |
186
|
|
|
'\EventEspresso\core\services\currency\DefaultCalculator', |
187
|
|
|
) |
188
|
|
|
); |
189
|
|
|
foreach ($calculators as $calculator) { |
190
|
|
|
if (! class_exists($calculator)) { |
191
|
|
|
continue; |
192
|
|
|
} |
193
|
|
|
$calculator = new $calculator(); |
194
|
|
|
if ($calculator instanceof Calculator && $calculator->isSupported()) { |
195
|
|
|
$this->calculator = $calculator; |
196
|
|
|
break; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return MoneyFormatter[] |
205
|
|
|
*/ |
206
|
|
|
public function formatters() |
207
|
|
|
{ |
208
|
|
|
$this->initializeFormatters(); |
209
|
|
|
return $this->formatters; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
|
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* initializes a filterable array of MoneyFormatter services |
216
|
|
|
*/ |
217
|
|
|
protected function initializeFormatters() |
218
|
|
|
{ |
219
|
|
|
if (! empty($this->formatters)) { |
220
|
|
|
return; |
221
|
|
|
} |
222
|
|
|
$this->formatters = apply_filters( |
223
|
|
|
'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeFormatters__MoneyFormatters_array', |
224
|
|
|
array( |
225
|
|
|
1 => new DecimalMoneyFormatter(), |
226
|
|
|
2 => new ThousandsMoneyFormatter(), |
227
|
|
|
3 => new CurrencySignMoneyFormatter(), |
228
|
|
|
4 => new CurrencyCodeMoneyFormatter(), |
229
|
|
|
5 => new InternationalMoneyFormatter(), |
230
|
|
|
) |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
} |
236
|
|
|
// End of file CreateMoney.php |
237
|
|
|
// Location: core/services/currency/CreateMoney.php |
238
|
|
|
|