forked from h4kuna/exchange
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Exchange.php
424 lines (360 loc) · 9.67 KB
/
Exchange.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<?php
namespace Exchange;
use Nette;
/**
* PHP > 5.3
*
* @author Milan Matějček
* @since 2009-06-22 - version 0.5
* @version 2.5
* @property-read $context
* @property-read $date
*/
class Exchange extends \ArrayIterator implements IExchange {
/**
* number of version
* @var string
*/
private static $version = FALSE;
/** @var Nette\Utils\Html */
private static $href;
/**
* param in url for change value
*/
const PARAM_CURRENCY = 'currency';
const PARAM_VAT = 'vat';
//-----------------config section-----------------------------------------------
/**
* default money on web, must be UPPER
* @var string
*/
protected $default = self::CZK;
/** @var Storage */
protected $storage;
/** @var Download */
protected $download;
/**
* show actual money for web and is first in array, must be UPPER
* @var string
*/
protected $web;
/** @var bool */
protected $globalVat = FALSE;
/**
* vat, only prefered value [%]
* @example 20% = 1.20
* @var real
*/
protected $vat = 1.2;
//------------------------------------------------------------------------------
/**
* last working value
* @var array
*/
protected $lastChange = array(NULL, NULL);
/** @var DateTime */
private $date;
/** @var Nette\DI\IContainer */
private $context;
public function __construct(Nette\DI\IContainer $context) {
parent::__construct();
$this->context = $context;
$this->download();
$this->session();
$this->loadCurrencies();
}
/**
* return property of currency
* @example ->getSymbol(); ->getSymbol('usd');
* @param string $name
* @param strong $args
* @return mixed
*/
public function __call($name, $args) {
$n = \strtolower(\substr($name, 3));
if ($this->download->getProperty($n)) {
return $this->getCurrency(!isset($args[0]) ? $this->key : $args[0], $n);
}
throw new ExchangeException('Call undefined method ' . $name);
}
/** set date for download */
public function setDate($date = NULL) {
$this->date = $date instanceof \DateTime ? $date : new \DateTime($date);
$this->download($this->date);
}
/**
* @param string $code
* @param key property of currency
* @return Nette\Utils\Html
*/
public function currencyLink($code, $key = self::SYMBOL) {
$code = $this->loadCurrency($code);
$a = self::getHref();
if (isset($this[$code][$key])) {
$a->setText($this[$code][$key]);
} else {
$a->setText($code);
}
if ($this->web === $code) {
$a->class = 'current';
}
return $a->href(NULL, array(self::PARAM_CURRENCY => $code));
}
/**
* create link for vat
* @param string $textOn
* @param string $textOff
* @return Nette\Utils\Html
*/
public function vatLink($textOn, $textOff) {
$a = self::getHref();
$a->href(NULL, array(self::PARAM_VAT => !$this->globalVat));
if ($this->globalVat) {
$a->setText($textOff);
} else {
$a->setText($textOn);
}
return $a;
}
/**
* array for form to addSelect
* @param string $key
* @return array
*/
public function selectInput($key = self::SYMBOL) {
$out = array();
foreach ($this as $k => $v) {
$out[$k] = isset($v[$key])? $v[$key]: $k;
}
return $out;
}
/**
* create helper to template
* @param Nette\Templating\FileTemplate $tpl user tamplate
*/
public function registerAsHelper($tpl = NULL) {
$tpl = ($tpl === NULL) ? $this->context->application->template : $tpl;
$tpl->registerHelper('currencyVat', callback($this, 'formatVat'));
$tpl->registerHelper('currency', callback($this, 'format'));
$tpl->exchange = $this;
}
/**
* transfer number by exchange rate
* @param double|int|string $price number
* @param string $from default currency
* @param string $to output currency
* @param int $round number round
* @return double
*/
public function change($price, $from=FALSE, $to=FALSE, $round=FALSE) {
if (is_string($price)) {
$price = (double) Download::stroke2point($price);
}
$from = (!$from) ? $this->default : $this->loadCurrency($from);
$to = (!$to) ? $this->web : $this->loadCurrency($to);
$price = $this[$to][self::RATE] / $this[$from][self::RATE] * $price;
if ($round !== FALSE) {
$price = round($price, $round);
}
return $price;
}
/**
* count, format price and set vat
* @param number $number price
* @param string|bool $from TRUE currency doesn't counting, FALSE set actual
* @param string $to output currency, FALSE set actual
* @param bool|real $vat use vat, but get vat by method $this->formatVat(), look at to globatVat upper
* @return number string
*/
public function format($number, $from=NULL, $to=NULL, $vat=FALSE) {
if ($to != FALSE) {
$old = $this->web;
$to = $this->loadCurrency($to);
$this->web = $to;
}
if ($from !== TRUE) {
$number = $this->change($number, $from, $to);
}
$getVat = FALSE;
if ($vat === FALSE) {
$vat = $this->vat;
} elseif ($vat === TRUE) {
$getVat = TRUE;
$vat = $this->vat;
} else {
$vat = (double) $vat;
}
$withVat = $number * $vat;
if ($this->globalVat || $getVat) {
$number = $withVat;
}
$number = $this->numberFormating($number, $this->web);
if ($to != FALSE) {
$this->web = $old;
} else {
$to = $this->web;
}
$this->lastChange = array($withVat, $to);
return $number;
}
/**
* before call this method MUST call method format()
* formating price only with vat
* @return string
*/
public function formatVat() {
return $this->numberFormating($this->lastChange[0], $this->lastChange[1]);
}
/**
* load currency by code
* @param string $code
* @return string
*/
public function loadCurrency($code) {
$code = \strtoupper($code);
if (!$this->offsetExists($code)) {
$this->offsetSet($code, $this->storage[$code]);
}
return $code;
}
/**
* @param code1, code2, ...
* @return array
*/
public function loadCurrencies(/* ... */) {
$codes = \func_get_args();
if (empty($codes)) {
$codes = $this->download->getCurrencies();
} elseif ($codes[0] === TRUE) {
$codes = $this->getAllCode();
}
foreach ($codes as $code) {
$this->loadCurrency($code);
}
}
// <editor-fold defaultstate="collapsed" desc="getter">
/**
* @param bool $codeSort
* @return array
*/
public function & getAllCode() {
return $this->storage->getAllCode();
}
/**
* return value of array, you can see __call()
* @param string $code currency
* @param string $key use value of constant upper, rate, code
* @return mix
*/
public function getCurrency($code=FALSE, $key=FALSE) {
$code = $code ? $this->loadCurrency($code) : $this->web;
return (!$key) ? $this[$code] : $this[$code][$key];
}
/** @return float */
public function getVat() {
return $this->vat;
}
/** @return number */
public function getPercentVat()
{
return round(($this->vat - 1) * 100, 2);
}
/** @return string */
public function getWeb() {
return $this->web;
}
/** @return Nette\DI\IContainer */
public function getContext() {
return $this->context;
}
public function getDefault() {
return $this->default;
}
public function getDate() {
if (!$this->date)
$this->setDate();
return $this->date;
}
/**
* version of this class
* @return string
*/
static public function getVersion() {
if (self::$version === FALSE) {
$rc = new \ReflectionClass(__CLASS__);
$found = array();
preg_match('~@version (.*)~', $rc->getDocComment(), $array);
self::$version = $array[1];
}
return self::$version;
}
// </editor-fold>
//-----------------protected
/**
* formating number
* @param number $number
* @return string
*/
protected function numberFormating($number, $to) {
$change = $this->download->getRchange();
$change[0] = number_format($number, $this[$to][self::DECIMAL], $this[$to][self::DEC_POINT], $this[$to][self::THOUSANDS]);
return str_replace($this->download->getRfound(), $change, $this[$to][self::NUM_FORMAT]);
}
/**
* start download the source
* @return void
*/
protected function download(\DateTime $date = NULL) {
if ($this->storage === NULL) {
$this->storage = new File($this->context->cacheStorage, $date);
} elseif (!is_object($this->storage)) {
$this->storage = new $this->storage($this->context->cacheStorage, $date);
}
if ($this->download === NULL) {
$this->download = new CnbDay($this->storage, $this->default, $date);
} elseif (!is_object($this->download)) {
$this->download = new $this->download($this->storage, $this->default, $date);
}
if (!($this->download instanceof IDownload)) {
throw new ExchangeException('Class for download must be instance of ' . __NAMESPACE__ . '\IDownload.');
}
if (!$this->storage->needUpdate())
return;
$this->download->downloading();
}
/**
* setup session
*/
protected function session($expiration = '+30 days') {
$session = $this->context->session->getSection(__NAMESPACE__);
$session->setExpiration($expiration);
$request = $this->context->httpRequest;
//-------------crrency------------------------------------------------------
$qVal = strtoupper($request->getQuery(self::PARAM_CURRENCY));
$currency = $this->storage[$qVal];
if (empty($currency)) {
$qVal = NULL;
} else {
$this->offsetSet($qVal, $currency);
}
if ($qVal) {
$session->currency = $qVal;
} elseif (!isset($session->currency)) {
$session->currency = is_null($this->web) ? $this->default : $this->web;
}
$this->web = $session->currency;
//-------------vat----------------------------------------------------------
$qVal = $request->getQuery(self::PARAM_VAT);
if ($qVal !== NULL) {
$this->globalVat = $session->vat = (bool) $qVal;
} elseif (!isset($session->vat)) {
$session->vat = $this->globalVat;
}
}
/** @return Nette\Utils\Html */
protected static function getHref() {
if (!self::$href)
self::$href = Nette\Utils\Html::el('a');
return clone self::$href;
}
}