-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCurrencyApi.php
More file actions
185 lines (152 loc) · 5.2 KB
/
CurrencyApi.php
File metadata and controls
185 lines (152 loc) · 5.2 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
<?php
declare(strict_types=1);
namespace Otherguy\Currency\Drivers;
use Brick\Math\BigDecimal;
use Brick\Math\RoundingMode;
use DateTimeImmutable;
use DateTimeInterface;
use Otherguy\Currency\Currency;
use Otherguy\Currency\Exceptions\ApiException;
use Otherguy\Currency\Helpers\DateHelper;
use Otherguy\Currency\Results\ConversionResult;
use Override;
class CurrencyApi extends BaseCurrencyDriver
{
protected string $apiURL = 'api.currencyapi.com';
protected string $baseCurrency = 'USD';
#[Override]
public function accessKey(string $accessKey): static
{
$this->httpHeaders['apikey'] = $accessKey;
return $this;
}
#[Override]
public function get(string|Currency|array $forCurrency = []): ConversionResult
{
if ($forCurrency !== []) {
$this->currencies($forCurrency);
}
$response = $this->apiRequest('v3/latest', $this->buildRateParams());
return new ConversionResult(
$this->getBaseCurrency(),
$this->responseDate($response),
$this->ratesFromData($response['data'] ?? []),
);
}
#[Override]
public function historical(
?DateTimeInterface $date = null,
string|Currency|array $forCurrency = [],
): ConversionResult {
if ($date instanceof DateTimeInterface) {
$this->date($date);
}
if ($forCurrency !== []) {
$this->currencies($forCurrency);
}
if ($this->getDate() === null) {
throw new ApiException('Date needs to be set!');
}
$response = $this->apiRequest('v3/historical', [
...$this->buildRateParams(),
'date' => $this->getDate(),
]);
return new ConversionResult(
$this->getBaseCurrency(),
$this->getDate(),
$this->ratesFromData($response['data'] ?? []),
);
}
#[Override]
public function convert(
?float $amount = null,
string|Currency|null $fromCurrency = null,
string|Currency|null $toCurrency = null,
?DateTimeInterface $date = null,
): ConversionResult {
if ($date instanceof DateTimeInterface) {
$this->date($date);
}
if ($amount !== null) {
$this->amount = $amount;
}
if ($fromCurrency !== null) {
$this->baseCurrency = Currency::code($fromCurrency);
}
if ($toCurrency !== null) {
$this->currencies = [Currency::code($toCurrency)];
}
$target = $this->currencies[0] ?? null;
if ($target === null) {
throw new ApiException('A target currency is required for convert().');
}
if ($this->amount === null) {
throw new ApiException('An amount is required for convert().');
}
if ($this->getDate() !== null) {
return $this->historical();
}
$response = $this->apiRequest('v3/convert', [
'value' => $this->amount,
'base_currency' => $this->getBaseCurrency(),
'currencies' => $target,
]);
$data = $response['data'] ?? [];
$converted = is_array($data) ? ($data['value'] ?? null) : null;
if (!is_scalar($converted)) {
throw new ApiException('CurrencyAPI response did not contain a converted value.');
}
$rate = BigDecimal::of((string) $converted)
->dividedBy(BigDecimal::of((string) $this->amount), ConversionResult::DEFAULT_SCALE, RoundingMode::HalfUp);
return new ConversionResult(
$this->getBaseCurrency(),
$this->responseDate($response),
[$target => $rate],
);
}
/**
* @return array<string, scalar>
*/
private function buildRateParams(): array
{
$params = [
'base_currency' => $this->getBaseCurrency(),
];
if ($this->getSymbols() !== []) {
$params['currencies'] = implode(',', $this->getSymbols());
}
return $params;
}
/**
* @param mixed $data
*
* @return array<string, BigDecimal|float|int|string>
*/
private function ratesFromData(mixed $data): array
{
if (!is_array($data)) {
throw new ApiException('CurrencyAPI response did not contain rate data.');
}
$rates = [];
foreach ($data as $currency => $rateData) {
$value = is_array($rateData) ? ($rateData['value'] ?? null) : null;
if (!$value instanceof BigDecimal && !is_float($value) && !is_int($value) && !is_string($value)) {
throw new ApiException('CurrencyAPI response did not contain a rate for ' . (string) $currency . '.');
}
$rates[(string) $currency] = $value;
}
return $rates;
}
/**
* @param array<array-key, mixed> $response
*/
private function responseDate(array $response): ?string
{
$meta = $response['meta'] ?? [];
$timestamp = is_array($meta) ? ($meta['last_updated_at'] ?? null) : null;
if (!is_string($timestamp) || $timestamp === '') {
return null;
}
return DateHelper::format(new DateTimeImmutable($timestamp));
}
}