1
|
|
|
<?php |
2
|
|
|
namespace PHPTools\PHPHtmlDom\Core; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Esta clase representativa de un objeto DOMElement |
6
|
|
|
*/ |
7
|
|
|
class PHPHtmlDomElement extends \PHPTools\PHPHtmlDom\Core\PHPHtmlDomElementAbstract |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Objeto nativo php que representa un elemento DOM. |
11
|
|
|
* @var \DOMElement |
12
|
|
|
*/ |
13
|
|
|
protected $dom_element; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Nombre de la etiqueta del elemento. |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $tagName; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Texto que contiene el elemento, puede ser un arreglo de texto o una cadena d etexto simple. |
23
|
|
|
* @var array|string |
24
|
|
|
*/ |
25
|
|
|
public $text; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Texto con formato que contiene el elemento, esto abarca todas las etiquetas de estilo de texto. |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
public $textFormatting; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Objeto que contiene los atributos del elemento. |
35
|
|
|
* @var \stdClass |
36
|
|
|
*/ |
37
|
|
|
public $attrs; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Objeto que contiene los elementtos hijos. |
41
|
|
|
* @var \PHPTools\PHPHtmlDom\Core\PHPHtmlDomList |
42
|
|
|
*/ |
43
|
|
|
public $childs; |
44
|
|
|
|
45
|
|
|
public function __construct (\DOMElement $element) |
46
|
|
|
{ |
47
|
|
|
$this->dom_element = $element; |
48
|
|
|
|
49
|
|
|
$this->tagName = $element->tagName; |
50
|
|
|
|
51
|
|
|
$this->text = ''; |
52
|
|
|
|
53
|
|
|
$this->textFormatting = ''; |
54
|
|
|
|
55
|
|
|
$this->get_attrs(); |
56
|
|
|
$this->get_childs(); |
57
|
|
|
$this->get_Text(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Metodo que permite obtener todos atributos del elementos y convertirlos en un objeto stdClass. |
62
|
|
|
* @return self |
63
|
|
|
*/ |
64
|
|
|
private function get_attrs() |
65
|
|
|
{ |
66
|
|
|
$this->attrs = new \stdClass; |
67
|
|
|
|
68
|
|
|
foreach($this->dom_element->attributes as $name => $node) |
69
|
|
|
{ |
70
|
|
|
$this->attrs->{strtolower($name)} = $node->nodeValue; |
71
|
|
|
} |
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Metodo que permite obtener los elementos hijos y convertirlos en un objeto lista PHPHtmlDomList. |
77
|
|
|
* @return self |
78
|
|
|
*/ |
79
|
|
|
private function get_childs() |
80
|
|
|
{ |
81
|
|
|
$this->childs = new \PHPTools\PHPHtmlDom\Core\PHPHtmlDomList($this->dom_element->childNodes); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Metodo que permite obtener el texto que se encuentra dentro del elemento. |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
|
|
private function get_Text() |
91
|
|
|
{ |
92
|
|
|
$text_formatting = array('b','strong','em','i','small','strong','sub','sup','ins','del','mark','br','hr'); |
93
|
|
|
|
94
|
|
|
foreach ($this->dom_element->childNodes as $node) |
95
|
|
|
{ |
96
|
|
|
if($node->nodeType == 3) |
97
|
|
|
{ |
98
|
|
|
$this->set_text(trim($node->textContent)); |
99
|
|
|
$this->textFormatting.=$node->textContent; |
100
|
|
|
} |
101
|
|
|
else if($node->nodeType == 1 && !!in_array($node->tagName, $text_formatting)) |
102
|
|
|
{ |
103
|
|
|
if(!!in_array($node->tagName, ['br','hr'])) |
104
|
|
|
{ |
105
|
|
|
$this->textFormatting.= sprintf('<%s>',$node->tagName); |
106
|
|
|
} |
107
|
|
|
else |
108
|
|
|
{ |
109
|
|
|
$tag = $node->tagName; |
110
|
|
|
$attrs = $this->attrs_to_string($node->attributes); |
111
|
|
|
$text = $node->textContent; |
112
|
|
|
|
113
|
|
|
$this->textFormatting.= sprintf('<%1$s%2$s>%3$s</%1$s>',$tag,$attrs,$text); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Metodo que permite definir e texto del elemento. |
123
|
|
|
* @param string $text Cden de texto a definirse. |
124
|
|
|
* @return self |
125
|
|
|
*/ |
126
|
|
|
private function set_text($text) |
127
|
|
|
{ |
128
|
|
|
if(!!$text) |
129
|
|
|
{ |
130
|
|
|
if(!!$this->text) |
131
|
|
|
{ |
132
|
|
|
if(!!is_array($this->text)) |
133
|
|
|
{ |
134
|
|
|
$this->text[] = $text; |
135
|
|
|
} |
136
|
|
|
else |
137
|
|
|
{ |
138
|
|
|
$this->text = array($this->text,$text); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
else |
142
|
|
|
{ |
143
|
|
|
$this->text = $text; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Este metodo permite concatenar un objeto de atributos en una sola cadena. |
152
|
|
|
* @param attay $attrs Arreglo de atributos. |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
private function attrs_to_string($attrs) |
156
|
|
|
{ |
157
|
|
|
$attrs_string =''; |
158
|
|
|
|
159
|
|
|
foreach($attrs as $name => $node) |
160
|
|
|
{ |
161
|
|
|
$attrs_string.= sprintf(' %s="%s"',$name,$node->nodeValue); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $attrs_string; |
165
|
|
|
} |
166
|
|
|
} |