|
1
|
|
|
<?php namespace Arcanedev\Currencies\Converters; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\Currencies\Entities\RateCollection; |
|
4
|
|
|
use Illuminate\Support\Arr; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class OpenExchangeRatesConverter |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanedev\Currencies\Converters |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class OpenExchangeRatesConverter extends AbstractConverter |
|
13
|
|
|
{ |
|
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
15
|
|
|
| Properties |
|
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
17
|
|
|
*/ |
|
18
|
|
|
/** |
|
19
|
|
|
* The base URL. |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $baseUrl = 'http://openexchangerates.org/api'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The API ID. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $apiId; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var bool |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $proPlan; |
|
36
|
|
|
|
|
37
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
38
|
|
|
| Getters & Setters |
|
39
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
40
|
|
|
*/ |
|
41
|
|
|
/** |
|
42
|
|
|
* Set the configs. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $configs |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
24 |
|
protected function setProviderConfigs(array $configs) |
|
49
|
|
|
{ |
|
50
|
24 |
|
$this->apiId = Arr::get($configs, 'api-id'); |
|
51
|
24 |
|
$this->proPlan = Arr::get($configs, 'pro-plan', false); |
|
52
|
24 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Get the API ID. |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
* |
|
59
|
|
|
* @throws \Exception |
|
60
|
|
|
*/ |
|
61
|
12 |
|
private function getAppId() |
|
62
|
|
|
{ |
|
63
|
12 |
|
if ( ! $this->apiId) { |
|
64
|
|
|
throw new \Exception('OpenExchangeRates.org requires an app key.'); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
12 |
|
return $this->apiId; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
12 |
|
private function getFromCurrency($from) |
|
71
|
|
|
{ |
|
72
|
12 |
|
if (is_null($from)) { |
|
73
|
12 |
|
$from = $this->getDefaultCurrency(); |
|
74
|
9 |
|
} |
|
75
|
|
|
|
|
76
|
12 |
|
return $from; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
12 |
|
private function getToCurrencies($to) |
|
80
|
|
|
{ |
|
81
|
12 |
|
if (is_null($to)) { |
|
82
|
12 |
|
return array_diff( |
|
83
|
12 |
|
$this->getSupportedCurrencies(), |
|
84
|
12 |
|
[$this->getDefaultCurrency()] |
|
85
|
9 |
|
); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $to; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
92
|
|
|
| Main Functions |
|
93
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
94
|
|
|
*/ |
|
95
|
|
|
/** |
|
96
|
|
|
* Get currencies rates. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string|null $from |
|
99
|
|
|
* @param array|string|null $to |
|
100
|
|
|
* |
|
101
|
|
|
* @return RateCollection |
|
102
|
|
|
*/ |
|
103
|
12 |
|
public function rates($from = null, $to = null) |
|
104
|
|
|
{ |
|
105
|
12 |
|
$from = $this->getFromCurrency($from); |
|
106
|
12 |
|
$to = $this->getToCurrencies($to); |
|
107
|
|
|
|
|
108
|
12 |
|
$self = $this; |
|
109
|
12 |
|
$callback = function () use ($self, $from, $to) { |
|
110
|
12 |
|
return $self->request($from, $to); |
|
111
|
12 |
|
}; |
|
112
|
|
|
|
|
113
|
12 |
|
return $this->isCacheEnabled() |
|
114
|
9 |
|
? $this->cache->remember($this->cacheKey, $this->cacheDuration, $callback) |
|
115
|
12 |
|
: call_user_func($callback); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Make an API request. |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $from |
|
122
|
|
|
* @param array $to |
|
123
|
|
|
* |
|
124
|
|
|
* @return RateCollection |
|
125
|
|
|
* |
|
126
|
|
|
* @throws \Exception |
|
127
|
|
|
*/ |
|
128
|
12 |
|
protected function request($from, array $to) |
|
129
|
|
|
{ |
|
130
|
12 |
|
$params = array_filter([ |
|
131
|
12 |
|
'app_id' => $this->getAppId(), |
|
132
|
12 |
|
'base' => $from, |
|
133
|
|
|
// TODO: Add a config parameter to limit results to specific currencies (Pro Plan) |
|
134
|
12 |
|
'symbols' => $this->proPlan ? implode(',', $to) : null, |
|
135
|
9 |
|
]); |
|
136
|
|
|
|
|
137
|
12 |
|
$response = $this->client->get($this->getBaseUrl() . "/latest.json?".http_build_query($params)); |
|
138
|
12 |
|
$data = json_decode($response, true); |
|
139
|
|
|
|
|
140
|
12 |
|
return $this->prepareRates($from, $data['rates']); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Prepare rates collection. |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $from |
|
147
|
|
|
* @param array $rates |
|
148
|
|
|
* |
|
149
|
|
|
* @return RateCollection |
|
150
|
|
|
*/ |
|
151
|
12 |
|
protected function prepareRates($from, array $rates) |
|
152
|
|
|
{ |
|
153
|
12 |
|
return RateCollection::make()->load($from, $rates); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|