-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange_model.php
76 lines (64 loc) · 2.13 KB
/
exchange_model.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
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
/**
* Exchange model
*
* Used for selling/buying portal. Very simple class, mainly used for quick
* currency convertations from one currency to other.
*
* Here I am using fresh data that are fetched from European Central Bank.
*
* @todo Store data somewhere, in that case I don't need to trouble bank server all the time.
*
* @copyright 2014 Roberts Rozkalns
* @version Release: @1@
* @since Class available since Release 0.1
*/
class Exchange_Model {
public $rates = array();
public function __construct() {
parent::__construct();
$this->rates['EUR'] = "1";
if (!$this->lvlDead()) {
$this->rates['LVL'] = "0.702804";
}
$XMLContent = file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");
foreach ($XMLContent as $line) {
if (preg_match("/currency='([[:alpha:]]+)'/", $line, $currencyCode)) {
if (preg_match("/rate='([[:graph:]]+)'/", $line, $rate)) {
$this->rates[$currencyCode[1]] = $rate[1];
}
}
}
}
/** The time till Latvian Lats should be displayed.
*
* @return bool Do we need to display Lats or not.
*/
public function lvlDead() {
if (time() > strtotime("06/30/2014 23:59")) {
return true;
}
return false;
}
/** Prepare all currencies for AJAX call
*
* @return string With all currencies
*/
public function currencies() {
$r = join(array_keys($this->rates), ",");
return $r;
}
/** Calculate the value of given data
*
* @param int $ammount Ammount of money you want to exchange
* @param string $from From which currency you want to exchange
* @param string $to To which currency you want to exchange
* @return double With calculated value in format x.xx
*/
public function value($ammount, $from, $to) {
$r['r'] = number_format($this->rates[$to] / $this->rates[$from] * $ammount, 2, ".", "");
return $r;
}
}