1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SteffenBrand\CurrCurr\Mapper; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use SimpleXMLElement; |
9
|
|
|
use SteffenBrand\CurrCurr\Exception\ExchangeRatesMappingFailedException; |
10
|
|
|
use SteffenBrand\CurrCurr\Model\ExchangeRate; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ExchangeRatesMapper |
14
|
|
|
* @package SteffenBrand\CurrCurr\Mapper |
15
|
|
|
*/ |
16
|
|
|
class ExchangeRatesMapper implements MapperInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Map exchange rates response. |
20
|
|
|
* |
21
|
|
|
* @param ResponseInterface $response |
22
|
|
|
* @return ExchangeRate[] |
23
|
|
|
* @throws \SteffenBrand\CurrCurr\Exception\ExchangeRatesMappingFailedException |
24
|
|
|
*/ |
25
|
|
|
public function map(ResponseInterface $response): array |
26
|
|
|
{ |
27
|
|
|
$body = (string) $response->getBody(); |
28
|
|
|
$xml = $this->parseBody($body); |
29
|
|
|
$date = $this->parseDate($xml); |
30
|
|
|
|
31
|
|
|
return $this->parseExchangeRates($xml, $date); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Parse body. |
36
|
|
|
* |
37
|
|
|
* @param string $body |
38
|
|
|
* @return SimpleXMLElement |
39
|
|
|
* @throws \SteffenBrand\CurrCurr\Exception\ExchangeRatesMappingFailedException |
40
|
|
|
*/ |
41
|
|
|
private function parseBody(string $body): SimpleXMLElement |
42
|
|
|
{ |
43
|
|
|
if (empty($body) === false) { |
44
|
|
|
libxml_use_internal_errors(true); |
45
|
|
|
$xml = simplexml_load_string($body); |
46
|
|
|
$errors = libxml_get_errors(); |
47
|
|
|
if (empty($errors) === true) { |
48
|
|
|
return $xml; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
throw new ExchangeRatesMappingFailedException(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Parse date. |
57
|
|
|
* |
58
|
|
|
* @param SimpleXMLElement $xml |
59
|
|
|
* @return \DateTime |
60
|
|
|
* @throws \SteffenBrand\CurrCurr\Exception\ExchangeRatesMappingFailedException |
61
|
|
|
*/ |
62
|
|
|
private function parseDate(SimpleXMLElement $xml): \DateTime |
63
|
|
|
{ |
64
|
|
|
$date = \DateTime::createFromFormat('Y-m-d', (string) $xml->Cube->Cube['time']); |
65
|
|
|
if (false !== $date) { |
66
|
|
|
$date->setTime(0, 0); |
67
|
|
|
return $date; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new ExchangeRatesMappingFailedException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Parse exchange rates. |
75
|
|
|
* |
76
|
|
|
* @param SimpleXMLElement $xml |
77
|
|
|
* @param \DateTime $date |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
private function parseExchangeRates(SimpleXMLElement $xml, \DateTime $date): array |
81
|
|
|
{ |
82
|
|
|
$exchangeRates = []; |
83
|
|
|
|
84
|
|
|
foreach ($xml->Cube->Cube->Cube as $item) { |
85
|
|
|
$currency = (string) $item['currency']; |
86
|
|
|
$rate = (float) $item['rate']; |
87
|
|
|
$exchangeRates[$currency] = new ExchangeRate( |
88
|
|
|
$currency, |
89
|
|
|
$rate, |
90
|
|
|
$date |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $exchangeRates; |
95
|
|
|
} |
96
|
|
|
} |