1
|
|
|
<?php namespace zServices\ReceitaFederal\Services\Portais\AN; |
2
|
|
|
|
3
|
|
|
use zServices\Miscellany\Interfaces\CrawlerInterface; |
4
|
|
|
use Symfony\Component\DomCrawler\Crawler as BaseCrawler; |
5
|
|
|
use zServices\Miscellany\Exceptions\NoSelectorsConfigured; |
6
|
|
|
use zServices\Miscellany\Exceptions\InvalidCaptcha; |
7
|
|
|
use zServices\Miscellany\Exceptions\ErrorFoundData; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
class Crawler extends BaseCrawler implements CrawlerInterface |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Selectors to crawler |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private $selectors = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get selectors and initialize crawler in HTML |
23
|
|
|
* @param string $html |
24
|
|
|
* @param array $selectors |
25
|
|
|
* @return void |
|
|
|
|
26
|
|
|
*/ |
27
|
|
|
public function __construct($html, $selectors) |
28
|
|
|
{ |
29
|
|
|
$this->selectors = $selectors; |
30
|
|
|
|
31
|
|
|
parent::__construct($html); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Verifica antes de fazer o crawler se possui erros |
36
|
|
|
* na requisição |
37
|
|
|
* @return boolean|null |
38
|
|
|
*/ |
39
|
|
|
public function hasError() |
40
|
|
|
{ |
41
|
|
|
if (count($this->selectors) == 0) { |
42
|
|
|
throw new NoSelectorsConfigured("NoSelectorsConfigured", 1); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
// verifica se a página seguida na requisição |
46
|
|
|
// é página de erro da receita federal |
47
|
|
|
$node = $this->filter($this->selectors['error']); |
48
|
|
|
|
49
|
|
|
if ($node->count()) { |
50
|
|
|
throw new ErrorFoundData($this->clearString($node->text()), 1); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// CNPJ informado é válido? |
54
|
|
|
if ($this->filter('#imgCaptcha')->count()) { |
55
|
|
|
throw new InvalidCaptcha('Captcha inválido', 99); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Extrai informações do HTML através do DOM |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function scraping() |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
$scrapped = []; |
67
|
|
|
|
68
|
|
|
$this->hasError(); |
69
|
|
|
|
70
|
|
|
foreach ($this->selectors as $name => $selector) { |
71
|
|
|
if (is_string($selector)) { |
72
|
|
|
$node = $this->scrap($selector); |
73
|
|
|
|
74
|
|
|
if ($node->count()) { |
75
|
|
|
$scrapped[$name] = $this->clearString($node->text()); |
76
|
|
|
} |
77
|
|
|
} elseif (is_array($selector)) { |
78
|
|
|
foreach ($selector as $selector => $repeat) { |
|
|
|
|
79
|
|
|
$node = $this->scrap($selector); |
80
|
|
|
if ($node->count()) { |
81
|
|
|
foreach ($node->filter($repeat) as $loop) |
82
|
|
|
{ |
83
|
|
|
$scrapped[$name][] = $this->clearString($loop->nodeValue); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $scrapped; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Limpa o valor repassado |
95
|
|
|
* @param string $string |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function clearString($string) |
99
|
|
|
{ |
100
|
|
|
return trim(preg_replace(['/[\s]+/mu'], ' ', $string)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Filtra selector no crawler |
105
|
|
|
*/ |
106
|
|
|
public function scrap($selector) |
107
|
|
|
{ |
108
|
|
|
$node = $this->filter($selector); |
109
|
|
|
return $node; |
110
|
|
|
} |
111
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.