|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ultraleet\CurrencyRates\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use Ultraleet\CurrencyRates\AbstractProvider; |
|
6
|
|
|
use Ultraleet\CurrencyRates\Result; |
|
7
|
|
|
use Ultraleet\CurrencyRates\Exceptions\ConnectionException; |
|
8
|
|
|
use Ultraleet\CurrencyRates\Exceptions\ResponseException; |
|
9
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
10
|
|
|
use DateTime; |
|
11
|
|
|
use InvalidArgumentException; |
|
12
|
|
|
use GuzzleHttp\Exception\TransferException; |
|
13
|
|
|
|
|
14
|
|
|
class FixerProvider extends AbstractProvider |
|
15
|
|
|
{ |
|
16
|
|
|
protected $guzzle; |
|
17
|
|
|
protected $url = "http://api.fixer.io"; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param \GuzzleHttp\Client $guzzle |
|
23
|
|
|
* @return void |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(GuzzleClient $guzzle) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->guzzle = $guzzle; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get latest currency exchange rates. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $base |
|
34
|
|
|
* @param array $targets |
|
35
|
|
|
* @return \Ultraleet\CurrencyRates\Contracts\Result |
|
36
|
|
|
*/ |
|
37
|
|
|
public function latest($base = 'EUR', $targets = []) |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->query('latest', $base, $targets); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get historical currency exchange rates. |
|
44
|
|
|
* |
|
45
|
|
|
* @param \DateTime $date |
|
46
|
|
|
* @param string $base |
|
47
|
|
|
* @param array $targets |
|
48
|
|
|
* @return \Ultraleet\CurrencyRates\Contracts\Result |
|
49
|
|
|
*/ |
|
50
|
|
|
public function historical($date, $base = 'EUR', $targets = []) |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->query($date->format('Y-m-d'), $base, $targets); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get historical currency exchange rates. |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $date 'latest' for latest, 'Y-m-d' date for historical. |
|
59
|
|
|
* @param string $base |
|
60
|
|
|
* @param array $targets |
|
61
|
|
|
* @return \Ultraleet\CurrencyRates\Contracts\Result |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function query($date, $base, $targets) |
|
64
|
|
|
{ |
|
65
|
|
|
$url = $this->url . '/' . $date; |
|
66
|
|
|
$query = []; |
|
67
|
|
|
|
|
68
|
|
|
// add base to query string |
|
69
|
|
|
if ($base !== 'EUR') { |
|
70
|
|
|
$query[] = 'base=' . $base; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// add symbols to query string |
|
74
|
|
|
if (!empty($targets)) { |
|
75
|
|
|
$query[] = 'symbols=' . implode(',', $targets); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// append query string to url |
|
79
|
|
|
if (!empty($query)) { |
|
80
|
|
|
$url .= '?' . implode('&', $query); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// query the API |
|
84
|
|
|
try { |
|
85
|
|
|
$response = $this->guzzle->request('GET', $url); |
|
86
|
|
|
} catch (TransferException $e) { |
|
87
|
|
|
throw new ConnectionException($e->getMessage()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// process response |
|
91
|
|
|
$response = json_decode($response->getBody(), true); |
|
92
|
|
|
if (isset($response['rates']) && is_array($response['rates']) && |
|
93
|
|
|
isset($response['base']) && isset($response['date'])) { |
|
94
|
|
|
return new Result( |
|
95
|
|
|
$response['base'], |
|
96
|
|
|
new DateTime($response['date']), |
|
97
|
|
|
$response['rates'] |
|
98
|
|
|
); |
|
99
|
|
|
} elseif (isset($response['error'])) { |
|
100
|
|
|
throw new ResponseException($response['error']); |
|
101
|
|
|
} else { |
|
102
|
|
|
throw new ResponseException('Response body is malformed.'); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|