|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1);
|
|
4
|
|
|
|
|
5
|
|
|
namespace PHPHtmlParser\Dom;
|
|
6
|
|
|
|
|
7
|
|
|
use PHPHtmlParser\Dom\Node\HtmlNode;
|
|
8
|
|
|
use PHPHtmlParser\Dom\Node\AbstractNode;
|
|
9
|
|
|
use PHPHtmlParser\Exceptions\ChildNotFoundException;
|
|
10
|
|
|
use PHPHtmlParser\Exceptions\NotLoadedException;
|
|
11
|
|
|
|
|
12
|
|
|
trait RootAccessTrait
|
|
13
|
|
|
{
|
|
14
|
|
|
/**
|
|
15
|
|
|
* Contains the root node of this dom tree.
|
|
16
|
|
|
*
|
|
17
|
|
|
* @var HtmlNode
|
|
18
|
|
|
*/
|
|
19
|
|
|
public $root;
|
|
20
|
|
|
|
|
21
|
|
|
/**
|
|
22
|
|
|
* A simple wrapper around the root node.
|
|
23
|
|
|
*
|
|
24
|
|
|
* @param string $name
|
|
25
|
|
|
*
|
|
26
|
|
|
* @throws NotLoadedException
|
|
27
|
|
|
*
|
|
28
|
|
|
* @return mixed
|
|
29
|
|
|
*/
|
|
30
|
27 |
|
public function __get($name)
|
|
31
|
|
|
{
|
|
32
|
27 |
|
$this->isLoaded();
|
|
33
|
|
|
|
|
34
|
27 |
|
return $this->root->$name;
|
|
35
|
|
|
}
|
|
36
|
|
|
|
|
37
|
|
|
/**
|
|
38
|
|
|
* Simple wrapper function that returns the first child.
|
|
39
|
|
|
*
|
|
40
|
|
|
* @throws ChildNotFoundException
|
|
41
|
|
|
* @throws NotLoadedException
|
|
42
|
|
|
*/
|
|
43
|
6 |
|
public function firstChild(): AbstractNode
|
|
44
|
|
|
{
|
|
45
|
6 |
|
$this->isLoaded();
|
|
46
|
|
|
|
|
47
|
6 |
|
return $this->root->firstChild();
|
|
48
|
|
|
}
|
|
49
|
|
|
|
|
50
|
|
|
/**
|
|
51
|
|
|
* Simple wrapper function that returns the last child.
|
|
52
|
|
|
*
|
|
53
|
|
|
* @throws ChildNotFoundException
|
|
54
|
|
|
* @throws NotLoadedException
|
|
55
|
|
|
*/
|
|
56
|
3 |
|
public function lastChild(): AbstractNode
|
|
57
|
|
|
{
|
|
58
|
3 |
|
$this->isLoaded();
|
|
59
|
|
|
|
|
60
|
3 |
|
return $this->root->lastChild();
|
|
61
|
|
|
}
|
|
62
|
|
|
|
|
63
|
|
|
/**
|
|
64
|
|
|
* Simple wrapper function that returns count of child elements.
|
|
65
|
|
|
*
|
|
66
|
|
|
* @throws NotLoadedException
|
|
67
|
|
|
*/
|
|
68
|
3 |
|
public function countChildren(): int
|
|
69
|
|
|
{
|
|
70
|
3 |
|
$this->isLoaded();
|
|
71
|
|
|
|
|
72
|
3 |
|
return $this->root->countChildren();
|
|
73
|
|
|
}
|
|
74
|
|
|
|
|
75
|
|
|
/**
|
|
76
|
|
|
* Get array of children.
|
|
77
|
|
|
*
|
|
78
|
|
|
* @throws NotLoadedException
|
|
79
|
|
|
*/
|
|
80
|
3 |
|
public function getChildren(): array
|
|
81
|
|
|
{
|
|
82
|
3 |
|
$this->isLoaded();
|
|
83
|
|
|
|
|
84
|
3 |
|
return $this->root->getChildren();
|
|
85
|
|
|
}
|
|
86
|
|
|
|
|
87
|
|
|
/**
|
|
88
|
|
|
* Check if node have children nodes.
|
|
89
|
|
|
*
|
|
90
|
|
|
* @throws NotLoadedException
|
|
91
|
|
|
*/
|
|
92
|
3 |
|
public function hasChildren(): bool
|
|
93
|
|
|
{
|
|
94
|
3 |
|
$this->isLoaded();
|
|
95
|
|
|
|
|
96
|
3 |
|
return $this->root->hasChildren();
|
|
97
|
|
|
}
|
|
98
|
|
|
|
|
99
|
|
|
abstract function isLoaded(): void;
|
|
|
|
|
|
|
100
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.