|
1
|
|
|
<?php |
|
2
|
|
|
namespace Katapoka\Ahgora; |
|
3
|
|
|
|
|
4
|
|
|
use DOMDocument; |
|
5
|
|
|
use DOMElement; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Katapoka\Ahgora\Contracts\IHttpResponse; |
|
8
|
|
|
|
|
9
|
|
|
class HtmlPageParser |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @param \Katapoka\Ahgora\Contracts\IHttpResponse $punchsPageResponse |
|
13
|
|
|
* |
|
14
|
|
|
* @return mixed |
|
15
|
|
|
*/ |
|
16
|
|
|
public function getPunchsTableHtml(IHttpResponse $punchsPageResponse) |
|
17
|
|
|
{ |
|
18
|
|
|
$tables = $this->getPageTables($punchsPageResponse); |
|
19
|
|
|
|
|
20
|
|
|
//The first table is the data summary |
|
21
|
|
|
return $tables['punchs']; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Get the punch's rows in array format. |
|
26
|
|
|
* |
|
27
|
|
|
* @param IHttpResponse $punchsPageResponse |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
*/ |
|
31
|
|
|
public function getPunchsRows(IHttpResponse $punchsPageResponse) |
|
32
|
|
|
{ |
|
33
|
|
|
$punchsTableHtml = $this->getPunchsTableHtml($punchsPageResponse); |
|
34
|
|
|
|
|
35
|
|
|
$dom = new DOMDocument(); |
|
36
|
|
|
if (!@$dom->loadHTML($punchsTableHtml)) { |
|
37
|
|
|
throw new InvalidArgumentException('Failed to parse punchsTable'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
$rows = $dom->getElementsByTagName('tr'); |
|
41
|
|
|
$rowsCollection = []; |
|
42
|
|
|
|
|
43
|
|
|
foreach ($rows as $row) { |
|
44
|
|
|
if ($punchRow = $this->parsePunchRow($row)) { |
|
45
|
|
|
$rowsCollection[] = $punchRow; |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $rowsCollection; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Parse the punch row and return its values. |
|
54
|
|
|
* |
|
55
|
|
|
* @param \DOMElement $row |
|
56
|
|
|
* |
|
57
|
|
|
* @return array|bool |
|
58
|
|
|
*/ |
|
59
|
|
|
private function parsePunchRow(DOMElement $row) { |
|
60
|
|
|
$cols = $row->getElementsByTagName('td'); |
|
61
|
|
|
if ($cols->length !== 8) { |
|
62
|
|
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return [ |
|
66
|
|
|
'date' => trim($cols->item(0)->nodeValue), |
|
67
|
|
|
'punchs' => trim($cols->item(2)->nodeValue), |
|
68
|
|
|
]; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Get both tables and return the strings into an array with the properties 'summary' and 'punchs'. |
|
73
|
|
|
* |
|
74
|
|
|
* @param IHttpResponse $punchsPageResponse |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
|
|
private function getPageTables(IHttpResponse $punchsPageResponse) |
|
79
|
|
|
{ |
|
80
|
|
|
$regex = '/<table.*?>.*?<\/table>/si'; |
|
81
|
|
|
|
|
82
|
|
|
if (!preg_match_all($regex, $punchsPageResponse->getBody(), $matches)) { |
|
83
|
|
|
throw new InvalidArgumentException('Pattern not found in the response'); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return [ |
|
87
|
|
|
'summary' => $matches[0][0], |
|
88
|
|
|
'punchs' => $matches[0][1], |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|