-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathECBConverter.php
More file actions
192 lines (164 loc) · 4.7 KB
/
ECBConverter.php
File metadata and controls
192 lines (164 loc) · 4.7 KB
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
<?php
namespace Richardds\ECBAPI;
/**
* Class ECBConverter
* @package Richardds\ECBAPI
*/
class ECBConverter
{
/**
* @var null|Currency[]
*/
private $exchange_data;
/**
* @var string
*/
private $cache_file;
/**
* @var int
*/
private $cache_timeout;
/**
* ECBConverter constructor.
*
* @param null|string $cache_file
* @param int $cache_timeout
*/
public function __construct(string $cache_file = '.ecb_cache', int $cache_timeout = 3600)
{
$this->cache_file = $cache_file;
$this->cache_timeout = $cache_timeout;
}
/**
* @throws ECBException
*/
public function checkFileCache(): void
{
if (!file_exists($this->cache_file) || time() - filemtime($this->cache_file) > $this->cache_timeout) {
$this->reloadExchangeReferences();
file_put_contents($this->cache_file, serialize($this->exchange_data), LOCK_EX);
} elseif (is_null($this->exchange_data)) {
$this->exchange_data = unserialize(file_get_contents($this->cache_file));
}
}
/**
* Converts foreign currency to euro.
*
* @param float $amount
* @param string|array $currency_code
* @param int|null $round
* @return int|array
* @throws ECBException
*/
public function toEuro(float $amount, $currency_code, ?int $round = null)
{
return $this->convert($amount, $currency_code, function ($amount, $rate) use ($round) {
$val = $amount / $rate;
return !is_null($round) ? round($val, $round) : $val;
});
}
/**
* @throws ECBException
*/
private function check(): void
{
if (!empty($this->cache_file)) {
$this->checkFileCache();
} elseif (is_null($this->exchange_data)) {
$this->reloadExchangeReferences();
}
}
/**
* @param bool $asArray
* @return Currency[]|array
* @throws ECBException
*/
public function list(bool $asArray = false): array
{
$this->check();
$references = $this->exchange_data;
if ($asArray) {
$array = [];
foreach ($references as $reference) {
$array[$reference->getCode()] = $reference->getRate();
}
return $array;
}
return $references;
}
/**
* @param float $amount
* @param string|array $currency_code
* @param callable $callback
* @return int|array
* @throws ECBException
*/
public function convert(float $amount, $currency_code, callable $callback)
{
$this->check();
// Selected currencies
if (is_array($currency_code)) {
$results = [];
foreach ($currency_code as $currency_c) {
$results[$currency_c] = $callback($amount, $this->exchange_data[$currency_c]->getRate());
}
return $results;
}
// All currencies
if ($currency_code === '*') {
$results = [];
foreach ($this->exchange_data as $currency) {
$results[$currency->getCode()] = $callback($amount, $currency->getRate());
}
return $results;
}
// Single currency
return $callback($amount, $this->exchange_data[$currency_code]->getRate());
}
/**
* Reloads ECB exchange references.
*
* @throws ECBException
*/
public function reloadExchangeReferences(): void
{
try {
$this->exchange_data = ECB::getExchangeReferences();
} catch (ECBException $e) {
throw new ECBException('Failed to update ESB exchange references',
ECBException::CONVERT_FAILED, $e);
}
}
/**
* Converts euro to foreign currency.
*
* @param float $amount
* @param string|array $currency_code
* @param int|null $precision
* @return int|array
* @throws ECBException
*/
public function toForeign(float $amount, string $currency_code, ?int $precision = null)
{
return $this->convert($amount, $currency_code, function ($amount, $rate) use ($precision) {
$val = $amount * $rate;
return !is_null($precision) ? round($val, $precision) : $val;
});
}
public function getCacheFile(): string
{
return $this->cache_file;
}
public function setCacheFile(string $cache_file): void
{
$this->cache_file = $cache_file;
}
public function getCacheTimeout(): int
{
return $this->cache_timeout;
}
public function setCacheTimeout(int $cache_timeout): void
{
$this->cache_timeout = $cache_timeout;
}
}