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