|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\CBARCurrency; |
|
4
|
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
|
6
|
|
|
use Orkhanahmadov\CBARCurrency\Exceptions\DateException; |
|
7
|
|
|
use Orkhanahmadov\CBARCurrency\Exceptions\CurrencyException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @property float|int USD |
|
11
|
|
|
* @property float|int EUR |
|
12
|
|
|
* @property float|int AUD |
|
13
|
|
|
* @property float|int ARS |
|
14
|
|
|
* @property float|int BYN |
|
15
|
|
|
* @property float|int BRL |
|
16
|
|
|
* @property float|int AED |
|
17
|
|
|
* @property float|int ZAR |
|
18
|
|
|
* @property float|int KRW |
|
19
|
|
|
* @property float|int CZK |
|
20
|
|
|
* @property float|int CLP |
|
21
|
|
|
* @property float|int CNY |
|
22
|
|
|
* @property float|int DKK |
|
23
|
|
|
* @property float|int GEL |
|
24
|
|
|
* @property float|int HKD |
|
25
|
|
|
* @property float|int INR |
|
26
|
|
|
* @property float|int GBP |
|
27
|
|
|
* @property float|int IDR |
|
28
|
|
|
* @property float|int IRR |
|
29
|
|
|
* @property float|int SEK |
|
30
|
|
|
* @property float|int CHF |
|
31
|
|
|
* @property float|int ILS |
|
32
|
|
|
* @property float|int CAD |
|
33
|
|
|
* @property float|int KWD |
|
34
|
|
|
* @property float|int KZT |
|
35
|
|
|
* @property float|int KGS |
|
36
|
|
|
* @property float|int LBP |
|
37
|
|
|
* @property float|int MYR |
|
38
|
|
|
* @property float|int MXN |
|
39
|
|
|
* @property float|int MDL |
|
40
|
|
|
* @property float|int EGP |
|
41
|
|
|
* @property float|int NOK |
|
42
|
|
|
* @property float|int UZS |
|
43
|
|
|
* @property float|int PLN |
|
44
|
|
|
* @property float|int RUB |
|
45
|
|
|
* @property float|int SGD |
|
46
|
|
|
* @property float|int SAR |
|
47
|
|
|
* @property float|int SDR |
|
48
|
|
|
* @property float|int TRY |
|
49
|
|
|
* @property float|int TWD |
|
50
|
|
|
* @property float|int TJS |
|
51
|
|
|
* @property float|int TMT |
|
52
|
|
|
* @property float|int UAH |
|
53
|
|
|
* @property float|int JPY |
|
54
|
|
|
* @property float|int NZD |
|
55
|
|
|
* |
|
56
|
|
|
* @method USD(int|float $int) |
|
57
|
|
|
* @method EUR(int|float $int) |
|
58
|
|
|
* @method AUD(int|float $int) |
|
59
|
|
|
* @method ARS(int|float $int) |
|
60
|
|
|
* @method BYN(int|float $int) |
|
61
|
|
|
* @method BRL(int|float $int) |
|
62
|
|
|
* @method AED(int|float $int) |
|
63
|
|
|
* @method ZAR(int|float $int) |
|
64
|
|
|
* @method KRW(int|float $int) |
|
65
|
|
|
* @method CZK(int|float $int) |
|
66
|
|
|
* @method CLP(int|float $int) |
|
67
|
|
|
* @method CNY(int|float $int) |
|
68
|
|
|
* @method DKK(int|float $int) |
|
69
|
|
|
* @method GEL(int|float $int) |
|
70
|
|
|
* @method HKD(int|float $int) |
|
71
|
|
|
* @method INR(int|float $int) |
|
72
|
|
|
* @method GBP(int|float $int) |
|
73
|
|
|
* @method IDR(int|float $int) |
|
74
|
|
|
* @method IRR(int|float $int) |
|
75
|
|
|
* @method SEK(int|float $int) |
|
76
|
|
|
* @method CHF(int|float $int) |
|
77
|
|
|
* @method ILS(int|float $int) |
|
78
|
|
|
* @method CAD(int|float $int) |
|
79
|
|
|
* @method KWD(int|float $int) |
|
80
|
|
|
* @method KZT(int|float $int) |
|
81
|
|
|
* @method KGS(int|float $int) |
|
82
|
|
|
* @method LBP(int|float $int) |
|
83
|
|
|
* @method MYR(int|float $int) |
|
84
|
|
|
* @method MXN(int|float $int) |
|
85
|
|
|
* @method MDL(int|float $int) |
|
86
|
|
|
* @method EGP(int|float $int) |
|
87
|
|
|
* @method NOK(int|float $int) |
|
88
|
|
|
* @method UZS(int|float $int) |
|
89
|
|
|
* @method PLN(int|float $int) |
|
90
|
|
|
* @method RUB(int|float $int) |
|
91
|
|
|
* @method SGD(int|float $int) |
|
92
|
|
|
* @method SAR(int|float $int) |
|
93
|
|
|
* @method SDR(int|float $int) |
|
94
|
|
|
* @method TRY(int|float $int) |
|
95
|
|
|
* @method TWD(int|float $int) |
|
96
|
|
|
* @method TJS(int|float $int) |
|
97
|
|
|
* @method TMT(int|float $int) |
|
98
|
|
|
* @method UAH(int|float $int) |
|
99
|
|
|
* @method JPY(int|float $int) |
|
100
|
|
|
* @method NZD(int|float $int) |
|
101
|
|
|
*/ |
|
102
|
|
|
class CBAR |
|
103
|
|
|
{ |
|
104
|
|
|
/** |
|
105
|
|
|
* @var string|null |
|
106
|
|
|
*/ |
|
107
|
|
|
private $date; |
|
108
|
|
|
/** |
|
109
|
|
|
* @var Client |
|
110
|
|
|
*/ |
|
111
|
|
|
private $client; |
|
112
|
|
|
/** |
|
113
|
|
|
* @var array |
|
114
|
|
|
*/ |
|
115
|
|
|
private $rates = []; |
|
116
|
|
|
/** |
|
117
|
|
|
* @var float|int|null |
|
118
|
|
|
*/ |
|
119
|
|
|
private $aznAmount = null; |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Parser constructor. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string|null $date |
|
125
|
|
|
*/ |
|
126
|
|
|
public function __construct(?string $date = null) |
|
127
|
|
|
{ |
|
128
|
|
|
$this->client = new Client(); |
|
129
|
|
|
$this->date = $date ?: date('d.m.Y'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Sets currency rate date. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $date |
|
136
|
|
|
* |
|
137
|
|
|
* @throws DateException |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function for(string $date) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->date = $date; |
|
144
|
|
|
|
|
145
|
|
|
if (! isset($this->rates[$this->date])) { |
|
146
|
|
|
$this->getRatesFromCBAR(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Gets currency rate. |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $currency |
|
156
|
|
|
* |
|
157
|
|
|
* @throws DateException |
|
158
|
|
|
* @throws CurrencyException |
|
159
|
|
|
* |
|
160
|
|
|
* @return mixed |
|
161
|
|
|
*/ |
|
162
|
|
|
public function __get(string $currency) |
|
163
|
|
|
{ |
|
164
|
|
|
if (! isset($this->rates[$this->date])) { |
|
165
|
|
|
$this->getRatesFromCBAR(); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if (! isset($this->rates[$this->date][$currency])) { |
|
169
|
|
|
throw new CurrencyException('Currency with ' . $currency . ' code is not available'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if ($this->aznAmount) { |
|
173
|
|
|
$conversion = bcdiv($this->aznAmount, $this->rates[$this->date][$currency]['rate'], 4); |
|
174
|
|
|
$this->aznAmount = null; |
|
175
|
|
|
|
|
176
|
|
|
return $conversion; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
return bcdiv($this->rates[$this->date][$currency]['rate'], $this->rates[$this->date][$currency]['nominal'], 4); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Converts currency with given amount. |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $currency |
|
186
|
|
|
* @param array $arguments |
|
187
|
|
|
* |
|
188
|
|
|
* @throws DateException |
|
189
|
|
|
* @throws CurrencyException |
|
190
|
|
|
* |
|
191
|
|
|
* @return float|int |
|
192
|
|
|
*/ |
|
193
|
|
|
public function __call(string $currency, array $arguments) |
|
194
|
|
|
{ |
|
195
|
|
|
if (! isset($this->rates[$this->date])) { |
|
196
|
|
|
$this->getRatesFromCBAR(); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (! isset($this->rates[$this->date][$currency])) { |
|
200
|
|
|
throw new CurrencyException('Currency with ' . $currency . ' code is not available'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
return $this->$currency * ($arguments[0] ?? 1); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Converts AZN amount to other currency. |
|
208
|
|
|
* |
|
209
|
|
|
* @param float|int $amount |
|
210
|
|
|
* |
|
211
|
|
|
* @return CBAR |
|
212
|
|
|
*/ |
|
213
|
|
|
public function AZN($amount = 1) |
|
214
|
|
|
{ |
|
215
|
|
|
$this->aznAmount = $amount; |
|
216
|
|
|
|
|
217
|
|
|
return $this; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Fetches currency rates from CBAR with given date. |
|
222
|
|
|
* |
|
223
|
|
|
* @throws DateException |
|
224
|
|
|
*/ |
|
225
|
|
|
private function getRatesFromCBAR() |
|
226
|
|
|
{ |
|
227
|
|
|
if (! $validatedDate = strtotime($this->date)) { |
|
|
|
|
|
|
228
|
|
|
throw new DateException($this->date . ' is not a valid date.'); |
|
229
|
|
|
} |
|
230
|
|
|
$this->date = date('d.m.Y', $validatedDate); |
|
231
|
|
|
|
|
232
|
|
|
$response = $this->client->get('https://www.cbar.az/currencies/' . $this->date . '.xml'); |
|
233
|
|
|
|
|
234
|
|
|
$xml = simplexml_load_string($response->getBody()->getContents()); |
|
235
|
|
|
|
|
236
|
|
|
foreach ($xml->ValType[1]->Valute as $currency) { |
|
237
|
|
|
$this->rates[$this->date][(string) $currency->attributes()['Code']] = [ |
|
|
|
|
|
|
238
|
|
|
'rate' => (float) $currency->Value, |
|
239
|
|
|
'nominal' => (int) $currency->Nominal, |
|
240
|
|
|
'name' => (string) $currency->Name, |
|
241
|
|
|
]; |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param Client $client |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setClient(Client $client): void |
|
249
|
|
|
{ |
|
250
|
|
|
$this->client = $client; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param array $rates |
|
255
|
|
|
* |
|
256
|
|
|
* @return CBAR |
|
257
|
|
|
*/ |
|
258
|
|
|
public function setRates(array $rates): self |
|
259
|
|
|
{ |
|
260
|
|
|
$this->rates = $rates; |
|
261
|
|
|
|
|
262
|
|
|
return $this; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @return array |
|
267
|
|
|
*/ |
|
268
|
|
|
public function getRates(): array |
|
269
|
|
|
{ |
|
270
|
|
|
return $this->rates; |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
|