Skip to content

Commit d1fb03a

Browse files
committed
1 parent 2e59a97 commit d1fb03a

File tree

10 files changed

+606
-16
lines changed

10 files changed

+606
-16
lines changed

autoload.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,14 @@ function dd(...$data) {
1818
}
1919
}
2020

21-
function d(...$data) {return;
22-
$GLOBALS['X'] = $GLOBALS['X'] ?? 0;
23-
$GLOBALS['X']++;
24-
$GLOBALS['X'] > 100 && die();
25-
//$x = debug_backtrace();
26-
//print_r($x[0]['file'].':'.$x[0]['line'].PHP_EOL);
21+
function d(...$data) {
22+
$x = debug_backtrace();
23+
print_r($x[0]['file'].':'.$x[0]['line'].PHP_EOL);
2724
foreach ($data as $d) {
2825
//var_dump($d);
29-
//echo '<pre>';
26+
echo '<pre>';
3027
print_r($d);
31-
//echo '</pre>';
28+
echo '</pre>';
3229
echo PHP_EOL;
3330
}
3431
}

src/DomNode.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
use PhpTemplates\Dom\Contracts\DomNodeAttrInterface;
66
use PhpTemplates\Dom\Contracts\DomNodeInterface;
77
use PhpTemplates\Dom\Traits\DomElement;
8+
use PhpTemplates\Dom\Traits\QuerySelector;
89

910
/**
1011
* @inheritdoc
1112
*/
1213
class DomNode implements DomNodeInterface
1314
{
1415
use DomElement;
16+
use QuerySelector;
1517

1618
/**
1719
* DomNodeElement name, like input, textarea, div, etc.
@@ -86,8 +88,8 @@ public function getAttributes(): array
8688
public function getAttribute(string $name)
8789
{
8890
foreach ($this->attrs as $attr) {
89-
if ($attr->name == $name) {
90-
return $attr->value;
91+
if ($attr->getName() == $name) {
92+
return $attr->getValue();
9193
}
9294
}
9395
}
@@ -103,8 +105,8 @@ public function setAttribute($name, $value = null): DomNodeInterface
103105
}
104106

105107
foreach ($this->attrs as $attr) {
106-
if ($attr->name == $name) {
107-
$attr->value = $value;
108+
if ($attr->getName() == $name) {
109+
$attr->setValue($value);
108110

109111
return $this;
110112
}
@@ -119,7 +121,7 @@ public function setAttribute($name, $value = null): DomNodeInterface
119121
public function hasAttribute(string $name): bool
120122
{
121123
foreach ($this->attrs as $attr) {
122-
if ($attr->name == $name) {
124+
if ($attr->getName() == $name) {
123125
return true;
124126
}
125127
}
@@ -129,7 +131,7 @@ public function hasAttribute(string $name): bool
129131
public function removeAttribute(string $name): self
130132
{
131133
foreach ($this->attrs as $i => $attr) {
132-
if ($attr->name == $name) {
134+
if ($attr->getName() == $name) {
133135
unset($this->attrs[$i]);
134136
}
135137
}

src/DomNodeIterator.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class DomNodeIterator implements \Iterator, DomElementInterface
1212

1313
public function rewind()
1414
{
15+
$this->i = 0;
1516
$this->current = $this->head;
1617
}
1718

@@ -53,7 +54,7 @@ public function item(int $index): ?DomElementInterface
5354
}
5455

5556
$item = $this->head;
56-
for ($i = 1; $i <= $index; $i++) {
57+
for ($i = 0; $i < $index; $i++) {
5758
$item = $item->getNextSibling();
5859
}
5960

src/DomNodeList.php

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
<?php
2+
3+
namespace PhpTemplates\Dom;
4+
5+
use PhpTemplates\Dom\Contracts\DomElementInterface;
6+
7+
class DomNodeList implements \Iterator, DomElementInterface
8+
{
9+
private int $i = 0;
10+
private array $list = [];
11+
12+
public function rewind()
13+
{
14+
$this->i = 0;
15+
}
16+
17+
public function current()
18+
{
19+
return $this->list[$this->i];
20+
}
21+
22+
public function key()
23+
{
24+
return $this->i;
25+
}
26+
27+
public function next()
28+
{
29+
$this->i++;
30+
}
31+
32+
public function valid()
33+
{
34+
return isset($this->list[$this->i]);
35+
}
36+
37+
public function push(DomElementInterface $node)
38+
{
39+
$this->list[] = $node;
40+
41+
return $this;
42+
}
43+
44+
public function first(): ?DomElementInterface
45+
{
46+
if (!$this->list) {
47+
return null;
48+
}
49+
50+
return reset($this->list);
51+
}
52+
53+
public function item(int $index): ?DomElementInterface
54+
{
55+
return $this->list[$index] ?? null;
56+
}
57+
58+
public function last(): ?DomElementInterface
59+
{
60+
if (!$this->list) {
61+
return null;
62+
}
63+
64+
return end($this->list);
65+
}
66+
67+
public function count(): int
68+
{
69+
return count($this->list);
70+
}
71+
72+
public function __toString(): string
73+
{
74+
$output = '';
75+
foreach ($this->list as $node) {
76+
$output .= $node;
77+
}
78+
79+
return $output;
80+
}
81+
82+
public function getParentNode(): ?DomElementInterface
83+
{
84+
if (isset($this->list[0])) {
85+
return $this->list[0]->getParentNode();
86+
}
87+
}
88+
89+
public function getPrevSibling(): ?DomElementInterface
90+
{
91+
if (isset($this->list[0])) {
92+
return $this->list[0]->getPrevSibling();
93+
}
94+
}
95+
96+
public function getNextSibling(): ?DomElementInterface
97+
{
98+
if (isset($this->list[0])) {
99+
return $this->list[0]->getNextSibling();
100+
}
101+
}
102+
103+
public function getChildNodes(): DomNodeIterator
104+
{
105+
if (isset($this->list[0])) {
106+
return $this->list[0]->getChildNodes();
107+
}
108+
109+
return new DomNodeIterator;
110+
}
111+
112+
public function setParentNode(DomElementInterface $node = null): DomElementInterface
113+
{
114+
foreach ($this->list as $elNode) {
115+
$elNode->setParentNode($node);
116+
}
117+
118+
return $this;
119+
}
120+
121+
public function setPrevSibling(DomElementInterface $node = null): DomElementInterface
122+
{
123+
foreach ($this->list as $elNode) {
124+
$elNode->setPrevSibling($node);
125+
}
126+
127+
return $this;
128+
}
129+
130+
public function setNextSibling(DomElementInterface $node = null): DomElementInterface
131+
{
132+
foreach ($this->list as $elNode) {
133+
$elNode->setNextSibling($node);
134+
}
135+
136+
return $this;
137+
}
138+
139+
public function appendChild(DomElementInterface $node): DomElementInterface
140+
{
141+
foreach ($this->list as $elNode) {
142+
$elNode->appendChild($node);
143+
}
144+
145+
return $this;
146+
}
147+
148+
public function prependChild(DomElementInterface $node): DomElementInterface
149+
{
150+
foreach ($this->list as $elNode) {
151+
$elNode->prependChild($node);
152+
}
153+
154+
return $this;
155+
}
156+
157+
public function insertBefore(DomElementInterface $node): DomElementInterface
158+
{
159+
foreach ($this->list as $elNode) {
160+
$elNode->insertBefore($node);
161+
}
162+
163+
return $this;
164+
}
165+
166+
public function appendTo(DomElementInterface $node): DomElementInterface
167+
{
168+
foreach ($this->list as $elNode) {
169+
$elNode->appendTo($node);
170+
}
171+
172+
return $this;
173+
}
174+
175+
public function insertAfter(DomElementInterface $node): DomElementInterface
176+
{
177+
foreach ($this->list as $elNode) {
178+
$elNode->insertAfter($node);
179+
}
180+
181+
return $this;
182+
}
183+
184+
public function before(DomElementInterface $node): DomElementInterface
185+
{
186+
foreach ($this->list as $elNode) {
187+
$elNode->before($node);
188+
}
189+
190+
return $this;
191+
}
192+
193+
public function after(DomElementInterface $node): DomElementInterface
194+
{
195+
foreach ($this->list as $elNode) {
196+
$elNode->after($node);
197+
}
198+
199+
return $this;
200+
}
201+
202+
public function detach(): DomElementInterface
203+
{
204+
foreach ($this->list as $elNode) {
205+
$elNode->detach();
206+
}
207+
208+
return $this;
209+
}
210+
}

0 commit comments

Comments
 (0)