-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFastForex.php
More file actions
207 lines (170 loc) · 6.02 KB
/
FastForex.php
File metadata and controls
207 lines (170 loc) · 6.02 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?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 FastForex extends BaseCurrencyDriver
{
protected string $apiURL = 'api.fastforex.io';
protected string $baseCurrency = 'USD';
#[Override]
public function accessKey(string $accessKey): static
{
return $this->config('api_key', $accessKey);
}
#[Override]
public function get(string|Currency|array $forCurrency = []): ConversionResult
{
if ($forCurrency !== []) {
$this->currencies($forCurrency);
}
$endpoint = match (count($this->getSymbols())) {
0 => 'fetch-all',
1 => 'fetch-one',
default => 'fetch-multi',
};
$response = $this->apiRequest($endpoint, $this->buildRateParams());
return new ConversionResult(
$this->optionalResponseString($response, 'base') ?? $this->getBaseCurrency(),
$this->responseDate($response),
$this->ratesFromResponse($response),
);
}
#[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('historical', [
...$this->buildRateParams(),
'date' => $this->getDate(),
]);
return new ConversionResult(
$this->optionalResponseString($response, 'base') ?? $this->getBaseCurrency(),
$this->optionalResponseString($response, 'date') ?? $this->getDate(),
$this->ratesFromResponse($response),
);
}
#[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('convert', [
'from' => $this->getBaseCurrency(),
'to' => $target,
'amount' => $this->amount,
]);
$rates = $this->ratesFromResponse($response);
if (!isset($rates[$target])) {
throw new ApiException("fastFOREX response did not contain a conversion for {$target}.");
}
$rate = BigDecimal::of((string) $rates[$target])
->dividedBy(BigDecimal::of((string) $this->amount), ConversionResult::DEFAULT_SCALE, RoundingMode::HalfUp);
return new ConversionResult(
$this->optionalResponseString($response, 'base') ?? $this->getBaseCurrency(),
$this->responseDate($response),
[$target => $rate],
);
}
/**
* @param array<string, scalar> $params
*
* @return array<array-key, mixed>
*/
#[Override]
protected function apiRequest(string $endpoint, array $params = []): array
{
$response = parent::apiRequest($endpoint, $params);
if (isset($response['error'])) {
throw new ApiException(is_scalar($response['error']) ? (string) $response['error'] : 'fastFOREX API error');
}
return $response;
}
/**
* @return array<string, scalar>
*/
private function buildRateParams(): array
{
$params = [
'from' => $this->getBaseCurrency(),
];
if ($this->getSymbols() !== []) {
$params['to'] = implode(',', $this->getSymbols());
}
return $params;
}
/**
* @param array<array-key, mixed> $response
*
* @return array<string, BigDecimal|float|int|string>
*/
private function ratesFromResponse(array $response): array
{
$rates = $response['result'] ?? $response['results'] ?? null;
if (!is_array($rates)) {
throw new ApiException('fastFOREX response did not contain rate data.');
}
$normalised = [];
foreach ($rates as $currency => $rate) {
if (!$rate instanceof BigDecimal && !is_float($rate) && !is_int($rate) && !is_string($rate)) {
throw new ApiException('fastFOREX response did not contain a rate for ' . (string) $currency . '.');
}
$normalised[(string) $currency] = $rate;
}
return $normalised;
}
/**
* @param array<array-key, mixed> $response
*/
private function responseDate(array $response): ?string
{
if (isset($response['date']) && is_scalar($response['date'])) {
return (string) $response['date'];
}
if (isset($response['updated']) && is_string($response['updated']) && $response['updated'] !== '') {
return DateHelper::format(new DateTimeImmutable(strtok($response['updated'], ' ') ?: $response['updated']));
}
return null;
}
}