1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) itmedia.by <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Submarine\NbrbExchangeRatesBundle\Client; |
7
|
|
|
|
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
class ApiClient |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
const CACHE_KEY = 'nbrb-xml-cache'; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Получение официального курса белорусского рубля по отношению к иностранным валютам на определенную дату |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $urlExchangeRates = 'http://www.nbrb.by/Services/XmlExRates.aspx'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Получение динамики официального курса белорусского рубля по отношению к заданной иностранной валюте, |
26
|
|
|
* устанавливаемого Национальным банком Республики Беларусь (не более чем за 365 дней): |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $urlExchangeRatesDynamic = 'http://www.nbrb.by/Services/XmlExRatesDyn.aspx'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Client |
34
|
|
|
*/ |
35
|
|
|
private $client; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
private $httpConnectTimeout; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private $httpTimeout; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* ApiClient constructor. |
49
|
|
|
* @param string $urlExchangeRates |
50
|
|
|
* @param string $urlExchangeRatesDynamic |
51
|
|
|
* @param int $httpConnectTimeout |
52
|
|
|
* @param int $httpTimeout |
53
|
|
|
*/ |
54
|
|
|
public function __construct($urlExchangeRates, $urlExchangeRatesDynamic, $httpConnectTimeout, $httpTimeout) |
55
|
|
|
{ |
56
|
|
|
$this->urlExchangeRates = $urlExchangeRates; |
57
|
|
|
$this->urlExchangeRatesDynamic = $urlExchangeRatesDynamic; |
58
|
|
|
$this->httpConnectTimeout = $httpConnectTimeout; |
59
|
|
|
$this->httpTimeout = $httpTimeout; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return Client |
65
|
|
|
*/ |
66
|
|
|
public function getClient() |
67
|
|
|
{ |
68
|
|
|
if (!$this->client) { |
69
|
|
|
$this->client = new Client(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->client; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Курсы валют за дату |
78
|
|
|
* |
79
|
|
|
* @param \DateTime $date |
80
|
|
|
* @param bool $quotName |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function getXmlExchangesRates(\DateTime $date, $quotName = true) |
85
|
|
|
{ |
86
|
|
|
|
87
|
|
|
$query = [ |
88
|
|
|
'ondate' => $date->format('m/d/Y') |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
if ($quotName === true) { |
92
|
|
|
$query['mode'] = 1; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this->getResponseBody($this->urlExchangeRates, $query); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Динамика курса |
101
|
|
|
* |
102
|
|
|
* @param $currencyId |
103
|
|
|
* @param \DateTime $firstDate |
104
|
|
|
* @param \DateTime $lastDate |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getXmlExchangesRatesDynamic($currencyId, \DateTime $firstDate, \DateTime $lastDate) |
109
|
|
|
{ |
110
|
|
|
$query = [ |
111
|
|
|
'curId' => $currencyId, |
112
|
|
|
'fromDate' => $firstDate->format('m/d/Y'), |
113
|
|
|
'toDate' => $lastDate->format('m/d/Y'), |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
return $this->getResponseBody($this->urlExchangeRatesDynamic, $query); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Возвращает XML документ |
122
|
|
|
* @param $url |
123
|
|
|
* @param $query |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
private function getResponseBody($url, $query) |
127
|
|
|
{ |
128
|
|
|
$options['query'] = $query; |
|
|
|
|
129
|
|
|
$options['connect_timeout'] = $this->httpConnectTimeout; |
130
|
|
|
$options['timeout'] = $this->httpTimeout; |
131
|
|
|
|
132
|
|
|
$response = $this->getClient()->get($url, $options); |
133
|
|
|
|
134
|
|
|
return $response->getBody()->read($response->getBody()->getSize()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
} |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.