1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\Currencylayer; |
4
|
|
|
|
5
|
|
|
use Carbon\CarbonImmutable; |
6
|
|
|
use DateTimeImmutable; |
7
|
|
|
use GuzzleHttp\Client as Guzzle; |
8
|
|
|
|
9
|
|
|
class CurrencylayerClient implements Client |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Guzzle |
13
|
|
|
*/ |
14
|
|
|
private $client; |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $accessKey; |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $source; |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $currencies; |
27
|
|
|
/** |
28
|
|
|
* @var DateTimeImmutable |
29
|
|
|
*/ |
30
|
|
|
private $date; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* CurrencylayerClient constructor. |
34
|
|
|
* |
35
|
|
|
* @param string $accessKey |
36
|
|
|
* @param bool $useHttps |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string $accessKey, bool $useHttps = false) |
39
|
|
|
{ |
40
|
|
|
$this->client = new Guzzle([ |
41
|
|
|
'base_uri' => $useHttps ? 'https://apilayer.net/api/' : 'http://apilayer.net/api/' |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
$this->accessKey = $accessKey; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $source |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function source(string $source): Client |
53
|
|
|
{ |
54
|
|
|
$this->source = $source; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array<string>|string $currencies |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function currencies($currencies): Client |
65
|
|
|
{ |
66
|
|
|
if (is_array($currencies)) { |
67
|
|
|
$this->currencies = implode(',', $currencies); |
68
|
|
|
} else { |
69
|
|
|
$this->currencies = $currencies; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param DateTimeImmutable|string $date |
77
|
|
|
* |
78
|
|
|
* @return $this |
79
|
|
|
* |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
|
|
public function date($date): Client |
83
|
|
|
{ |
84
|
|
|
$this->date = $date instanceof DateTimeImmutable ? $date : new CarbonImmutable($date); |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return Currency |
91
|
|
|
* |
92
|
|
|
* @throws \Exception |
93
|
|
|
*/ |
94
|
|
|
public function live(): Currency |
95
|
|
|
{ |
96
|
|
|
$data = $this->request('live', [ |
97
|
|
|
'currencies' => $this->currencies, |
98
|
|
|
'source' => $this->source, |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
return new Currency($data); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return Currency |
106
|
|
|
* |
107
|
|
|
* @throws \Exception |
108
|
|
|
*/ |
109
|
|
|
public function historical(): Currency |
110
|
|
|
{ |
111
|
|
|
$data = $this->request('historical', [ |
112
|
|
|
'date' => $this->date->format('Y-m-d'), |
113
|
|
|
'currencies' => $this->currencies, |
114
|
|
|
'source' => $this->source, |
115
|
|
|
]); |
116
|
|
|
|
117
|
|
|
return new Currency($data); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return Currency |
122
|
|
|
* |
123
|
|
|
* @throws \Exception |
124
|
|
|
*/ |
125
|
|
|
public function quotes(): Currency |
126
|
|
|
{ |
127
|
|
|
$query = [ |
128
|
|
|
'currencies' => $this->currencies, |
129
|
|
|
'source' => $this->source, |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
if ($this->date) { |
133
|
|
|
$query['date'] = $this->date->format('Y-m-d'); |
134
|
|
|
|
135
|
|
|
return new Currency($this->request('historical', $query)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return new Currency($this->request('live', $query)); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param int|float $amount |
143
|
|
|
* |
144
|
|
|
* @return Conversion |
145
|
|
|
* |
146
|
|
|
* @throws \Exception |
147
|
|
|
*/ |
148
|
|
|
public function convert($amount): Conversion |
149
|
|
|
{ |
150
|
|
|
$query = [ |
151
|
|
|
'from' => $this->source, |
152
|
|
|
'to' => $this->currencies, |
153
|
|
|
'amount' => $amount, |
154
|
|
|
]; |
155
|
|
|
|
156
|
|
|
if ($this->date) { |
157
|
|
|
$query['date'] = $this->date->format('Y-m-d'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return new Conversion($this->request('convert', $query)); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* |
165
|
|
|
* |
166
|
|
|
* @param string $endpoint |
167
|
|
|
* @param array $query |
168
|
|
|
* |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
|
private function request(string $endpoint, array $query): array |
172
|
|
|
{ |
173
|
|
|
$response = $this->client->get($endpoint, [ |
174
|
|
|
'query' => array_merge($query, ['access_key' => $this->accessKey]), |
175
|
|
|
]); |
176
|
|
|
|
177
|
|
|
$data = \GuzzleHttp\json_decode($response->getBody()->getContents(), true); |
178
|
|
|
|
179
|
|
|
if (! $data['success']) { |
180
|
|
|
throw new \InvalidArgumentException($data['error']['info']); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
return $data; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param Guzzle $client |
188
|
|
|
*/ |
189
|
|
|
public function setClient(Guzzle $client): void |
190
|
|
|
{ |
191
|
|
|
$this->client = $client; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|