1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\CurrencyExchangeBundle\Driver; |
13
|
|
|
|
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This class downloads exchange rates from http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml. |
18
|
|
|
*/ |
19
|
|
|
class EcbDriver implements CurrencyDriverInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var null|Client |
23
|
|
|
*/ |
24
|
|
|
private $httpClient; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $url; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param null|Client $httpClient |
33
|
|
|
* @param string $url |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Client $httpClient, $url) |
36
|
|
|
{ |
37
|
|
|
$this->httpClient = $httpClient; |
38
|
|
|
$this->url = $url; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Downloads raw currency data. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
private function getRawData() |
47
|
|
|
{ |
48
|
|
|
$request = $this->httpClient->get($this->url); |
49
|
|
|
|
50
|
|
|
try { |
51
|
|
|
$xml = simplexml_load_string((string)$request->getBody(), 'SimpleXMLElement'); |
52
|
|
|
} catch (\Exception $e) { |
53
|
|
|
throw new \UnexpectedValueException('Got invalid response'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $xml; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function getRates($date = null) |
63
|
|
|
{ |
64
|
|
|
if ($date) { |
|
|
|
|
65
|
|
|
throw new \UnexpectedValueException('ECB driver does not support currency history at the moment.'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$rates = []; |
69
|
|
|
$response = $this->getRawData(); |
70
|
|
|
|
71
|
|
|
$valid = isset($response) && isset($response->Cube->Cube->Cube); |
72
|
|
|
if (!$valid) { |
73
|
|
|
throw new \UnexpectedValueException('Got invalid response'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$data = $response->xpath('//gesmes:Envelope/*[3]/*'); |
77
|
|
|
foreach ($data[0]->children() as $child) { |
78
|
|
|
$code = (string)$child->attributes()->currency; |
79
|
|
|
$rate = (float)$child->attributes()->rate; |
80
|
|
|
$rates[$code] = $rate; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $rates; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function getBaseCurrency() |
90
|
|
|
{ |
91
|
|
|
// Default base currency of The European Central Bank |
92
|
|
|
return 'EUR'; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: