1
|
|
|
<?php |
2
|
|
|
namespace CurrencyConverter\View\Helper; |
3
|
|
|
|
4
|
|
|
use Cake\View\Helper; |
5
|
|
|
use Cake\ORM\TableRegistry; |
6
|
|
|
use Cake\I18n\Time; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property \Cake\View\Helper\HtmlHelper $Html |
10
|
|
|
* @property \Cake\View\Helper\FormHelper $Form |
11
|
|
|
* @property \Cake\View\Helper\NumberHelper $Number |
12
|
|
|
*/ |
13
|
|
|
class CurrencyConverterHelper extends Helper |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Using database |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
public $database; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Time interval for refreshing database |
24
|
|
|
* |
25
|
|
|
* @var int |
26
|
|
|
*/ |
27
|
|
|
public $refresh; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Number of decimal to use for formatting converted price |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
public $decimal; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* CurrencyratesTable Class |
38
|
|
|
* @var \Cake\ORM\Table |
39
|
|
|
*/ |
40
|
|
|
private $_currencyratesTable; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Default settings |
44
|
|
|
* |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
protected $_defaultConfig = [ |
48
|
|
|
'database' => 2, // Use database to store rate |
49
|
|
|
'refresh' => 24, // Time interval for refreshing rate in database |
50
|
|
|
'decimal' => 2, // Number of decimal to use for convert number |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param array $config |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
|
public function initialize(array $config = []) { |
58
|
|
|
|
59
|
|
|
$config = $this->getConfig(); |
60
|
|
|
|
61
|
|
|
$this->database = $config['database']; |
62
|
|
|
$this->refresh = $config['refresh']; |
63
|
|
|
$this->decimal = $config['decimal']; |
64
|
|
|
|
65
|
|
|
$this->_currencyratesTable = TableRegistry::get('CurrencyConverter.Currencyrates'); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Convert method take an amount as first parameter and convert it using $from currency and $to currency. |
70
|
|
|
* |
71
|
|
|
* @param float|string $amount the amount to convert. |
72
|
|
|
* @param string $from currency to convert from |
73
|
|
|
* @param string $to currency to convert to |
74
|
|
|
* @return float $amount converted |
75
|
|
|
*/ |
76
|
|
|
public function convert($amount, $from, $to) |
77
|
|
|
{ |
78
|
|
|
$amount = floatval($amount); |
79
|
|
|
$rate = $this->getRateToUse($from, $to); |
80
|
|
|
|
81
|
|
|
return $convert = $this->_formatConvert($rate * $amount); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Rate method return the rate of two currencies |
86
|
|
|
* |
87
|
|
|
* @param string $from currency to get the rate from |
88
|
|
|
* @param string $to currency to get the rate to |
89
|
|
|
* @return float|null $rate |
90
|
|
|
*/ |
91
|
|
|
public function rate($from, $to) |
92
|
|
|
{ |
93
|
|
|
return $this->getRateToUse($from, $to); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* getRateToUse return rate to use |
98
|
|
|
* Using $from and $to parameters representing currency to deal with and the configuration settings |
99
|
|
|
* This method save or update currencyrates Table if necesseray too. |
100
|
|
|
* |
101
|
|
|
* @param string $from currency to get the rate from |
102
|
|
|
* @param string $to currency to get the rate to |
103
|
|
|
* @return float|null $rate |
104
|
|
|
*/ |
105
|
|
|
public function getRateToUse($from, $to) |
106
|
|
|
{ |
107
|
|
|
if ($from == $to) { |
108
|
|
|
return 1; |
109
|
|
|
} else { |
110
|
|
|
if ($this->database) { |
111
|
|
|
// Get a currency rate from table |
112
|
|
|
$result = $this->_currencyratesTable->find('all')->where(['from_currency' => $from, 'to_currency' => $to])->first(); |
113
|
|
|
if ($result) { |
114
|
|
|
// If currency rate is in table and it doesn't have to be updated |
115
|
|
|
if ($result->get('modified')->wasWithinLast($this->refresh . ' hours')) { |
116
|
|
|
return $rate = $result->get('rate'); |
|
|
|
|
117
|
|
|
} else { |
118
|
|
|
$rate = $this->_getRateFromAPI($from, $to); |
|
|
|
|
119
|
|
|
if ($rate) { |
|
|
|
|
120
|
|
|
$result->rate = $rate; |
121
|
|
|
$this->_currencyratesTable->save($result); |
122
|
|
|
} |
123
|
|
|
return $rate; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
// If currency rate isn't in table |
127
|
|
|
if (!$result) { |
128
|
|
|
$rate = $this->_getRateFromAPI($from, $to); |
|
|
|
|
129
|
|
|
if ($rate) { |
|
|
|
|
130
|
|
|
$entity = $this->_currencyratesTable->newEntity([ |
131
|
|
|
'from_currency' => $from, |
132
|
|
|
'to_currency' => $to, |
133
|
|
|
'rate' => $rate |
134
|
|
|
]); |
135
|
|
|
$this->_currencyratesTable->save($entity); |
136
|
|
|
} |
137
|
|
|
return $rate; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->_getRateFromAPI($from, $to); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Format number using configuration |
147
|
|
|
* |
148
|
|
|
* @param float price to format |
|
|
|
|
149
|
|
|
* @return float |
150
|
|
|
*/ |
151
|
|
|
private function _formatConvert($number) |
152
|
|
|
{ |
153
|
|
|
return floatval(number_format($number, $this->decimal)); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Call free.currencyconverterapi.com API to get a rate for one currency to an other one currency |
158
|
|
|
* |
159
|
|
|
* @param string $from the currency |
160
|
|
|
* @param string $to the currency |
161
|
|
|
* @return int|null $rate |
162
|
|
|
*/ |
163
|
|
|
private function _getRateFromAPI($from, $to) |
164
|
|
|
{ |
165
|
|
|
$rate = null; |
166
|
|
|
|
167
|
|
|
$url = 'https://free.currencyconverterapi.com/api/v5/convert?q=' . $from . '_' . $to . '&compact=ultra'; |
168
|
|
|
$request = @fopen($url, 'r'); |
169
|
|
|
|
170
|
|
|
if ($request) { |
|
|
|
|
171
|
|
|
$response = fgets($request, 4096); |
172
|
|
|
fclose($request); |
173
|
|
|
|
174
|
|
|
$response = json_decode($response, true); |
175
|
|
|
if (isset($response[$from . '_' . $to])) { |
176
|
|
|
$rate = $response[$from . '_' . $to]; |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
return $rate; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.