1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sportic\Timing\RaceTecClient\Parsers; |
4
|
|
|
|
5
|
|
|
use DOMElement; |
6
|
|
|
use Sportic\Timing\CommonClient\Models\Race; |
7
|
|
|
use Sportic\Timing\CommonClient\Models\Result; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class EventPage |
11
|
|
|
* @package Sportic\Timing\RaceTecClient\Parsers |
12
|
|
|
*/ |
13
|
|
|
class EventPage extends AbstractParser |
14
|
|
|
{ |
15
|
|
|
protected $returnContent = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
protected function generateContent() |
21
|
|
|
{ |
22
|
|
|
$this->returnContent['races'] = $this->parseRaces(); |
23
|
|
|
$this->returnContent['results']['header'] = $this->parseResultsHeader(); |
24
|
|
|
$this->returnContent['results']['list'] = $this->parseResultsTable(); |
25
|
|
|
$this->returnContent['results']['pagination'] = $this->parseResultsPagination(); |
26
|
|
|
|
27
|
|
|
return $this->returnContent; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getModelClassName() |
31
|
|
|
{ |
32
|
|
|
// TODO: Implement getModelClassName() method. |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
protected function parseRaces() |
39
|
|
|
{ |
40
|
|
|
$return = []; |
41
|
|
|
$eventMenu = $this->getCrawler()->filter('#ctl00_Content_Main_pnlEventMenu'); |
42
|
|
|
if ($eventMenu->count() > 0) { |
43
|
|
|
$raceLinks = $eventMenu->filter('div.tab > a'); |
44
|
|
|
foreach ($raceLinks as $link) { |
45
|
|
|
$parameters = [ |
46
|
|
|
'name' => $link->nodeValue, |
47
|
|
|
'href' => $link->getAttribute('href') |
48
|
|
|
]; |
49
|
|
|
$return[] = new Race($parameters); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
protected function parseResultsTable() |
60
|
|
|
{ |
61
|
|
|
$return = []; |
62
|
|
|
$resultsRows = $this->getCrawler()->filter( |
63
|
|
|
'#ctl00_Content_Main_grdNew_DXMainTable > tbody > tr' |
64
|
|
|
); |
65
|
|
|
if ($resultsRows->count() > 0) { |
66
|
|
|
foreach ($resultsRows as $resultRow) { |
67
|
|
|
if ($resultRow->getAttribute('id') !== 'ctl00_Content_Main_grdNew_DXHeadersRow') { |
68
|
|
|
$result = $this->parseResultsRow($resultRow); |
69
|
|
|
if ($result) { |
70
|
|
|
$return[] = $result; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
protected function parseResultsHeader() |
83
|
|
|
{ |
84
|
|
|
$return = []; |
85
|
|
|
|
86
|
|
|
$fields = $this->getCrawler()->filter( |
87
|
|
|
'#ctl00_Content_Main_grdNew_DXHeadersRow table td a' |
88
|
|
|
); |
89
|
|
|
$fieldMap = self::getLabelMaps(); |
90
|
|
|
if ($fields->count() > 0) { |
91
|
|
|
$colNum = 0; |
92
|
|
View Code Duplication |
foreach ($fields as $field) { |
|
|
|
|
93
|
|
|
$fieldName = $field->nodeValue; |
94
|
|
|
$labelFind = array_search($fieldName, $fieldMap); |
95
|
|
|
if ($labelFind) { |
|
|
|
|
96
|
|
|
$return[$colNum] = $labelFind; |
97
|
|
|
} |
98
|
|
|
$colNum++; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $return; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param DOMElement $row |
107
|
|
|
* |
108
|
|
|
* @return bool|Result |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
protected function parseResultsRow(DOMElement $row) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$parameters = []; |
113
|
|
|
$i = 0; |
114
|
|
|
foreach ($row->childNodes as $cell) { |
115
|
|
|
if ($cell instanceof DOMElement) { |
116
|
|
|
$parameters = $this->parseResultsRowCell($i, $cell, $parameters); |
117
|
|
|
$i++; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
if (count($parameters)) { |
121
|
|
|
return new Result($parameters); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param int $colCount |
129
|
|
|
* @param DOMElement $cell |
130
|
|
|
* |
131
|
|
|
* @param array $parameters |
132
|
|
|
* |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
protected function parseResultsRowCell($colCount, DOMElement $cell, $parameters = []) |
136
|
|
|
{ |
137
|
|
|
if (isset($this->returnContent['results']['header'][$colCount])) { |
138
|
|
|
$field = $this->returnContent['results']['header'][$colCount]; |
139
|
|
|
if ($field == 'fullName') { |
140
|
|
|
$parameters['href'] = $cell->firstChild->getAttribute('href'); |
141
|
|
|
$parameters[$field] = trim($cell->nodeValue); |
142
|
|
|
} else { |
143
|
|
|
$parameters[$field] = trim($cell->nodeValue); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $parameters; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @return array |
152
|
|
|
*/ |
153
|
|
|
protected function parseResultsPagination() |
154
|
|
|
{ |
155
|
|
|
$return = [ |
156
|
|
|
'current' => 1, |
157
|
|
|
'all' => 1, |
158
|
|
|
'items' => 1, |
159
|
|
|
]; |
160
|
|
|
|
161
|
|
|
$paginationObject = $this->getCrawler()->filter( |
162
|
|
|
'#ctl00_Content_Main_lblTopPager' |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
if ($paginationObject->count() > 0) { |
166
|
|
|
$elements = explode(' ', $paginationObject->html()); |
167
|
|
|
$return['current'] = intval($elements[1]); |
168
|
|
|
$return['all'] = intval($elements[3]); |
169
|
|
|
$return['items'] = intval(str_replace('(', '', $elements[4])); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return array |
177
|
|
|
*/ |
178
|
|
|
public static function getLabelMaps() |
179
|
|
|
{ |
180
|
|
|
return [ |
181
|
|
|
'posGen' => 'Pos', |
182
|
|
|
'bib' => 'Race No', |
183
|
|
|
'fullName' => 'Name', |
184
|
|
|
'time' => 'Time', |
185
|
|
|
'category' => 'Category', |
186
|
|
|
'posCategory' => 'Cat Pos', |
187
|
|
|
'gender' => 'Gender', |
188
|
|
|
'posGender' => 'Gen Pos' |
189
|
|
|
]; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.