1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Exchange Rate package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* Implementation of exchange rate crawler for Banca Intesa Serbia, http://www.bancaintesa.rs. |
6
|
|
|
* |
7
|
|
|
* (c) 2016 RunOpenCode |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
namespace RunOpenCode\ExchangeRate\BancaIntesaSerbia\Parser; |
13
|
|
|
|
14
|
|
|
use RunOpenCode\ExchangeRate\Contract\RateInterface; |
15
|
|
|
use RunOpenCode\ExchangeRate\Model\Rate; |
16
|
|
|
use RunOpenCode\ExchangeRate\BancaIntesaSerbia\Api; |
17
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class HtmlParser |
21
|
|
|
* |
22
|
|
|
* Parse HTML document with daily rates from Banca Intesa Serbia. |
23
|
|
|
* |
24
|
|
|
* @package RunOpenCode\ExchangeRate\BancaIntesaSerbia\Parser |
25
|
|
|
*/ |
26
|
|
|
class HtmlParser |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var RateInterface[] |
30
|
|
|
*/ |
31
|
|
|
private $rates; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $currentRate; |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \DateTime |
40
|
|
|
*/ |
41
|
|
|
private $date; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $html; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* HtmlParser constructor. |
50
|
|
|
* @param $node string |
51
|
|
|
* @param $date \DateTime |
52
|
|
|
*/ |
53
|
|
|
public function __construct($node, \DateTime $date) |
54
|
|
|
{ |
55
|
|
|
$this->html = $node; |
56
|
|
|
$this->date = $date; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getRates() |
60
|
|
|
{ |
61
|
|
|
if (!$this->rates) { |
|
|
|
|
62
|
|
|
$this->rates = $this->parseHtml($this->html, $this->date); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $this->rates; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function parseHtml($html, \DateTime $date) |
69
|
|
|
{ |
70
|
|
|
$crawler = new Crawler($html); |
71
|
|
|
$crawler = $crawler->filter('table'); |
72
|
|
|
|
73
|
|
|
return $this->extractRates($crawler, $date); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private function extractRates(Crawler $crawler, \DateTime $date) |
77
|
|
|
{ |
78
|
|
|
$rates = array(); |
79
|
|
|
|
80
|
|
|
$crawler->filter('tr')->each(function (Crawler $node) use ($date, &$rates) { |
81
|
|
|
|
82
|
|
|
$row = $this->parseRow($node); |
83
|
|
|
|
84
|
|
|
if (null !== $row) { |
85
|
|
|
|
86
|
|
|
$rates[] = $this->buildRate( |
87
|
|
|
$row['default'] / $row['unit'], |
88
|
|
|
$row['currencyCode'], |
89
|
|
|
'default', |
90
|
|
|
$date |
91
|
|
|
); |
92
|
|
|
|
93
|
|
View Code Duplication |
if ($row['foreign_exchange_buying'] > 0) { |
|
|
|
|
94
|
|
|
$rates[] = $this->buildRate( |
95
|
|
|
$row['foreign_exchange_buying'] / $row['unit'], |
96
|
|
|
$row['currencyCode'], |
97
|
|
|
'foreign_exchange_buying', |
98
|
|
|
$date |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
if ($row['foreign_exchange_selling'] > 0) { |
|
|
|
|
103
|
|
|
$rates[] = $this->buildRate( |
104
|
|
|
$row['foreign_exchange_selling'] / $row['unit'], |
105
|
|
|
$row['currencyCode'], |
106
|
|
|
'foreign_exchange_selling', |
107
|
|
|
$this->date |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
View Code Duplication |
if ($row['foreign_cash_buying'] > 0) { |
|
|
|
|
112
|
|
|
$rates[] = $this->buildRate( |
113
|
|
|
$row['foreign_cash_buying'] / $row['unit'], |
114
|
|
|
$row['currencyCode'], |
115
|
|
|
'foreign_cash_buying', |
116
|
|
|
$this->date |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
View Code Duplication |
if ($row['foreign_cash_selling'] > 0) { |
|
|
|
|
121
|
|
|
$rates[] = $this->buildRate( |
122
|
|
|
$row['foreign_cash_selling'] / $row['unit'], |
123
|
|
|
$row['currencyCode'], |
124
|
|
|
'foreign_cash_selling', |
125
|
|
|
$this->date |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
}); |
130
|
|
|
|
131
|
|
|
return $rates; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function parseRow(Crawler $crawler) |
135
|
|
|
{ |
136
|
|
|
$currentRow = array( |
137
|
|
|
'currencyCode' => '' |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
$crawler->filter('td')->each(function (Crawler $node, $i) use (&$currentRow) { |
141
|
|
|
|
142
|
|
|
switch ($i) { |
143
|
|
|
case 1: |
144
|
|
|
$currentRow['currencyCode'] = trim($node->text()); |
145
|
|
|
break; |
146
|
|
|
case 2: |
147
|
|
|
$currentRow['unit'] = (int)trim($node->text()); |
148
|
|
|
break; |
149
|
|
|
case 3: |
150
|
|
|
$currentRow['foreign_exchange_buying'] = (float)trim($node->text()); |
151
|
|
|
break; |
152
|
|
|
case 4: |
153
|
|
|
$currentRow['default'] = (float)trim($node->text()); |
154
|
|
|
break; |
155
|
|
|
case 5: |
156
|
|
|
$currentRow['foreign_exchange_selling'] = (float)trim($node->text()); |
157
|
|
|
break; |
158
|
|
|
case 6: |
159
|
|
|
$currentRow['foreign_cash_buying'] = (float)trim($node->text()); |
160
|
|
|
break; |
161
|
|
|
case 7: |
162
|
|
|
$currentRow['foreign_cash_selling'] = (float)trim($node->text()); |
163
|
|
|
break; |
164
|
|
|
} |
165
|
|
|
}); |
166
|
|
|
|
167
|
|
|
return strlen($currentRow['currencyCode']) === 3 ? $currentRow : null; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
private function buildRate($value, $currencyCode, $rateType, $date) { |
171
|
|
|
|
172
|
|
|
return new Rate( |
173
|
|
|
Api::NAME, |
174
|
|
|
$value, |
175
|
|
|
$currencyCode, |
176
|
|
|
$rateType, |
177
|
|
|
$date, |
178
|
|
|
'RSD', |
179
|
|
|
new \DateTime('now'), |
180
|
|
|
new \DateTime('now') |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.