1 | <?php |
||
2 | /** |
||
3 | * This file is part of the O2System Framework package. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @author Steeve Andrian Salim |
||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
10 | */ |
||
11 | |||
12 | // ------------------------------------------------------------------------ |
||
13 | |||
14 | namespace O2System\Html\Element; |
||
15 | |||
16 | // ------------------------------------------------------------------------ |
||
17 | |||
18 | use O2System\Html\Element; |
||
19 | use O2System\Spl\Iterators\ArrayIterator; |
||
20 | |||
21 | /** |
||
22 | * Class Nodes |
||
23 | * |
||
24 | * @package O2System\Html\Element |
||
25 | */ |
||
26 | class Nodes extends ArrayIterator |
||
27 | { |
||
28 | /** |
||
29 | * Nodes::$nodesEntities |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | private $nodesEntities = []; |
||
34 | |||
35 | // ------------------------------------------------------------------------ |
||
36 | |||
37 | /** |
||
38 | * Nodes::createNode |
||
39 | * |
||
40 | * @param string $tagName |
||
41 | * @param string|null $entityName |
||
42 | * |
||
43 | * @return mixed |
||
44 | */ |
||
45 | public function createNode($tagName, $entityName = null) |
||
46 | { |
||
47 | if ($tagName instanceof Element) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
48 | $this->push($tagName); |
||
49 | } else { |
||
50 | $this->push(new Element($tagName, $entityName)); |
||
51 | } |
||
52 | |||
53 | return $this->last(); |
||
54 | } |
||
55 | |||
56 | // ------------------------------------------------------------------------ |
||
57 | |||
58 | /** |
||
59 | * Nodes::push |
||
60 | * |
||
61 | * @param mixed $value |
||
62 | */ |
||
63 | public function push($value) |
||
64 | { |
||
65 | parent::push($value); |
||
66 | $this->nodesEntities[] = $this->last()->entity->getEntityName(); |
||
67 | } |
||
68 | |||
69 | // ------------------------------------------------------------------------ |
||
70 | |||
71 | /** |
||
72 | * Nodes::hasNode |
||
73 | * |
||
74 | * @param string $index |
||
75 | * |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function hasNode($index) |
||
79 | { |
||
80 | if (is_string($index) and in_array($index, $this->nodesEntities)) { |
||
81 | if (false !== ($key = array_search($index, $this->nodesEntities))) { |
||
82 | if ($this->offsetExists($key)) { |
||
83 | return true; |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | |||
88 | return false; |
||
89 | } |
||
90 | |||
91 | // ------------------------------------------------------------------------ |
||
92 | |||
93 | /** |
||
94 | * Nodes::getNode |
||
95 | * |
||
96 | * @param string $index |
||
97 | * |
||
98 | * @return bool|mixed |
||
99 | */ |
||
100 | public function getNode($index) |
||
101 | { |
||
102 | if (is_string($index) and in_array($index, $this->nodesEntities)) { |
||
103 | if (false !== ($key = array_search($index, $this->nodesEntities))) { |
||
104 | if ($this->offsetExists($key)) { |
||
105 | return $this->offsetGet($index); |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | |||
110 | return false; |
||
111 | } |
||
112 | |||
113 | // ------------------------------------------------------------------------ |
||
114 | |||
115 | /** |
||
116 | * Nodes::item |
||
117 | * |
||
118 | * @param string $index |
||
119 | * |
||
120 | * @return mixed |
||
121 | */ |
||
122 | public function item($index) |
||
123 | { |
||
124 | if($this->offsetExists($index)) { |
||
125 | return $this->offsetGet($index); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | // ------------------------------------------------------------------------ |
||
130 | |||
131 | /** |
||
132 | * Nodes::prepend |
||
133 | * |
||
134 | * @param mixed $value |
||
135 | */ |
||
136 | public function prepend($value) |
||
137 | { |
||
138 | parent::unshift($value); |
||
139 | } |
||
140 | |||
141 | // ------------------------------------------------------------------------ |
||
142 | |||
143 | /** |
||
144 | * Nodes::getNodeByTagName |
||
145 | * |
||
146 | * @param string $tagName |
||
147 | * |
||
148 | * @return array |
||
149 | */ |
||
150 | public function getNodeByTagName($tagName) |
||
151 | { |
||
152 | $result = []; |
||
153 | |||
154 | foreach ($this as $node) { |
||
155 | if ($node->tagName === $tagName) { |
||
156 | $result[] = $node; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | return $result; |
||
161 | } |
||
162 | |||
163 | // ------------------------------------------------------------------------ |
||
164 | |||
165 | /** |
||
166 | * Nodes::getNodeByEntityName |
||
167 | * |
||
168 | * @param string $entityName |
||
169 | * |
||
170 | * @return bool|mixed |
||
171 | */ |
||
172 | public function getNodeByEntityName($entityName) |
||
173 | { |
||
174 | if (false !== ($index = array_search($entityName, $this->nodesEntities))) { |
||
175 | if ($this->offsetExists($index)) { |
||
176 | return $this->offsetGet($index); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | return false; |
||
181 | } |
||
182 | } |