@@ -19,14 +19,14 @@ |
||
19 | 19 | class CurrencyCodeCurrencyAmountFormatter implements CurrencyAmountFormatterInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * @param string $money_amount |
|
24 | - * @param Currency $currency |
|
25 | - * @return string |
|
26 | - */ |
|
27 | - public function format($money_amount, Currency $currency) |
|
28 | - { |
|
29 | - return $money_amount . ' ' . $currency->code(); |
|
30 | - } |
|
22 | + /** |
|
23 | + * @param string $money_amount |
|
24 | + * @param Currency $currency |
|
25 | + * @return string |
|
26 | + */ |
|
27 | + public function format($money_amount, Currency $currency) |
|
28 | + { |
|
29 | + return $money_amount . ' ' . $currency->code(); |
|
30 | + } |
|
31 | 31 | |
32 | 32 | } |
@@ -19,50 +19,50 @@ |
||
19 | 19 | interface CurrencyAmountFormatterInterface |
20 | 20 | { |
21 | 21 | |
22 | - /** |
|
23 | - * do NOT apply any formatting |
|
24 | - * eg: 123456 |
|
25 | - */ |
|
26 | - const RAW = 0; |
|
27 | - |
|
28 | - /** |
|
29 | - * only format money amount by adding the decimal mark |
|
30 | - * eg: 1234.56 |
|
31 | - */ |
|
32 | - const DECIMAL_ONLY = 1; |
|
33 | - |
|
34 | - /** |
|
35 | - * format money amount by adding decimal mark and thousands separator |
|
36 | - * eg: 1,234.56 |
|
37 | - */ |
|
38 | - const ADD_THOUSANDS = 2; |
|
39 | - |
|
40 | - /** |
|
41 | - * format money amount by adding decimal mark, thousands separator, and currency sign |
|
42 | - * eg: $1,234.56 |
|
43 | - */ |
|
44 | - const ADD_CURRENCY_SIGN = 3; |
|
45 | - |
|
46 | - /** |
|
47 | - * format money amount by adding decimal mark, thousands separator, currency sign, and currency code |
|
48 | - * eg: $1,234.56 USD |
|
49 | - */ |
|
50 | - const ADD_CURRENCY_CODE = 4; |
|
51 | - |
|
52 | - /** |
|
53 | - * format money amount by adding decimal mark, thousands separator, currency sign, |
|
54 | - * and currency code wrapped in HTML span tag with HTML class |
|
55 | - * eg: $1,234.56 <span class="currency-code">(USD)</span> |
|
56 | - */ |
|
57 | - const INTERNATIONAL = 5; |
|
58 | - |
|
59 | - |
|
60 | - /** |
|
61 | - * @param string $money_amount |
|
62 | - * @param Currency $currency |
|
63 | - * @return string |
|
64 | - */ |
|
65 | - public function format($money_amount, Currency $currency); |
|
22 | + /** |
|
23 | + * do NOT apply any formatting |
|
24 | + * eg: 123456 |
|
25 | + */ |
|
26 | + const RAW = 0; |
|
27 | + |
|
28 | + /** |
|
29 | + * only format money amount by adding the decimal mark |
|
30 | + * eg: 1234.56 |
|
31 | + */ |
|
32 | + const DECIMAL_ONLY = 1; |
|
33 | + |
|
34 | + /** |
|
35 | + * format money amount by adding decimal mark and thousands separator |
|
36 | + * eg: 1,234.56 |
|
37 | + */ |
|
38 | + const ADD_THOUSANDS = 2; |
|
39 | + |
|
40 | + /** |
|
41 | + * format money amount by adding decimal mark, thousands separator, and currency sign |
|
42 | + * eg: $1,234.56 |
|
43 | + */ |
|
44 | + const ADD_CURRENCY_SIGN = 3; |
|
45 | + |
|
46 | + /** |
|
47 | + * format money amount by adding decimal mark, thousands separator, currency sign, and currency code |
|
48 | + * eg: $1,234.56 USD |
|
49 | + */ |
|
50 | + const ADD_CURRENCY_CODE = 4; |
|
51 | + |
|
52 | + /** |
|
53 | + * format money amount by adding decimal mark, thousands separator, currency sign, |
|
54 | + * and currency code wrapped in HTML span tag with HTML class |
|
55 | + * eg: $1,234.56 <span class="currency-code">(USD)</span> |
|
56 | + */ |
|
57 | + const INTERNATIONAL = 5; |
|
58 | + |
|
59 | + |
|
60 | + /** |
|
61 | + * @param string $money_amount |
|
62 | + * @param Currency $currency |
|
63 | + * @return string |
|
64 | + */ |
|
65 | + public function format($money_amount, Currency $currency); |
|
66 | 66 | |
67 | 67 | |
68 | 68 | } |
@@ -15,13 +15,13 @@ |
||
15 | 15 | interface MoneyFormatterInterface |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * applies formatting based on the specified formatting level |
|
20 | - * corresponding to one of the constants on MoneyFormatter |
|
21 | - * |
|
22 | - * @param Money $money |
|
23 | - * @param int $formatting_level |
|
24 | - * @return string |
|
25 | - */ |
|
26 | - public function format(Money $money, $formatting_level = CurrencyAmountFormatterInterface::ADD_THOUSANDS); |
|
18 | + /** |
|
19 | + * applies formatting based on the specified formatting level |
|
20 | + * corresponding to one of the constants on MoneyFormatter |
|
21 | + * |
|
22 | + * @param Money $money |
|
23 | + * @param int $formatting_level |
|
24 | + * @return string |
|
25 | + */ |
|
26 | + public function format(Money $money, $formatting_level = CurrencyAmountFormatterInterface::ADD_THOUSANDS); |
|
27 | 27 | } |
@@ -17,79 +17,79 @@ |
||
17 | 17 | class MoneyFormatter implements MoneyFormatterInterface |
18 | 18 | { |
19 | 19 | |
20 | - /** |
|
21 | - * @var CurrencyAmountFormatterInterface[] $formatters |
|
22 | - */ |
|
23 | - protected $formatters; |
|
24 | - |
|
25 | - |
|
26 | - /** |
|
27 | - * MoneyFormatter constructor. |
|
28 | - * |
|
29 | - * @param CurrencyAmountFormatterInterface[] $formatters |
|
30 | - */ |
|
31 | - public function __construct(array $formatters = array()) |
|
32 | - { |
|
33 | - $this->formatters = $formatters; |
|
34 | - } |
|
35 | - |
|
36 | - |
|
37 | - /** |
|
38 | - * @return CurrencyAmountFormatterInterface[] |
|
39 | - */ |
|
40 | - protected function formatters() |
|
41 | - { |
|
42 | - if (empty($this->formatters)) { |
|
43 | - $this->initializeFormatters(); |
|
44 | - } |
|
45 | - return $this->formatters; |
|
46 | - } |
|
47 | - |
|
48 | - |
|
49 | - |
|
50 | - /** |
|
51 | - * initializes a filterable array of CurrencyAmountFormatterInterface services |
|
52 | - */ |
|
53 | - protected function initializeFormatters() |
|
54 | - { |
|
55 | - $this->formatters = apply_filters( |
|
56 | - 'FHEE__EventEspresso_core_services_currency_formatters_MoneyFormatter__initializeFormatters__MoneyFormatters_array', |
|
57 | - array( |
|
58 | - CurrencyAmountFormatterInterface::DECIMAL_ONLY => new DecimalCurrencyAmountFormatter(), |
|
59 | - CurrencyAmountFormatterInterface::ADD_THOUSANDS => new ThousandsCurrencyAmountFormatter(), |
|
60 | - CurrencyAmountFormatterInterface::ADD_CURRENCY_SIGN => new CurrencySignCurrencyAmountFormatter(), |
|
61 | - CurrencyAmountFormatterInterface::ADD_CURRENCY_CODE => new CurrencyCodeCurrencyAmountFormatter(), |
|
62 | - CurrencyAmountFormatterInterface::INTERNATIONAL => new InternationalCurrencyAmountFormatter(), |
|
63 | - ) |
|
64 | - ); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * applies formatting based on the specified formatting level |
|
71 | - * corresponding to one of the constants on MoneyFormatter |
|
72 | - * |
|
73 | - * @param Money $money |
|
74 | - * @param int $formatting_level |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function format(Money $money, $formatting_level = CurrencyAmountFormatterInterface::ADD_THOUSANDS) |
|
78 | - { |
|
79 | - $amount = $money->floatAmount(); |
|
80 | - $formatters = $this->formatters(); |
|
81 | - for ($x = 1; $x <= $formatting_level; $x++) { |
|
82 | - if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof CurrencyAmountFormatterInterface) { |
|
83 | - $amount = $formatters[ $x ]->format($amount, $money->currency()); |
|
84 | - } |
|
85 | - } |
|
86 | - return (string) apply_filters( |
|
87 | - 'FHEE__EventEspresso_core_domain_values_currency_formatters_MoneyFormatter__format__formatted_amount', |
|
88 | - $amount, |
|
89 | - $money, |
|
90 | - $this |
|
91 | - ); |
|
92 | - } |
|
20 | + /** |
|
21 | + * @var CurrencyAmountFormatterInterface[] $formatters |
|
22 | + */ |
|
23 | + protected $formatters; |
|
24 | + |
|
25 | + |
|
26 | + /** |
|
27 | + * MoneyFormatter constructor. |
|
28 | + * |
|
29 | + * @param CurrencyAmountFormatterInterface[] $formatters |
|
30 | + */ |
|
31 | + public function __construct(array $formatters = array()) |
|
32 | + { |
|
33 | + $this->formatters = $formatters; |
|
34 | + } |
|
35 | + |
|
36 | + |
|
37 | + /** |
|
38 | + * @return CurrencyAmountFormatterInterface[] |
|
39 | + */ |
|
40 | + protected function formatters() |
|
41 | + { |
|
42 | + if (empty($this->formatters)) { |
|
43 | + $this->initializeFormatters(); |
|
44 | + } |
|
45 | + return $this->formatters; |
|
46 | + } |
|
47 | + |
|
48 | + |
|
49 | + |
|
50 | + /** |
|
51 | + * initializes a filterable array of CurrencyAmountFormatterInterface services |
|
52 | + */ |
|
53 | + protected function initializeFormatters() |
|
54 | + { |
|
55 | + $this->formatters = apply_filters( |
|
56 | + 'FHEE__EventEspresso_core_services_currency_formatters_MoneyFormatter__initializeFormatters__MoneyFormatters_array', |
|
57 | + array( |
|
58 | + CurrencyAmountFormatterInterface::DECIMAL_ONLY => new DecimalCurrencyAmountFormatter(), |
|
59 | + CurrencyAmountFormatterInterface::ADD_THOUSANDS => new ThousandsCurrencyAmountFormatter(), |
|
60 | + CurrencyAmountFormatterInterface::ADD_CURRENCY_SIGN => new CurrencySignCurrencyAmountFormatter(), |
|
61 | + CurrencyAmountFormatterInterface::ADD_CURRENCY_CODE => new CurrencyCodeCurrencyAmountFormatter(), |
|
62 | + CurrencyAmountFormatterInterface::INTERNATIONAL => new InternationalCurrencyAmountFormatter(), |
|
63 | + ) |
|
64 | + ); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * applies formatting based on the specified formatting level |
|
71 | + * corresponding to one of the constants on MoneyFormatter |
|
72 | + * |
|
73 | + * @param Money $money |
|
74 | + * @param int $formatting_level |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function format(Money $money, $formatting_level = CurrencyAmountFormatterInterface::ADD_THOUSANDS) |
|
78 | + { |
|
79 | + $amount = $money->floatAmount(); |
|
80 | + $formatters = $this->formatters(); |
|
81 | + for ($x = 1; $x <= $formatting_level; $x++) { |
|
82 | + if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof CurrencyAmountFormatterInterface) { |
|
83 | + $amount = $formatters[ $x ]->format($amount, $money->currency()); |
|
84 | + } |
|
85 | + } |
|
86 | + return (string) apply_filters( |
|
87 | + 'FHEE__EventEspresso_core_domain_values_currency_formatters_MoneyFormatter__format__formatted_amount', |
|
88 | + $amount, |
|
89 | + $money, |
|
90 | + $this |
|
91 | + ); |
|
92 | + } |
|
93 | 93 | |
94 | 94 | } |
95 | 95 | // End of file MoneyFormatter.php |
@@ -79,8 +79,8 @@ |
||
79 | 79 | $amount = $money->floatAmount(); |
80 | 80 | $formatters = $this->formatters(); |
81 | 81 | for ($x = 1; $x <= $formatting_level; $x++) { |
82 | - if (isset($formatters[ $x ]) && $formatters[ $x ] instanceof CurrencyAmountFormatterInterface) { |
|
83 | - $amount = $formatters[ $x ]->format($amount, $money->currency()); |
|
82 | + if (isset($formatters[$x]) && $formatters[$x] instanceof CurrencyAmountFormatterInterface) { |
|
83 | + $amount = $formatters[$x]->format($amount, $money->currency()); |
|
84 | 84 | } |
85 | 85 | } |
86 | 86 | return (string) apply_filters( |
@@ -24,163 +24,163 @@ |
||
24 | 24 | class MoneyFactory |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * @var CurrencyFactory $currency_factory |
|
29 | - */ |
|
30 | - protected $currency_factory; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var Calculator $calculator |
|
34 | - */ |
|
35 | - protected $calculator; |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * CreateMoney constructor. |
|
40 | - * |
|
41 | - * @param CurrencyFactory $currency_factory |
|
42 | - * @param Calculator $calculator |
|
43 | - */ |
|
44 | - public function __construct( |
|
45 | - CurrencyFactory $currency_factory, |
|
46 | - Calculator $calculator = null |
|
47 | - ) { |
|
48 | - $this->calculator = $calculator; |
|
49 | - $this->currency_factory = $currency_factory; |
|
50 | - } |
|
51 | - |
|
52 | - |
|
53 | - /** |
|
54 | - * factory method that returns a Money object using amount specified in the currency's subunits |
|
55 | - * example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD') |
|
56 | - * |
|
57 | - * @param int $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents |
|
58 | - * example: $12.50 USD would equate to a subunits amount of 1250 |
|
59 | - * @param string $currency_code |
|
60 | - * @return Money |
|
61 | - * @throws EE_Error |
|
62 | - * @throws InvalidArgumentException |
|
63 | - * @throws InvalidDataTypeException |
|
64 | - */ |
|
65 | - public function createFromSubUnits($subunits_amount, $currency_code = '') |
|
66 | - { |
|
67 | - $currency = $this->currency_factory->createFromCode($currency_code); |
|
68 | - return new Money( |
|
69 | - // shift decimal BACK by number of places for currency |
|
70 | - $subunits_amount * pow(10, $currency->decimalPlaces() * -1), |
|
71 | - $currency, |
|
72 | - $this->calculator() |
|
73 | - ); |
|
74 | - } |
|
75 | - |
|
76 | - |
|
77 | - |
|
78 | - /** |
|
79 | - * factory method that returns a Money object using the currency corresponding to the site's country |
|
80 | - * example: CreateMoney::forSite(12.5) |
|
81 | - * |
|
82 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
83 | - * example: $12.5 USD would equate to a standard amount of 12.50 |
|
84 | - * @return Money |
|
85 | - * @throws EE_Error |
|
86 | - * @throws InvalidArgumentException |
|
87 | - * @throws InvalidDataTypeException |
|
88 | - */ |
|
89 | - public function createForSite($amount) |
|
90 | - { |
|
91 | - return new Money( |
|
92 | - $amount, |
|
93 | - $this->currency_factory->createFromCountryCode(), |
|
94 | - $this->calculator() |
|
95 | - ); |
|
96 | - } |
|
97 | - |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * factory method that returns a Money object using the currency as specified by the supplied ISO country code |
|
102 | - * example: CreateMoney::forCountry(12.5,'US') |
|
103 | - * |
|
104 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
105 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
106 | - * @param string $CNT_ISO |
|
107 | - * @return Money |
|
108 | - * @throws EE_Error |
|
109 | - * @throws InvalidArgumentException |
|
110 | - * @throws InvalidDataTypeException |
|
111 | - */ |
|
112 | - public function createForCountry($amount, $CNT_ISO) |
|
113 | - { |
|
114 | - return new Money( |
|
115 | - $amount, |
|
116 | - $this->currency_factory->createFromCountryCode($CNT_ISO), |
|
117 | - $this->calculator() |
|
118 | - ); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - |
|
123 | - /** |
|
124 | - * factory method that returns a Money object using the currency as specified by the supplied currency code |
|
125 | - * example: CreateMoney::forCurrency(12.5, 'USD') |
|
126 | - * |
|
127 | - * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
128 | - * example: $12.5 USD would equate to a value amount of 12.50 |
|
129 | - * @param string $currency_code |
|
130 | - * @return Money |
|
131 | - * @throws EE_Error |
|
132 | - * @throws InvalidArgumentException |
|
133 | - * @throws InvalidDataTypeException |
|
134 | - */ |
|
135 | - public function createForCurrency($amount, $currency_code) |
|
136 | - { |
|
137 | - return new Money( |
|
138 | - $amount, |
|
139 | - $this->currency_factory->createFromCode($currency_code), |
|
140 | - $this->calculator() |
|
141 | - ); |
|
142 | - } |
|
143 | - |
|
144 | - |
|
145 | - |
|
146 | - |
|
147 | - /** |
|
148 | - * @return Calculator |
|
149 | - */ |
|
150 | - public function calculator() |
|
151 | - { |
|
152 | - $this->initializeCalculators(); |
|
153 | - return $this->calculator; |
|
154 | - } |
|
155 | - |
|
156 | - |
|
157 | - |
|
158 | - /** |
|
159 | - * loops through a filterable array of Calculator services |
|
160 | - * and selects the first one that is supported by the current server |
|
161 | - */ |
|
162 | - protected function initializeCalculators() |
|
163 | - { |
|
164 | - if ($this->calculator instanceof Calculator) { |
|
165 | - return; |
|
166 | - } |
|
167 | - $calculators = apply_filters( |
|
168 | - 'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeCalculators__Calculators_array', |
|
169 | - array( |
|
170 | - 'EventEspresso\core\services\currency\DefaultCalculator', |
|
171 | - ) |
|
172 | - ); |
|
173 | - foreach ($calculators as $calculator) { |
|
174 | - if (! class_exists($calculator)) { |
|
175 | - continue; |
|
176 | - } |
|
177 | - $calculator = new $calculator(); |
|
178 | - if ($calculator instanceof Calculator && $calculator->isSupported()) { |
|
179 | - $this->calculator = $calculator; |
|
180 | - break; |
|
181 | - } |
|
182 | - } |
|
183 | - } |
|
27 | + /** |
|
28 | + * @var CurrencyFactory $currency_factory |
|
29 | + */ |
|
30 | + protected $currency_factory; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var Calculator $calculator |
|
34 | + */ |
|
35 | + protected $calculator; |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * CreateMoney constructor. |
|
40 | + * |
|
41 | + * @param CurrencyFactory $currency_factory |
|
42 | + * @param Calculator $calculator |
|
43 | + */ |
|
44 | + public function __construct( |
|
45 | + CurrencyFactory $currency_factory, |
|
46 | + Calculator $calculator = null |
|
47 | + ) { |
|
48 | + $this->calculator = $calculator; |
|
49 | + $this->currency_factory = $currency_factory; |
|
50 | + } |
|
51 | + |
|
52 | + |
|
53 | + /** |
|
54 | + * factory method that returns a Money object using amount specified in the currency's subunits |
|
55 | + * example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD') |
|
56 | + * |
|
57 | + * @param int $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents |
|
58 | + * example: $12.50 USD would equate to a subunits amount of 1250 |
|
59 | + * @param string $currency_code |
|
60 | + * @return Money |
|
61 | + * @throws EE_Error |
|
62 | + * @throws InvalidArgumentException |
|
63 | + * @throws InvalidDataTypeException |
|
64 | + */ |
|
65 | + public function createFromSubUnits($subunits_amount, $currency_code = '') |
|
66 | + { |
|
67 | + $currency = $this->currency_factory->createFromCode($currency_code); |
|
68 | + return new Money( |
|
69 | + // shift decimal BACK by number of places for currency |
|
70 | + $subunits_amount * pow(10, $currency->decimalPlaces() * -1), |
|
71 | + $currency, |
|
72 | + $this->calculator() |
|
73 | + ); |
|
74 | + } |
|
75 | + |
|
76 | + |
|
77 | + |
|
78 | + /** |
|
79 | + * factory method that returns a Money object using the currency corresponding to the site's country |
|
80 | + * example: CreateMoney::forSite(12.5) |
|
81 | + * |
|
82 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
83 | + * example: $12.5 USD would equate to a standard amount of 12.50 |
|
84 | + * @return Money |
|
85 | + * @throws EE_Error |
|
86 | + * @throws InvalidArgumentException |
|
87 | + * @throws InvalidDataTypeException |
|
88 | + */ |
|
89 | + public function createForSite($amount) |
|
90 | + { |
|
91 | + return new Money( |
|
92 | + $amount, |
|
93 | + $this->currency_factory->createFromCountryCode(), |
|
94 | + $this->calculator() |
|
95 | + ); |
|
96 | + } |
|
97 | + |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * factory method that returns a Money object using the currency as specified by the supplied ISO country code |
|
102 | + * example: CreateMoney::forCountry(12.5,'US') |
|
103 | + * |
|
104 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
105 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
106 | + * @param string $CNT_ISO |
|
107 | + * @return Money |
|
108 | + * @throws EE_Error |
|
109 | + * @throws InvalidArgumentException |
|
110 | + * @throws InvalidDataTypeException |
|
111 | + */ |
|
112 | + public function createForCountry($amount, $CNT_ISO) |
|
113 | + { |
|
114 | + return new Money( |
|
115 | + $amount, |
|
116 | + $this->currency_factory->createFromCountryCode($CNT_ISO), |
|
117 | + $this->calculator() |
|
118 | + ); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + |
|
123 | + /** |
|
124 | + * factory method that returns a Money object using the currency as specified by the supplied currency code |
|
125 | + * example: CreateMoney::forCurrency(12.5, 'USD') |
|
126 | + * |
|
127 | + * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc |
|
128 | + * example: $12.5 USD would equate to a value amount of 12.50 |
|
129 | + * @param string $currency_code |
|
130 | + * @return Money |
|
131 | + * @throws EE_Error |
|
132 | + * @throws InvalidArgumentException |
|
133 | + * @throws InvalidDataTypeException |
|
134 | + */ |
|
135 | + public function createForCurrency($amount, $currency_code) |
|
136 | + { |
|
137 | + return new Money( |
|
138 | + $amount, |
|
139 | + $this->currency_factory->createFromCode($currency_code), |
|
140 | + $this->calculator() |
|
141 | + ); |
|
142 | + } |
|
143 | + |
|
144 | + |
|
145 | + |
|
146 | + |
|
147 | + /** |
|
148 | + * @return Calculator |
|
149 | + */ |
|
150 | + public function calculator() |
|
151 | + { |
|
152 | + $this->initializeCalculators(); |
|
153 | + return $this->calculator; |
|
154 | + } |
|
155 | + |
|
156 | + |
|
157 | + |
|
158 | + /** |
|
159 | + * loops through a filterable array of Calculator services |
|
160 | + * and selects the first one that is supported by the current server |
|
161 | + */ |
|
162 | + protected function initializeCalculators() |
|
163 | + { |
|
164 | + if ($this->calculator instanceof Calculator) { |
|
165 | + return; |
|
166 | + } |
|
167 | + $calculators = apply_filters( |
|
168 | + 'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeCalculators__Calculators_array', |
|
169 | + array( |
|
170 | + 'EventEspresso\core\services\currency\DefaultCalculator', |
|
171 | + ) |
|
172 | + ); |
|
173 | + foreach ($calculators as $calculator) { |
|
174 | + if (! class_exists($calculator)) { |
|
175 | + continue; |
|
176 | + } |
|
177 | + $calculator = new $calculator(); |
|
178 | + if ($calculator instanceof Calculator && $calculator->isSupported()) { |
|
179 | + $this->calculator = $calculator; |
|
180 | + break; |
|
181 | + } |
|
182 | + } |
|
183 | + } |
|
184 | 184 | |
185 | 185 | |
186 | 186 | } |