1 | <?php namespace Keios\MoneyRight; |
||
29 | class Currency implements Serializable, JsonSerializable |
||
30 | { |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | public static $currencies; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $isoCode; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $name; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | protected $symbol; |
||
51 | |||
52 | /** |
||
53 | * @var null|array |
||
54 | */ |
||
55 | protected $alternateSymbols = null; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $subunit; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $symbolFirst; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $htmlEntity; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $decimalMark; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $thousandsSeparator; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $isoNumeric; |
||
86 | |||
87 | /** |
||
88 | * @var null|string |
||
89 | */ |
||
90 | protected $disambiguateSymbol = null; |
||
91 | |||
92 | /** |
||
93 | * Currency constructor |
||
94 | * |
||
95 | * @param string $isoCode |
||
96 | * |
||
97 | * @throws \Keios\MoneyRight\Exceptions\UnknownCurrencyException |
||
98 | */ |
||
99 | 237 | public function __construct($isoCode) |
|
100 | { |
||
101 | 237 | $currentCurrency = $this->getCurrencyFromIsoCode($isoCode); |
|
102 | |||
103 | 237 | $this->fill($currentCurrency); |
|
104 | 237 | } |
|
105 | |||
106 | /** |
||
107 | * Load currency data from JSON configuration and cache it in static property |
||
108 | */ |
||
109 | 237 | protected function prepareCurrencies() |
|
110 | { |
||
111 | 237 | if (is_null(self::$currencies)) { |
|
112 | 3 | self::$currencies = self::loadCurrencies(); |
|
113 | 3 | } |
|
114 | 237 | } |
|
115 | |||
116 | /** |
||
117 | * Loads currency information from preloaded data by ISO index |
||
118 | * |
||
119 | * @param $isoCode |
||
120 | * |
||
121 | * @return array |
||
122 | * @throws \Keios\MoneyRight\Exceptions\UnknownCurrencyException |
||
123 | */ |
||
124 | 237 | protected function getCurrencyFromIsoCode($isoCode) |
|
125 | { |
||
126 | 237 | $this->prepareCurrencies(); |
|
127 | |||
128 | 237 | $isoCode = strtolower($isoCode); |
|
129 | |||
130 | 237 | if (!array_key_exists($isoCode, self::$currencies)) { |
|
131 | 6 | throw new UnknownCurrencyException('Currency with '.$isoCode.' iso code does not exist!'); |
|
132 | } |
||
133 | |||
134 | 237 | return self::$currencies[$isoCode]; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Protected setter for all fields |
||
139 | * |
||
140 | * @param array $data |
||
141 | */ |
||
142 | 237 | protected function fill(array $data) |
|
143 | { |
||
144 | 237 | $this->isoCode = $data['iso_code']; |
|
145 | 237 | $this->name = $data['name']; |
|
146 | 237 | $this->symbol = $data['symbol']; |
|
147 | 237 | $this->subunit = $data['subunit']; |
|
148 | 237 | $this->symbolFirst = $data['symbol_first']; |
|
149 | 237 | $this->htmlEntity = $data['html_entity']; |
|
150 | 237 | $this->decimalMark = $data['decimal_mark']; |
|
151 | 237 | $this->thousandsSeparator = $data['thousands_separator']; |
|
152 | 237 | $this->isoNumeric = $data['iso_numeric']; |
|
153 | |||
154 | 237 | if (array_key_exists('alternate_symbols', $data)) { |
|
155 | 237 | $this->alternateSymbols = $data['alternate_symbols']; |
|
156 | 237 | } |
|
157 | |||
158 | 237 | if (array_key_exists('disambiguate_symbol', $data)) { |
|
159 | 3 | $this->disambiguateSymbol = $data['disambiguate_symbol']; |
|
160 | 3 | } |
|
161 | 237 | } |
|
162 | |||
163 | /** |
||
164 | * Prepares data for serialization |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | 3 | protected function aggregateData() |
|
169 | { |
||
170 | $data = [ |
||
171 | 3 | 'iso_code' => $this->isoCode, |
|
172 | 3 | 'name' => $this->name, |
|
173 | 3 | 'symbol' => $this->symbol, |
|
174 | 3 | 'subunit' => $this->subunit, |
|
175 | 3 | 'symbol_first' => $this->symbolFirst, |
|
176 | 3 | 'html_entity' => $this->htmlEntity, |
|
177 | 3 | 'decimal_mark' => $this->decimalMark, |
|
178 | 3 | 'thousands_separator' => $this->thousandsSeparator, |
|
179 | 3 | 'iso_numeric' => $this->isoNumeric, |
|
180 | 3 | ]; |
|
181 | |||
182 | 3 | if (!is_null($this->alternateSymbols)) { |
|
183 | 3 | $data['alternate_symbols'] = $this->alternateSymbols; |
|
184 | 3 | } |
|
185 | |||
186 | 3 | if (!is_null($this->disambiguateSymbol)) { |
|
187 | $data['disambiguate_symbol'] = $this->disambiguateSymbol; |
||
188 | } |
||
189 | |||
190 | 3 | return $data; |
|
191 | } |
||
192 | |||
193 | /* |
||
194 | * GETTERS |
||
195 | */ |
||
196 | |||
197 | /** |
||
198 | * Getter for currency iso code |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | 111 | public function getIsoCode() |
|
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getDecimalMark() |
||
214 | |||
215 | /** |
||
216 | * @return null|array |
||
217 | */ |
||
218 | public function getAlternateSymbols() |
||
222 | |||
223 | /** |
||
224 | * @return null|string |
||
225 | */ |
||
226 | public function getDisambiguateSymbol() |
||
230 | |||
231 | /** |
||
232 | * @return string |
||
233 | */ |
||
234 | public function getHtmlEntity() |
||
238 | |||
239 | /** |
||
240 | * @return string |
||
241 | */ |
||
242 | public function getIsoNumeric() |
||
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public function getName() |
||
254 | |||
255 | /** |
||
256 | * @return string |
||
257 | */ |
||
258 | public function getSubunit() |
||
262 | |||
263 | /** |
||
264 | * @return string |
||
265 | */ |
||
266 | public function getSymbol() |
||
270 | |||
271 | /** |
||
272 | * @return bool |
||
273 | */ |
||
274 | public function getSymbolFirst() |
||
278 | |||
279 | /** |
||
280 | * @return string |
||
281 | */ |
||
282 | public function getThousandsSeparator() |
||
286 | |||
287 | /* |
||
288 | * LOGIC |
||
289 | */ |
||
290 | |||
291 | /** |
||
292 | * Checks if this Currency object is equal to another Currency object |
||
293 | * |
||
294 | * @param \Keios\MoneyRight\Currency $other |
||
295 | * |
||
296 | * @return bool |
||
297 | */ |
||
298 | 105 | public function equals(Currency $other) |
|
302 | |||
303 | /** |
||
304 | * Serialization getter for \Serializable |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | 6 | public function serialize() |
|
312 | |||
313 | /** |
||
314 | * Serialization constructor for \Serializable |
||
315 | * |
||
316 | * @param string $serialized |
||
317 | */ |
||
318 | 6 | public function unserialize($serialized) |
|
319 | { |
||
320 | 6 | $currentCurrency = $this->getCurrencyFromIsoCode(unserialize($serialized)); |
|
321 | |||
322 | 6 | $this->fill($currentCurrency); |
|
323 | 6 | } |
|
324 | |||
325 | /** |
||
326 | * JSON Serialization getter for \JsonSerializable |
||
327 | * |
||
328 | * @return string |
||
329 | */ |
||
330 | 3 | public function jsonserialize() |
|
331 | { |
||
332 | 3 | $data = $this->aggregateData(); |
|
333 | |||
334 | 3 | return $data; |
|
335 | } |
||
336 | |||
337 | /** |
||
338 | * Loads currency config from JSON files |
||
339 | * @return array |
||
340 | */ |
||
341 | 3 | public static function loadCurrencies() |
|
345 | } |