1
|
|
|
<?php |
2
|
|
|
namespace PHPHtmlParser\Dom; |
3
|
|
|
|
4
|
|
|
use ArrayAccess; |
5
|
|
|
use ArrayIterator; |
6
|
|
|
use Countable; |
7
|
|
|
use IteratorAggregate; |
8
|
|
|
use PHPHtmlParser\Exceptions\EmptyCollectionException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Collection |
12
|
|
|
* |
13
|
|
|
* @package PHPHtmlParser\Dom |
14
|
|
|
*/ |
15
|
|
|
class Collection implements IteratorAggregate, ArrayAccess, Countable |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The collection of Nodes. |
20
|
|
|
* |
21
|
|
|
* @param array |
22
|
|
|
*/ |
23
|
|
|
protected $collection = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Attempts to call the method on the first node in |
27
|
|
|
* the collection. |
28
|
|
|
* |
29
|
|
|
* @param string $method |
30
|
|
|
* @param array $arguments |
31
|
|
|
* @return mixed |
32
|
|
|
* @throws EmptyCollectionException |
33
|
|
|
*/ |
34
|
12 |
|
public function __call(string $method, array $arguments) |
35
|
|
|
{ |
36
|
12 |
|
$node = reset($this->collection); |
37
|
12 |
|
if ($node instanceof AbstractNode) { |
38
|
9 |
|
return call_user_func_array([$node, $method], $arguments); |
39
|
|
|
} else { |
40
|
3 |
|
throw new EmptyCollectionException('The collection does not contain any Nodes.'); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Attempts to apply the magic get to the first node |
46
|
|
|
* in the collection. |
47
|
|
|
* |
48
|
|
|
* @param mixed $key |
49
|
|
|
* @return mixed |
50
|
|
|
* @throws EmptyCollectionException |
51
|
|
|
*/ |
52
|
6 |
|
public function __get($key) |
53
|
|
|
{ |
54
|
6 |
|
$node = reset($this->collection); |
55
|
6 |
|
if ($node instanceof AbstractNode) { |
56
|
3 |
|
return $node->$key; |
57
|
|
|
} else { |
58
|
3 |
|
throw new EmptyCollectionException('The collection does not contain any Nodes.'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Applies the magic string method to the first node in |
64
|
|
|
* the collection. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
* @throws EmptyCollectionException |
68
|
|
|
*/ |
69
|
6 |
|
public function __toString(): string |
70
|
|
|
{ |
71
|
6 |
|
$node = reset($this->collection); |
72
|
6 |
|
if ($node instanceof AbstractNode) { |
73
|
3 |
|
return (string)$node; |
74
|
|
|
} else { |
75
|
3 |
|
return ''; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the count of the collection. |
81
|
|
|
* |
82
|
|
|
* @return int |
83
|
|
|
*/ |
84
|
39 |
|
public function count(): int |
85
|
|
|
{ |
86
|
39 |
|
return count($this->collection); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Returns an iterator for the collection. |
91
|
|
|
* |
92
|
|
|
* @return ArrayIterator |
93
|
|
|
*/ |
94
|
3 |
|
public function getIterator(): ArrayIterator |
95
|
|
|
{ |
96
|
3 |
|
return new ArrayIterator($this->collection); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set an attribute by the given offset |
101
|
|
|
* |
102
|
|
|
* @param mixed $offset |
103
|
|
|
* @param mixed $value |
104
|
|
|
*/ |
105
|
198 |
|
public function offsetSet($offset, $value): void |
106
|
|
|
{ |
107
|
198 |
|
if (is_null($offset)) { |
108
|
192 |
|
$this->collection[] = $value; |
109
|
|
|
} else { |
110
|
6 |
|
$this->collection[$offset] = $value; |
111
|
|
|
} |
112
|
198 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checks if an offset exists. |
116
|
|
|
* |
117
|
|
|
* @param mixed $offset |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
189 |
|
public function offsetExists($offset): bool |
121
|
|
|
{ |
122
|
189 |
|
return isset($this->collection[$offset]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Unset a collection Node. |
127
|
|
|
* |
128
|
|
|
* @param mixed $offset |
129
|
|
|
*/ |
130
|
3 |
|
public function offsetUnset($offset): void |
131
|
|
|
{ |
132
|
3 |
|
unset($this->collection[$offset]); |
133
|
3 |
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Gets a node at the given offset, or null |
137
|
|
|
* |
138
|
|
|
* @param mixed $offset |
139
|
|
|
* @return mixed |
140
|
|
|
*/ |
141
|
165 |
|
public function offsetGet($offset) |
142
|
|
|
{ |
143
|
165 |
|
return isset($this->collection[$offset]) ? $this->collection[$offset] : null; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns this collection as an array. |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
3 |
|
public function toArray(): array |
152
|
|
|
{ |
153
|
3 |
|
return $this->collection; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Similar to jQuery "each" method. Calls the callback with each |
158
|
|
|
* Node in this collection. |
159
|
|
|
* |
160
|
|
|
* @param callable $callback |
161
|
|
|
*/ |
162
|
3 |
|
public function each(callable $callback) |
163
|
|
|
{ |
164
|
3 |
|
foreach ($this->collection as $key => $value) { |
165
|
3 |
|
$callback($value, $key); |
166
|
|
|
} |
167
|
3 |
|
} |
168
|
|
|
} |
169
|
|
|
|