1 | <?php |
||
24 | class MoneyFactory |
||
25 | { |
||
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( |
||
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) |
||
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) |
||
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) |
||
143 | |||
144 | |||
145 | |||
146 | |||
147 | /** |
||
148 | * @return Calculator |
||
149 | */ |
||
150 | public function calculator() |
||
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() |
||
184 | |||
185 | |||
186 | } |
||
187 | // End of file CreateMoney.php |
||
189 |