@@ -33,7 +33,6 @@ |
||
33 | 33 | * CurrencyFactory constructor. |
34 | 34 | * |
35 | 35 | * @param CountryCurrencyDao $country_currencies |
36 | - * @param EE_Organization_Config $organization_config |
|
37 | 36 | */ |
38 | 37 | public function __construct(CountryCurrencyDao $country_currencies) |
39 | 38 | { |
@@ -23,78 +23,78 @@ |
||
23 | 23 | class CurrencyFactory |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var CountryCurrencyDao $country_currencies |
|
28 | - */ |
|
29 | - private $country_currencies; |
|
26 | + /** |
|
27 | + * @var CountryCurrencyDao $country_currencies |
|
28 | + */ |
|
29 | + private $country_currencies; |
|
30 | 30 | |
31 | 31 | |
32 | - /** |
|
33 | - * CurrencyFactory constructor. |
|
34 | - * |
|
35 | - * @param CountryCurrencyDao $country_currencies |
|
36 | - * @param EE_Organization_Config $organization_config |
|
37 | - */ |
|
38 | - public function __construct(CountryCurrencyDao $country_currencies) |
|
39 | - { |
|
40 | - $this->country_currencies = $country_currencies; |
|
41 | - } |
|
32 | + /** |
|
33 | + * CurrencyFactory constructor. |
|
34 | + * |
|
35 | + * @param CountryCurrencyDao $country_currencies |
|
36 | + * @param EE_Organization_Config $organization_config |
|
37 | + */ |
|
38 | + public function __construct(CountryCurrencyDao $country_currencies) |
|
39 | + { |
|
40 | + $this->country_currencies = $country_currencies; |
|
41 | + } |
|
42 | 42 | |
43 | 43 | |
44 | - /** |
|
45 | - * @param array $country_currency |
|
46 | - * @return Currency |
|
47 | - */ |
|
48 | - private function createCurrencyFromCountryCurrency(array $country_currency) |
|
49 | - { |
|
50 | - return new Currency( |
|
51 | - $country_currency['CurrencyCode'], |
|
52 | - new Label( |
|
53 | - $country_currency['CurrencyNameSingle'], |
|
54 | - $country_currency['CurrencyNamePlural'] |
|
55 | - ), |
|
56 | - $country_currency['CurrencySign'], |
|
57 | - $country_currency['CurrencySignB4'], |
|
58 | - $country_currency['CurrencyDecimalPlaces'], |
|
59 | - $country_currency['CurrencyDecimalMark'], |
|
60 | - $country_currency['CurrencyThousands'], |
|
61 | - $country_currency['CurrencySubunits'], |
|
62 | - $country_currency['CurrencySignSeparator'] |
|
63 | - ); |
|
64 | - } |
|
44 | + /** |
|
45 | + * @param array $country_currency |
|
46 | + * @return Currency |
|
47 | + */ |
|
48 | + private function createCurrencyFromCountryCurrency(array $country_currency) |
|
49 | + { |
|
50 | + return new Currency( |
|
51 | + $country_currency['CurrencyCode'], |
|
52 | + new Label( |
|
53 | + $country_currency['CurrencyNameSingle'], |
|
54 | + $country_currency['CurrencyNamePlural'] |
|
55 | + ), |
|
56 | + $country_currency['CurrencySign'], |
|
57 | + $country_currency['CurrencySignB4'], |
|
58 | + $country_currency['CurrencyDecimalPlaces'], |
|
59 | + $country_currency['CurrencyDecimalMark'], |
|
60 | + $country_currency['CurrencyThousands'], |
|
61 | + $country_currency['CurrencySubunits'], |
|
62 | + $country_currency['CurrencySignSeparator'] |
|
63 | + ); |
|
64 | + } |
|
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * returns a Currency object for the supplied country code |
|
69 | - * |
|
70 | - * @param string $CNT_ISO |
|
71 | - * @return Currency |
|
72 | - * @throws EE_Error |
|
73 | - * @throws InvalidArgumentException |
|
74 | - */ |
|
75 | - public function createFromCountryCode($CNT_ISO) |
|
76 | - { |
|
77 | - return $this->createCurrencyFromCountryCurrency( |
|
78 | - $this->country_currencies->getCountryCurrencyByIsoCode($CNT_ISO) |
|
79 | - ); |
|
80 | - } |
|
67 | + /** |
|
68 | + * returns a Currency object for the supplied country code |
|
69 | + * |
|
70 | + * @param string $CNT_ISO |
|
71 | + * @return Currency |
|
72 | + * @throws EE_Error |
|
73 | + * @throws InvalidArgumentException |
|
74 | + */ |
|
75 | + public function createFromCountryCode($CNT_ISO) |
|
76 | + { |
|
77 | + return $this->createCurrencyFromCountryCurrency( |
|
78 | + $this->country_currencies->getCountryCurrencyByIsoCode($CNT_ISO) |
|
79 | + ); |
|
80 | + } |
|
81 | 81 | |
82 | 82 | |
83 | - /** |
|
84 | - * returns a Currency object for the supplied currency code |
|
85 | - * PLZ NOTE: variations may exist between how different countries display the same currency, |
|
86 | - * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
|
87 | - * |
|
88 | - * @param string $code |
|
89 | - * @return Currency |
|
90 | - * @throws InvalidArgumentException |
|
91 | - * @throws EE_Error |
|
92 | - */ |
|
93 | - public function createFromCode($code) |
|
94 | - { |
|
95 | - return $this->createCurrencyFromCountryCurrency( |
|
96 | - $this->country_currencies->getCountryCurrencyByCurrencyCode($code) |
|
97 | - ); |
|
98 | - } |
|
83 | + /** |
|
84 | + * returns a Currency object for the supplied currency code |
|
85 | + * PLZ NOTE: variations may exist between how different countries display the same currency, |
|
86 | + * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results |
|
87 | + * |
|
88 | + * @param string $code |
|
89 | + * @return Currency |
|
90 | + * @throws InvalidArgumentException |
|
91 | + * @throws EE_Error |
|
92 | + */ |
|
93 | + public function createFromCode($code) |
|
94 | + { |
|
95 | + return $this->createCurrencyFromCountryCurrency( |
|
96 | + $this->country_currencies->getCountryCurrencyByCurrencyCode($code) |
|
97 | + ); |
|
98 | + } |
|
99 | 99 | } |
100 | 100 | // Location: CreateCurrency.php |
@@ -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(\EE_Config::instance()->organization->CNT_ISO), |
|
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(\EE_Config::instance()->organization->CNT_ISO), |
|
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 | } |