1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) itmedia.by <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Submarine\NbrbExchangeRatesBundle\Provider; |
7
|
|
|
|
8
|
|
|
use Submarine\NbrbExchangeRatesBundle\Client\ApiClient; |
9
|
|
|
use Submarine\NbrbExchangeRatesBundle\CurrencyRateDate; |
10
|
|
|
use Submarine\NbrbExchangeRatesBundle\Exception\UndefinedCurrencyException; |
11
|
|
|
use Submarine\NbrbExchangeRatesBundle\ExchangeRate; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ExchangeRatesProvider |
15
|
|
|
* @package Submarine\NbrbParserBundle |
16
|
|
|
*/ |
17
|
|
|
class ExchangeRatesProvider implements ExchangeRatesProviderInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Выкидывать исключения? |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
private $showExceptions = true; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ApiClient |
30
|
|
|
*/ |
31
|
|
|
private $apiClient; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ExchangeRatesProvider constructor. |
35
|
|
|
* @param ApiClient $apiClient |
36
|
|
|
* @param bool $showExceptions |
37
|
|
|
*/ |
38
|
|
|
public function __construct(ApiClient $apiClient, $showExceptions) |
39
|
|
|
{ |
40
|
|
|
$this->apiClient = $apiClient; |
41
|
|
|
$this->showExceptions = $showExceptions; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Все курсы валют за указанную дату |
47
|
|
|
* |
48
|
|
|
* @param \DateTime $date Если null - текущая дата |
49
|
|
|
* |
50
|
|
|
* @return ExchangeRate[] |
51
|
|
|
*/ |
52
|
|
|
public function getAllRatesExchanges(\DateTime $date = null) |
53
|
|
|
{ |
54
|
|
|
if ($date === null) { |
55
|
|
|
$date = new \DateTime(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
try { |
59
|
|
|
$body = $this->apiClient->getXmlExchangesRates($date, false); |
60
|
|
|
$xml = simplexml_load_string($body); |
61
|
|
|
|
62
|
|
|
$result = []; |
63
|
|
|
|
64
|
|
|
if (count($xml->Currency)) { |
65
|
|
|
foreach ($xml->Currency as $item) { |
66
|
|
|
$exRate = ExchangeRate::createFromXML($item, $date); |
67
|
|
|
$result[$exRate->getCharCode()] = $exRate; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $result; |
72
|
|
|
|
73
|
|
|
} catch (\Exception $exc) { |
74
|
|
|
if ($this->showExceptions) { |
75
|
|
|
throw new $exc; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return []; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Курсы выбранных валют за указанную дату |
85
|
|
|
* |
86
|
|
|
* @param array $codes Коды валют в формате ISO (USD, UAH) |
87
|
|
|
* @param \DateTime $date Дата, по умолчанию текущая дата |
88
|
|
|
* |
89
|
|
|
* @throws UndefinedCurrencyException |
90
|
|
|
* @return ExchangeRate[] |
91
|
|
|
*/ |
92
|
|
|
public function getRatesExchanges(array $codes, \DateTime $date = null) |
93
|
|
|
{ |
94
|
|
|
$rates = $this->getAllRatesExchanges($date); |
95
|
|
|
$result = []; |
96
|
|
|
foreach ($codes as $code) { |
97
|
|
|
if (array_key_exists($code, $rates)) { |
98
|
|
|
$result[$code] = $rates[$code]; |
99
|
|
|
} elseif ($this->showExceptions) { |
100
|
|
|
throw new UndefinedCurrencyException(sprintf('Undefined currency code: %s', $code)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Курс валюты за указанную дату |
110
|
|
|
* |
111
|
|
|
* @param string $code Код валюты в формате ISO (USD, UAH) |
112
|
|
|
* @param \DateTime $date Если null - текущая дата |
113
|
|
|
* |
114
|
|
|
* @throws UndefinedCurrencyException |
115
|
|
|
* @return ExchangeRate |
116
|
|
|
*/ |
117
|
|
|
public function getRateExchange($code, \DateTime $date = null) |
118
|
|
|
{ |
119
|
|
|
$rates = $this->getAllRatesExchanges($date); |
120
|
|
|
if (array_key_exists($code, $rates)) { |
121
|
|
|
return $rates[$code]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($this->showExceptions) { |
125
|
|
|
throw new UndefinedCurrencyException(sprintf('Undefined currency code: %s', $code)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return new ExchangeRate(null, null, null, null, null, null, new \DateTime()); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param string $code |
134
|
|
|
* @param \DateTime $firstDate |
135
|
|
|
* @param \DateTime $lastDate |
136
|
|
|
* |
137
|
|
|
* @throws UndefinedCurrencyException |
138
|
|
|
* @return CurrencyRateDate[] |
139
|
|
|
*/ |
140
|
|
|
public function getRatesExchangesDynamic($code, \DateTime $firstDate, \DateTime $lastDate) |
141
|
|
|
{ |
142
|
|
|
$currency = $this->getRateExchange($code); |
143
|
|
|
|
144
|
|
|
if (!$currency->getId()) { |
145
|
|
|
|
146
|
|
|
if ($this->showExceptions) { |
147
|
|
|
throw new UndefinedCurrencyException(sprintf('Undefined currency id: %s', $code)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return []; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
try { |
154
|
|
|
$body = $this->apiClient->getXmlExchangesRatesDynamic($currency->getId(), $firstDate, $lastDate); |
155
|
|
|
$xml = simplexml_load_string($body); |
156
|
|
|
|
157
|
|
|
$result = []; |
158
|
|
|
if (count($xml->Record)) { |
159
|
|
|
foreach ($xml->Record as $item) { |
160
|
|
|
$rate = CurrencyRateDate::createFromXML($item); |
161
|
|
|
$result[] = $rate; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $result; |
166
|
|
|
|
167
|
|
|
} catch (\Exception $exc) { |
168
|
|
|
if ($this->showExceptions) { |
169
|
|
|
throw new $exc; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return []; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |