1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SomeWork\Minjust\Parser; |
4
|
|
|
|
5
|
|
|
use PHPHtmlParser\Dom\HtmlNode; |
6
|
|
|
use PHPHtmlParser\Exceptions\ChildNotFoundException; |
7
|
|
|
use PHPHtmlParser\Exceptions\NotLoadedException; |
8
|
|
|
use PHPHtmlParser\Dom\Collection; |
9
|
|
|
use PHPHtmlParser\Dom; |
10
|
|
|
use SomeWork\Minjust\Entity\DetailLawyer; |
11
|
|
|
use SomeWork\Minjust\Entity\LawFormation; |
12
|
|
|
use SomeWork\Minjust\Entity\Lawyer; |
13
|
|
|
use SomeWork\Minjust\FindResponse; |
14
|
|
|
|
15
|
|
|
class DomParser implements ParserInterface |
16
|
|
|
{ |
17
|
|
|
protected function getTotalPage(Dom $dom): int |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var HtmlNode[] $collection |
21
|
|
|
*/ |
22
|
|
|
$collection = $dom->find('div.pagination', 0)->find('a.step')->toArray(); |
23
|
|
|
if (0 === count($collection)) { |
24
|
|
|
return 1; |
25
|
|
|
} |
26
|
|
|
$lastStep = (int) end($collection)->text(); |
27
|
|
|
$currentPage = $this->getCurrentPage($dom); |
28
|
|
|
|
29
|
|
|
return $lastStep > $currentPage ? $lastStep : $currentPage; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function getCurrentPage(Dom $dom): int |
33
|
|
|
{ |
34
|
|
|
if ($span = $dom->find('span.currentStep', 0)) { |
35
|
|
|
return (int) $span->text(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return 1; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param \PHPHtmlParser\Dom $dom |
43
|
|
|
* |
44
|
|
|
* @return Lawyer[] |
45
|
|
|
* @throws ChildNotFoundException |
46
|
|
|
* @throws NotLoadedException |
47
|
|
|
*/ |
48
|
|
|
protected function getListLawyers(Dom $dom): array |
49
|
|
|
{ |
50
|
|
|
$data = []; |
51
|
|
|
/** |
52
|
|
|
* @var Dom\HtmlNode[]|Collection $nodes |
53
|
|
|
*/ |
54
|
|
|
$nodes = $dom->find('table.persons > tbody > tr'); |
55
|
|
|
foreach ($nodes as $node) { |
56
|
|
|
/** |
57
|
|
|
* @var Dom\HtmlNode[]|Collection $tds |
58
|
|
|
*/ |
59
|
|
|
$tds = $node->find('td'); |
60
|
|
|
$tds = array_filter($tds->toArray(), static function (HtmlNode $node) { |
61
|
|
|
return $node->outerHtml() !== '' && $node->getAttribute('class') !== 'empty'; |
62
|
|
|
}); |
63
|
|
|
$data[] = (new Lawyer()) |
64
|
|
|
->setRegisterNumber($tds[3]->text()) |
65
|
|
|
->setFullName($tds[4]->text(true)) |
66
|
|
|
->setUrl($tds[4]->firstChild()->getAttribute('href')) |
67
|
|
|
->setTerritorialSubject($tds[5]->text()) |
68
|
|
|
->setCertificateNumber($tds[6]->text()) |
69
|
|
|
->setStatus($tds[7]->text()); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $data; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getFullLawyer(Dom $dom): DetailLawyer |
76
|
|
|
{ |
77
|
|
|
$nodes = $dom->find('.floating > p.row')->toArray(); |
78
|
|
|
|
79
|
|
|
return (new DetailLawyer()) |
80
|
|
|
->setLawFormation($this->getLawFormation($nodes)) |
81
|
|
|
->setChamberOfLaw(trim($nodes[5]->text())); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
protected function getLawFormation(array $nodes): ?LawFormation |
85
|
|
|
{ |
86
|
|
|
$formation = (new LawFormation()) |
87
|
|
|
->setOrganizationalForm(trim($nodes[7]->text())) |
88
|
|
|
->setName(trim($nodes[9]->text())) |
89
|
|
|
->setAddress(trim($nodes[11]->text())) |
90
|
|
|
->setPhone(trim($nodes[13]->text())) |
91
|
|
|
->setEmail(trim($nodes[15]->text())); |
92
|
|
|
|
93
|
|
|
return $formation->getOrganizationalForm() !== '' ? $formation : null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function list(string $body): FindResponse |
97
|
|
|
{ |
98
|
|
|
$dom = (new Dom())->loadStr($body); |
99
|
|
|
|
100
|
|
|
$findResponse = new FindResponse(); |
101
|
|
|
$findResponse |
102
|
|
|
->setPage($this->getCurrentPage($dom)) |
103
|
|
|
->setTotalPage($this->getTotalPage($dom)) |
104
|
|
|
->setLawyers($this->getListLawyers($dom)); |
105
|
|
|
|
106
|
|
|
return $findResponse; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function detail(string $body): DetailLawyer |
110
|
|
|
{ |
111
|
|
|
$dom = (new Dom())->loadStr($body); |
112
|
|
|
|
113
|
|
|
return $this->getFullLawyer($dom); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|