1 | <?php |
||
9 | class HtmlPageParser |
||
10 | { |
||
11 | /** |
||
12 | * @param \Katapoka\Ahgora\Contracts\IHttpResponse $punchesPageResponse |
||
13 | * |
||
14 | * @return mixed |
||
15 | */ |
||
16 | public function getPunchesTableHtml(IHttpResponse $punchesPageResponse) |
||
17 | { |
||
18 | $tables = $this->getPageTables($punchesPageResponse); |
||
19 | |||
20 | //The first table is the data summary |
||
21 | return $tables['punches']; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Get the punch's rows in array format. |
||
26 | * |
||
27 | * @param IHttpResponse $punchesPageResponse |
||
28 | * |
||
29 | * @return array |
||
30 | */ |
||
31 | public function getPunchesRows(IHttpResponse $punchesPageResponse) |
||
32 | { |
||
33 | $punchesTableHtml = $this->getPunchesTableHtml($punchesPageResponse); |
||
34 | |||
35 | $dom = new DOMDocument(); |
||
36 | if (!@$dom->loadHTML($punchesTableHtml)) { |
||
37 | throw new InvalidArgumentException('Failed to parse punchesTable'); |
||
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) |
||
71 | |||
72 | /** |
||
73 | * Get both tables and return the strings into an array with the properties 'summary' and 'punches'. |
||
74 | * |
||
75 | * @param IHttpResponse $punchesPageResponse |
||
76 | * |
||
77 | * @return array |
||
78 | */ |
||
79 | private function getPageTables(IHttpResponse $punchesPageResponse) |
||
92 | } |
||
93 |