This repository has been archived by the owner on Dec 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Currency.php
227 lines (195 loc) · 4.18 KB
/
Currency.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
<?php
/**
* based on http://openexchangerates.org/
*/
class EtuDev_Util_Currency
{
/**
* @var string
*/
protected $api_key = null;
/**
* @var int
*/
protected $seconds_ttl = 3600;
/**
* @var array
*/
protected $currencies = array();
/**
* @var array
*/
protected $rates = array();
/**
* @var int
*/
protected $timestamp = null;
/**
* @var bool
*/
protected $lookup_for_rates_if_needed = true;
/**
* @param string $api_key
* @param int $seconds_ttl
*/
public function __construct($api_key, $seconds_ttl = 3600)
{
$this->api_key = $api_key;
$this->seconds_ttl = $seconds_ttl;
}
/**
* @return $this
*/
public function disableLookupForRates()
{
$this->lookup_for_rates_if_needed = false;
return $this;
}
/**
* @return $this
*/
public function enableLookupForRates()
{
$this->lookup_for_rates_if_needed = true;
return $this;
}
/**
* check if there are no rates or if the timestamp is more than $seconds_ttl seconds in the past
*
* @return bool
*/
public function isRatesTooOld()
{
return (!$this->rates || ($this->timestamp + $this->seconds_ttl) < time());
}
/**
* @return EtuDev_Util_Currency
*/
public function retrieveCurrenciesIfNotSet()
{
if (!$this->currencies) {
$this->loadCurrencies();
}
return $this;
}
/**
* @return bool
*/
public function needRetrieveRates()
{
return $this->lookup_for_rates_if_needed && (!$this->rates || $this->isRatesTooOld());
}
/**
* @return $this
*/
public function loadRates()
{
$this->load();
return $this;
}
/**
* if there are no rates or if they are too old, load()
*
* @return EtuDev_Util_Currency
*/
public function retrieveRatesIfNeeded()
{
if ($this->needRetrieveRates()) {
$this->load();
}
return $this;
}
/**
* public setter if it is already cached
*
* @param array $currencies associative array
*
* @return EtuDev_Util_Currency
*/
public function setCurrencies($currencies)
{
$this->currencies = (array) $currencies;
return $this;
}
/**
* public setter if it is already cached
*
* @param array $rates
*
* @return EtuDev_Util_Currency
*/
public function setRates($rates)
{
$this->rates = $rates;
return $this;
}
/**
* public setter if it is already cached
*
* @param int $timestamp
*
* @return EtuDev_Util_Currency
*/
public function setTimestamp($timestamp)
{
$this->timestamp = $timestamp;
return $this;
}
protected function loadCurrencies()
{
$this->currencies = (array) $this->doRequest('currencies.json');
}
protected function load()
{
$changes = $this->doRequest('latest.json');
if ($changes) {
$this->rates = $changes['rates'];
$this->timestamp = (int) $changes['timestamp'];
}
}
protected function doRequest($file)
{
$appId = $this->api_key;
$ch = curl_init("http://openexchangerates.org/api/{$file}?app_id={$appId}");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
$json = curl_exec($ch);
curl_close($ch);
return json_decode($json, true);
}
/**
* @return string
*/
public function getApiKey()
{
return $this->api_key;
}
/**
* @return array
*/
public function getCurrencies()
{
return $this->currencies;
}
/**
* @return array
*/
public function getRates()
{
return $this->rates;
}
/**
* @return int
*/
public function getSecondsTtl()
{
return $this->seconds_ttl;
}
/**
* @return int
*/
public function getTimestamp()
{
return $this->timestamp;
}
}