1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Defr; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use Defr\Justice\JusticeRecord; |
7
|
|
|
use Defr\Justice\SubjectNotFoundException; |
8
|
|
|
use Defr\Parser\JusticeJednatelPersonParser; |
9
|
|
|
use Defr\Parser\JusticeSpolecnikPersonParser; |
10
|
|
|
use Goutte\Client; |
11
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
12
|
|
|
|
13
|
|
|
final class Justice |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
const URL_BASE = 'https://or.justice.cz/ias/ui/'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
const URL_SUBJECTS = 'https://or.justice.cz/ias/ui/rejstrik-$firma?ico=%d'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Client |
27
|
|
|
*/ |
28
|
|
|
private $client; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Justice constructor. |
32
|
|
|
* |
33
|
|
|
* @param Client $client |
34
|
|
|
*/ |
35
|
|
|
public function __construct(Client $client) |
36
|
|
|
{ |
37
|
|
|
$this->client = $client; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param int $id |
42
|
|
|
* |
43
|
|
|
* @throws SubjectNotFoundException |
44
|
|
|
* |
45
|
|
|
* @return JusticeRecord|false |
46
|
|
|
*/ |
47
|
|
|
public function findById($id) |
48
|
|
|
{ |
49
|
|
|
Assertion::integer($id); |
50
|
|
|
|
51
|
|
|
$crawler = $this->client->request('GET', sprintf(self::URL_SUBJECTS, $id)); |
52
|
|
|
$detailUrl = $this->extractDetailUrlFromCrawler($crawler); |
|
|
|
|
53
|
|
|
|
54
|
|
|
if (false === $detailUrl) { |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$people = []; |
59
|
|
|
|
60
|
|
|
$crawler = $this->client->request('GET', $detailUrl); |
61
|
|
|
$crawler->filter('.aunp-content .div-table')->each(function (Crawler $table) use (&$people) { |
62
|
|
|
$title = $table->filter('.vr-hlavicka')->text(); |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
if ('jednatel: ' === $title) { |
66
|
|
|
$person = JusticeJednatelPersonParser::parseFromDomCrawler($table); |
67
|
|
|
$people[$person->getName()] = $person; |
68
|
|
|
} elseif ('Společník: ' === $title) { |
69
|
|
|
$person = JusticeSpolecnikPersonParser::parseFromDomCrawler($table); |
70
|
|
|
$people[$person->getName()] = $person; |
71
|
|
|
} |
72
|
|
|
} catch (\Exception $e) { |
73
|
|
|
throw $e; |
74
|
|
|
} |
75
|
|
|
}); |
76
|
|
|
|
77
|
|
|
return new JusticeRecord($people); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param Crawler $crawler |
82
|
|
|
* |
83
|
|
|
* @return false|string |
84
|
|
|
*/ |
85
|
|
|
private function extractDetailUrlFromCrawler(Crawler $crawler) |
86
|
|
|
{ |
87
|
|
|
$linksFound = $crawler->filter('.result-links > li > a'); |
88
|
|
|
if (!$linksFound) { |
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$href = $linksFound->extract(['href']); |
93
|
|
|
if (!isset($href[1])) { |
94
|
|
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return self::URL_BASE.$href[1]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: