1
|
|
|
<?php namespace zServices\Sintegra\Services\Portais\SP; |
2
|
|
|
|
3
|
|
|
use zServices\Miscellany\Interfaces\CrawlerInterface; |
4
|
|
|
use Symfony\Component\DomCrawler\Crawler as BaseCrawler; |
5
|
|
|
use zServices\Miscellany\Exceptions\ErrorFoundData; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class Crawler extends BaseCrawler implements CrawlerInterface |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Selectors to crawler |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $selectors = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get selectors and initialize crawler in HTML |
21
|
|
|
* @param string $html |
22
|
|
|
* @param array $selectors |
23
|
|
|
* @return void |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function __construct($html, $selectors) |
26
|
|
|
{ |
27
|
|
|
$this->selectors = $selectors; |
28
|
|
|
|
29
|
|
|
parent::__construct($html); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Verifica antes de fazer o crawler se possui erros |
34
|
|
|
* na requisição |
35
|
|
|
* @return boolean|null |
36
|
|
|
*/ |
37
|
|
|
public function hasError() |
38
|
|
|
{ |
39
|
|
|
$node = $this->scrap($this->selectors['razao_social']); |
40
|
|
|
|
41
|
|
|
if (!$node->count()) |
42
|
|
|
{ |
43
|
|
|
throw new ErrorFoundData($this->clearString($this->scrap($this->selectors['error'])->text()), 1); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Extrai informações do HTML através do DOM |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function scraping() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
$scrapped = []; |
55
|
|
|
|
56
|
|
|
$this->hasError(); |
57
|
|
|
|
58
|
|
|
foreach ($this->selectors as $name => $selector) { |
59
|
|
|
if (is_string($selector)) { |
60
|
|
|
$node = $this->scrap($selector); |
61
|
|
|
|
62
|
|
|
if ($node->count()) { |
63
|
|
|
$scrapped[$name] = $this->clearString($node->text()); |
64
|
|
|
} |
65
|
|
|
} elseif (is_array($selector)) { |
66
|
|
|
foreach ($selector as $selector => $repeat) { |
|
|
|
|
67
|
|
|
$node = $this->scrap($selector); |
68
|
|
|
if ($node->count()) { |
69
|
|
|
foreach ($node->filter($repeat) as $loop) |
70
|
|
|
{ |
71
|
|
|
$scrapped[$name][] = $this->clearString($loop->nodeValue); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $scrapped; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Limpa o valor repassado |
83
|
|
|
* @param string $string |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function clearString($string) |
87
|
|
|
{ |
88
|
|
|
return trim(preg_replace(['/[\s]+/mu'], ' ', $string)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Filtra selector no crawler |
93
|
|
|
*/ |
94
|
|
|
public function scrap($selector) |
95
|
|
|
{ |
96
|
|
|
$node = $this->filter($selector); |
97
|
|
|
return $node; |
98
|
|
|
} |
99
|
|
|
} |
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.