Skip to content

Commit 77d3a9c

Browse files
committed
Refactored the nodes into inner and leaf nodes
1 parent 3c077b2 commit 77d3a9c

File tree

8 files changed

+327
-247
lines changed

8 files changed

+327
-247
lines changed

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 5 additions & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace PHPHtmlParser\Dom;
33

44
use PHPHtmlParser\Selector;
5-
use PHPHtmlParser\Exceptions\ChildNotFoundException;
65
use PHPHtmlParser\Exceptions\CircularException;
76
use PHPHtmlParser\Exceptions\ParentNotFoundException;
87
use stringEncode\Encode;
@@ -31,17 +30,10 @@ abstract class AbstractNode
3130
*/
3231
protected $attr = [];
3332

34-
/**
35-
* An array of all the children.
36-
*
37-
* @var array
38-
*/
39-
protected $children = [];
40-
4133
/**
4234
* Contains the parent Node.
4335
*
44-
* @var AbstractNode
36+
* @var InnerNode
4537
*/
4638
protected $parent = null;
4739

@@ -133,17 +125,12 @@ public function getParent()
133125
/**
134126
* Sets the parent node.
135127
*
136-
* @param AbstractNode $parent
128+
* @param InnerNode $parent
137129
* @return $this
138130
* @throws CircularException
139131
*/
140-
public function setParent(AbstractNode $parent)
132+
public function setParent(InnerNode $parent)
141133
{
142-
// check integrity
143-
if ($this->isDescendant($parent->id())) {
144-
throw new CircularException('Can not add descendant "'.$parent->id().'" as my parent.');
145-
}
146-
147134
// remove from old parent
148135
if ( ! is_null($this->parent)) {
149136
if ($this->parent->id() == $parent->id()) {
@@ -166,231 +153,15 @@ public function setParent(AbstractNode $parent)
166153
}
167154

168155
/**
169-
* Sets the encoding class to this node and propagates it
170-
* to all its children.
156+
* Sets the encoding class to this node.
171157
*
172158
* @param Encode $encode
159+
* @return void
173160
*/
174161
public function propagateEncoding(Encode $encode)
175162
{
176163
$this->encode = $encode;
177164
$this->tag->setEncoding($encode);
178-
// check children
179-
foreach ($this->children as $id => $child) {
180-
/** @var AbstractNode $node */
181-
$node = $child['node'];
182-
$node->propagateEncoding($encode);
183-
}
184-
}
185-
186-
/**
187-
* Checks if this node has children.
188-
*
189-
* @return bool
190-
*/
191-
public function hasChildren()
192-
{
193-
return ! empty($this->children);
194-
}
195-
196-
/**
197-
* Returns the child by id.
198-
*
199-
* @param int $id
200-
* @return AbstractNode
201-
* @throws ChildNotFoundException
202-
*/
203-
public function getChild($id)
204-
{
205-
if ( ! isset($this->children[$id])) {
206-
throw new ChildNotFoundException("Child '$id' not found in this node.");
207-
}
208-
209-
return $this->children[$id]['node'];
210-
}
211-
212-
/**
213-
* Returns a new array of child nodes
214-
*
215-
* @return array
216-
*/
217-
public function getChildren()
218-
{
219-
$nodes = [];
220-
try {
221-
$child = $this->firstChild();
222-
do {
223-
$nodes[] = $child;
224-
$child = $this->nextChild($child->id());
225-
} while ( ! is_null($child));
226-
} catch (ChildNotFoundException $e) {
227-
// we are done looking for children
228-
}
229-
230-
return $nodes;
231-
}
232-
233-
/**
234-
* Counts children
235-
*
236-
* @return int
237-
*/
238-
public function countChildren()
239-
{
240-
return count($this->children);
241-
}
242-
243-
/**
244-
* Adds a child node to this node and returns the id of the child for this
245-
* parent.
246-
*
247-
* @param AbstractNode $child
248-
* @return bool
249-
* @throws CircularException
250-
*/
251-
public function addChild(AbstractNode $child)
252-
{
253-
$key = null;
254-
255-
// check integrity
256-
if ($this->isAncestor($child->id())) {
257-
throw new CircularException('Can not add child. It is my ancestor.');
258-
}
259-
260-
// check if child is itself
261-
if ($child->id() == $this->id) {
262-
throw new CircularException('Can not set itself as a child.');
263-
}
264-
265-
if ($this->hasChildren()) {
266-
if (isset($this->children[$child->id()])) {
267-
// we already have this child
268-
return false;
269-
}
270-
$sibling = $this->lastChild();
271-
$key = $sibling->id();
272-
$this->children[$key]['next'] = $child->id();
273-
}
274-
275-
// add the child
276-
$this->children[$child->id()] = [
277-
'node' => $child,
278-
'next' => null,
279-
'prev' => $key,
280-
];
281-
282-
// tell child I am the new parent
283-
$child->setParent($this);
284-
285-
//clear any cache
286-
$this->clear();
287-
288-
return true;
289-
}
290-
291-
/**
292-
* Removes the child by id.
293-
*
294-
* @param int $id
295-
* @return $this
296-
*/
297-
public function removeChild($id)
298-
{
299-
if ( ! isset($this->children[$id])) {
300-
return $this;
301-
}
302-
303-
// handle moving next and previous assignments.
304-
$next = $this->children[$id]['next'];
305-
$prev = $this->children[$id]['prev'];
306-
if ( ! is_null($next)) {
307-
$this->children[$next]['prev'] = $prev;
308-
}
309-
if ( ! is_null($prev)) {
310-
$this->children[$prev]['next'] = $next;
311-
}
312-
313-
// remove the child
314-
unset($this->children[$id]);
315-
316-
//clear any cache
317-
$this->clear();
318-
319-
return $this;
320-
}
321-
322-
/**
323-
* Attempts to get the next child.
324-
*
325-
* @param int $id
326-
* @return AbstractNode
327-
* @uses $this->getChild()
328-
*/
329-
public function nextChild($id)
330-
{
331-
$child = $this->getChild($id);
332-
$next = $this->children[$child->id()]['next'];
333-
334-
return $this->getChild($next);
335-
}
336-
337-
/**
338-
* Attempts to get the previous child.
339-
*
340-
* @param int $id
341-
* @return AbstractNode
342-
* @uses $this->getChild()
343-
*/
344-
public function previousChild($id)
345-
{
346-
$child = $this->getchild($id);
347-
$next = $this->children[$child->id()]['prev'];
348-
349-
return $this->getChild($next);
350-
}
351-
352-
/**
353-
* Checks if the given node id is a child of the
354-
* current node.
355-
*
356-
* @param int $id
357-
* @return bool
358-
*/
359-
public function isChild($id)
360-
{
361-
foreach ($this->children as $childId => $child) {
362-
if ($id == $childId) {
363-
return true;
364-
}
365-
}
366-
367-
return false;
368-
}
369-
370-
/**
371-
* Checks if the given node id is a descendant of the
372-
* current node.
373-
*
374-
* @param int $id
375-
* @return bool
376-
*/
377-
public function isDescendant($id)
378-
{
379-
if ($this->isChild($id)) {
380-
return true;
381-
}
382-
383-
foreach ($this->children as $childId => $child) {
384-
/** @var AbstractNode $node */
385-
$node = $child['node'];
386-
if ($node->hasChildren() &&
387-
$node->isDescendant($id)
388-
) {
389-
return true;
390-
}
391-
}
392-
393-
return false;
394165
}
395166

396167
/**

src/PHPHtmlParser/Dom/ArrayNode.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@
1212
abstract class ArrayNode extends AbstractNode implements IteratorAggregate, Countable
1313
{
1414

15-
/**
16-
* Returns the array to be used the the iterator.
17-
*
18-
* @return array
19-
*/
20-
abstract protected function getIteratorArray();
21-
2215
/**
2316
* Gets the iterator
2417
*
@@ -39,4 +32,10 @@ public function count()
3932
return count($this->getIteratorArray());
4033
}
4134

35+
/**
36+
* Returns the array to be used the the iterator.
37+
*
38+
* @return array
39+
*/
40+
abstract protected function getIteratorArray();
4241
}

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @package PHPHtmlParser\Dom
1111
*/
12-
class HtmlNode extends ArrayNode
12+
class HtmlNode extends InnerNode
1313
{
1414

1515
/**

0 commit comments

Comments
 (0)