Skip to content

Commit 4ef869a

Browse files
committed
Added a collection object
1 parent 6045c90 commit 4ef869a

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
namespace PHPHtmlParser\Dom;
3+
4+
use IteratorAggregate;
5+
use ArrayIterator;
6+
use ArrayAccess;
7+
use Countable;
8+
9+
class Collection implements IteratorAggregate, ArrayAccess, Countable {
10+
11+
/**
12+
* The collection of Nodes.
13+
*
14+
* @param array
15+
*/
16+
protected $collection = [];
17+
18+
/**
19+
* Returns the count of the collection.
20+
*
21+
* @return int
22+
*/
23+
public function count()
24+
{
25+
return count($this->collection);
26+
}
27+
28+
/**
29+
* Returns an iterator for the collection.
30+
*
31+
* @return ArrayIterator
32+
*/
33+
public function getIterator()
34+
{
35+
return ArrayIterator($this->collection);
36+
}
37+
38+
/**
39+
* Set an attribute by the given offset
40+
*
41+
* @param mixed $offset
42+
* @param mixed $value
43+
*/
44+
public function offsetSet($offset, $value)
45+
{
46+
if (is_null($offset))
47+
{
48+
$this->collection[] = $value;
49+
}
50+
else
51+
{
52+
$this->collection[$offset] = $value;
53+
}
54+
}
55+
56+
/**
57+
* Checks if an offset exists.
58+
*
59+
* @param mixed $offset
60+
* @return bool
61+
*/
62+
public function offsetExists($offset)
63+
{
64+
return isset($this->collection[$offset]);
65+
}
66+
67+
/**
68+
* Unset a collection Node.
69+
*
70+
* @param mixed $offset
71+
*/
72+
public function offsetUnset($offset)
73+
{
74+
unset($this->collection[$offset]);
75+
}
76+
77+
/**
78+
* Gets a node at the given offset, or null
79+
*
80+
* @param mixed $offset
81+
* @return $offset
82+
*/
83+
public function offsetGet($offset)
84+
{
85+
return isset($this->collection[$offset]) ? $this->collection[$offset] : null;
86+
}
87+
}

src/PHPHtmlParser/Selector.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PHPHtmlParser;
33

4+
use PHPHtmlParser\Dom\Collection;
5+
46
class Selector {
57

68
/**
@@ -41,7 +43,7 @@ public function getSelectors()
4143
*/
4244
public function find($node)
4345
{
44-
$results = [];
46+
$results = new Collection;
4547
foreach ($this->selectors as $selector)
4648
{
4749
$nodes = [$node];

0 commit comments

Comments
 (0)