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