|
1
|
|
|
<?php |
|
2
|
|
|
namespace PHPTools\PHPHtmlDom; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Esta es la clas eque el usuario debe inicializar para convertir un texto html en un objeto DOM. |
|
6
|
|
|
*/ |
|
7
|
|
|
class PHPHtmlDom |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Objeto que permite convertir un selector css en un path. |
|
11
|
|
|
* @var \Symfony\Component\CssSelector\CssSelector |
|
12
|
|
|
*/ |
|
13
|
|
|
private $selector; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Objeto que permite escribir logs. |
|
17
|
|
|
* @var \PHPTools\PHPHtmlDom\Core\PHPHtmlDomLog |
|
18
|
|
|
*/ |
|
19
|
|
|
private $logger; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Objeto nativo de php que permite crear un documento DOM. |
|
23
|
|
|
* @var \DOMDocument |
|
24
|
|
|
*/ |
|
25
|
|
|
private $dom; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Objeto nativo de php que permite buscar un xpath dentro de un documento DOM. |
|
29
|
|
|
* @var \DOMXPath |
|
30
|
|
|
*/ |
|
31
|
|
|
private $xpath; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Objeto que permite importar el contennido html. |
|
35
|
|
|
* @var \PHPTools\PHPHtmlDom\Core\PHPHtmlDomImportHtml |
|
36
|
|
|
*/ |
|
37
|
|
|
private $importer; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Contenido crudo html. |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $html_content; |
|
44
|
|
|
|
|
45
|
|
|
public function __construct() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->selector = new \Symfony\Component\CssSelector\CssSelectorConverter; |
|
|
|
|
|
|
48
|
|
|
$this->logger = new \PHPTools\PHPHtmlDom\Core\PHPHtmlDomLog; |
|
49
|
|
|
$this->dom = new \DOMDocument; |
|
50
|
|
|
$this->importer = new \PHPTools\PHPHtmlDom\Core\PHPHtmlDomImportHtml; |
|
51
|
|
|
$this->html_content = NULL; |
|
52
|
|
|
|
|
53
|
|
|
$this->dom->preserveWhiteSpace = false; |
|
54
|
|
|
$this->dom->validateOnParse = true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Metodo que importa y convierte el contenido html en un objeto DOM. |
|
59
|
|
|
* @param string $text_html Cadena de texto con la url, path, texto html. |
|
60
|
|
|
* @return boolean |
|
61
|
|
|
*/ |
|
62
|
|
|
final public function importHTML($text_html) |
|
63
|
|
|
{ |
|
64
|
|
|
$content = $this->importer->import($text_html); |
|
65
|
|
|
|
|
66
|
|
|
if(!!is_null($content)) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->logger->logError('E001', array($text_html)); |
|
69
|
|
|
} |
|
70
|
|
|
else if(!$content) |
|
71
|
|
|
{ |
|
72
|
|
|
$this->logger->logWarn('W001', array($text_html)); |
|
73
|
|
|
} |
|
74
|
|
|
else |
|
75
|
|
|
{ |
|
76
|
|
|
$this->html_content = $content; |
|
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return !!$this->domImport(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Metodo que permite buscar elementos hijos a partir de un selector css. |
|
84
|
|
|
* @param string $css_selector Cadena de texto con el selector css. |
|
85
|
|
|
* @return PHPTools\PHPHtmlDom\Core\PHPHtmlDomList |
|
86
|
|
|
*/ |
|
87
|
|
|
final public function e($css_selector) |
|
88
|
|
|
{ |
|
89
|
|
|
$xpath = $this->toXPath($css_selector); |
|
90
|
|
|
|
|
91
|
|
|
if(!!$xpath) |
|
|
|
|
|
|
92
|
|
|
{ |
|
93
|
|
|
return new \PHPTools\PHPHtmlDom\Core\PHPHtmlDomList($this->xpath->query($xpath)); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Permite importar el contenido dom del texto html. |
|
99
|
|
|
* @return boolean |
|
100
|
|
|
*/ |
|
101
|
|
|
private function domImport() |
|
102
|
|
|
{ |
|
103
|
|
|
$dom_import = @$this->dom->loadHTML($this->html_content); |
|
104
|
|
|
|
|
105
|
|
|
if(!!$dom_import) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->xpath = new \DOMXPath($this->dom); |
|
108
|
|
|
} |
|
109
|
|
|
else |
|
110
|
|
|
{ |
|
111
|
|
|
$this->logger->logError('E002', array($this->html_content)); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $dom_import; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Metodo que convierte un selector css en un xpath |
|
119
|
|
|
* @param string $css_selector Cadena de texto con el selector css. |
|
120
|
|
|
* @return string|NULL Devuelve una cadena de texto con formato xpath si la converción e sposible o NULL en caso contrario. |
|
121
|
|
|
*/ |
|
122
|
|
|
private function toXPath($css_selector) |
|
123
|
|
|
{ |
|
124
|
|
|
$xpath = Null; |
|
125
|
|
|
|
|
126
|
|
|
try |
|
127
|
|
|
{ |
|
128
|
|
|
$xpath = $this->selector->toXPath($css_selector); |
|
129
|
|
|
} |
|
130
|
|
|
catch (\Exception $e) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->logger->logError('E003', array($css_selector)); |
|
133
|
|
|
$this->logger->logError('E000', array($e->getMessage())); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
return $xpath; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..