Passed
Pull Request — master (#2)
by Igor
03:31
created

DomParser::getFullLawyer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
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
    /**
18
     * @var string
19
     */
20
    protected const CURRENT_PAGE_SELECTOR = 'span.currentStep';
21
22
    /**
23
     * @var string
24
     */
25
    protected const PAGINATION_BLOCK_SELECTOR = 'div.pagination';
26
27
    /**
28
     * @var string
29
     */
30
    protected const PAGINATION_STEP_SELECTOR = 'a.step';
31
32
    /**
33
     * @var string
34
     */
35
    protected const LAWYERS_LIST_BLOCK_SELECTOR = 'table.persons > tbody > tr';
36
37
    /**
38
     * @var string
39
     */
40
    protected const LAWYER_DETAIL_SELECTOR = '.floating > p.row';
41
42
    public function list(string $body): FindResponse
43
    {
44
        $dom = (new Dom())->loadStr($body);
45
46
        return (new FindResponse())
47
            ->setPage($this->getCurrentPage($dom))
48
            ->setTotalPage($this->getTotalPage($dom))
49
            ->setLawyers($this->getListLawyers($dom));
50
    }
51
52
    protected function getCurrentPage(Dom $dom): int
53
    {
54
        if ($span = $dom->find(static::CURRENT_PAGE_SELECTOR, 0)) {
55
            return (int) $span->text();
56
        }
57
58
        return 1;
59
    }
60
61
    protected function getTotalPage(Dom $dom): int
62
    {
63
        /**
64
         * @var HtmlNode[] $collection
65
         */
66
        $collection = $dom
67
            ->find(static::PAGINATION_BLOCK_SELECTOR, 0)
68
            ->find(static::PAGINATION_STEP_SELECTOR)
69
            ->toArray();
70
        if (0 === count($collection)) {
71
            return 1;
72
        }
73
        $lastStep = (int) end($collection)->text();
74
        $currentPage = $this->getCurrentPage($dom);
75
76
        return $lastStep > $currentPage ? $lastStep : $currentPage;
77
    }
78
79
    /**
80
     * @param \PHPHtmlParser\Dom $dom
81
     *
82
     * @return Lawyer[]
83
     * @throws ChildNotFoundException
84
     * @throws NotLoadedException
85
     */
86
    protected function getListLawyers(Dom $dom): array
87
    {
88
        $data = [];
89
        /**
90
         * @var Dom\HtmlNode[]|Collection $nodes
91
         */
92
        $nodes = $dom->find(static::LAWYERS_LIST_BLOCK_SELECTOR);
93
        foreach ($nodes as $node) {
94
            /**
95
             * @var Dom\HtmlNode[]|Collection $tds
96
             */
97
            $tds = $node->find('td');
98
            $tds = array_filter($tds->toArray(), static function (HtmlNode $node) {
99
                return $node->outerHtml() !== '' && $node->getAttribute('class') !== 'empty';
100
            });
101
            $data[] = (new Lawyer())
102
                ->setRegisterNumber($tds[3]->text())
103
                ->setFullName($tds[4]->text(true))
104
                ->setUrl($tds[4]->firstChild()->getAttribute('href'))
105
                ->setTerritorialSubject($tds[5]->text())
106
                ->setCertificateNumber($tds[6]->text())
107
                ->setStatus($tds[7]->text());
108
        }
109
110
        return $data;
111
    }
112
113
    public function detail(string $body): DetailLawyer
114
    {
115
        $dom = (new Dom())->loadStr($body);
116
117
        /**
118
         * @var Dom\HtmlNode[] $nodes
119
         */
120
        $nodes = $dom->find(static::LAWYER_DETAIL_SELECTOR)->toArray();
121
122
        $lawyer = (new DetailLawyer())
123
            ->setChamberOfLaw(trim($nodes[5]->text()));
124
        if (($organizationForm = trim($nodes[7]->text())) !== '') {
125
            $lawyer->setLawFormation(
126
                (new LawFormation())
127
                    ->setOrganizationalForm($organizationForm)
128
                    ->setName(trim($nodes[9]->text()))
129
                    ->setAddress(trim($nodes[11]->text()))
130
                    ->setPhone(trim($nodes[13]->text()))
131
                    ->setEmail(trim($nodes[15]->text()))
132
            );
133
        }
134
135
        return $lawyer;
136
    }
137
}
138