Completed
Push — master ( c7b0fa...c03ff4 )
by Orkhan
02:18
created

CBAR::setDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Orkhanahmadov\CBARCurrency;
4
5
use GuzzleHttp\Client;
6
use Orkhanahmadov\CBARCurrency\Exceptions\CurrencyException;
7
use Orkhanahmadov\CBARCurrency\Exceptions\DateException;
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
     * @return $this
138
     * @throws DateException
139
     */
140
    public function for(string $date)
141
    {
142
        $this->date = $date;
143
144
        if (!isset($this->rates[$this->date])) {
145
            $this->getRatesFromCBAR();
146
        }
147
148
        return $this;
149
    }
150
151
    /**
152
     * Gets currency rate.
153
     *
154
     * @param string $currency
155
     *
156
     * @return mixed
157
     * @throws DateException
158
     * @throws CurrencyException
159
     */
160
    public function __get(string $currency)
161
    {
162
        if (!isset($this->rates[$this->date])) {
163
            $this->getRatesFromCBAR();
164
        }
165
166
        if (!isset($this->rates[$this->date][$currency])) {
167
            throw new CurrencyException('Currency with '.$currency.' code is not available');
168
        }
169
170
        if ($this->aznAmount) {
171
            $conversion = bcdiv($this->aznAmount, $this->rates[$this->date][$currency]['rate'], 4);
172
            $this->aznAmount = null;
173
174
            return $conversion;
175
        }
176
177
        return bcdiv($this->rates[$this->date][$currency]['rate'], $this->rates[$this->date][$currency]['nominal'], 4);
178
    }
179
180
    /**
181
     * Converts currency with given amount.
182
     *
183
     * @param string $currency
184
     * @param array $arguments
185
     *
186
     * @return float|int
187
     * @throws DateException
188
     * @throws CurrencyException
189
     */
190
    public function __call(string $currency, array $arguments)
191
    {
192
        if (!isset($this->rates[$this->date])) {
193
            $this->getRatesFromCBAR();
194
        }
195
196
        if (!isset($this->rates[$this->date][$currency])) {
197
            throw new CurrencyException('Currency with '.$currency.' code is not available');
198
        }
199
200
        return $this->$currency * $arguments[0];
201
    }
202
203
    /**
204
     * Converts AZN amount to other currency.
205
     *
206
     * @param float|int $amount
207
     *
208
     * @return CBAR
209
     */
210
    public function AZN($amount = 1)
211
    {
212
        $this->aznAmount = $amount;
213
214
        return $this;
215
    }
216
217
    /**
218
     * Fetches currency rates from CBAR with given date.
219
     * @throws DateException
220
     */
221
    private function getRatesFromCBAR()
222
    {
223
        if (!$validatedDate = strtotime($this->date)) {
224
            throw new DateException($this->date.' is not a valid date.');
225
        }
226
        $this->date = date('d.m.Y', $validatedDate);
227
228
        $response = $this->client->get('https://www.cbar.az/currencies/'.$this->date.'.xml');
229
230
        $xml = simplexml_load_string($response->getBody()->getContents());
231
232
        foreach ($xml->ValType[1]->Valute as $currency) {
233
            $this->rates[$this->date][(string) $currency->attributes()['Code']] = [
234
                'rate'    => (float) $currency->Value,
235
                'nominal' => (int) $currency->Nominal,
236
            ];
237
        }
238
    }
239
240
    /**
241
     * @param Client $client
242
     */
243
    public function setClient(Client $client): void
244
    {
245
        $this->client = $client;
246
    }
247
248
    /**
249
     * @param array $rates
250
     *
251
     * @return CBAR
252
     */
253
    public function setRates(array $rates): self
254
    {
255
        $this->rates = $rates;
256
257
        return $this;
258
    }
259
260
    /**
261
     * @return array
262
     */
263
    public function getRates(): array
264
    {
265
        return $this->rates;
266
    }
267
}
268