Skip to content

Commit 968f04a

Browse files
committed
Added magic and each methods to the collection.
Plus some tests
1 parent 4ef869a commit 968f04a

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed

src/PHPHtmlParser/Dom/Collection.php

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22
namespace PHPHtmlParser\Dom;
33

4-
use IteratorAggregate;
5-
use ArrayIterator;
6-
use ArrayAccess;
74
use Countable;
5+
use ArrayAccess;
6+
use ArrayIterator;
7+
use IteratorAggregate;
88

99
class Collection implements IteratorAggregate, ArrayAccess, Countable {
1010

@@ -15,6 +15,54 @@ class Collection implements IteratorAggregate, ArrayAccess, Countable {
1515
*/
1616
protected $collection = [];
1717

18+
/**
19+
* Attempts to call the method on the first node in
20+
* the collection.
21+
*
22+
* @param string $method
23+
* @param array $arguments
24+
* @return mixed;
25+
*/
26+
public function __call($method, $arguments)
27+
{
28+
$node = reset($this->collection);
29+
if ($node instanceof Node)
30+
{
31+
return call_user_func_array([$node, $method], $arguments);
32+
}
33+
}
34+
35+
/**
36+
* Attempts to apply the magic get to the first node
37+
* in the collection.
38+
*
39+
* @param mixed $key
40+
* @return mixed
41+
*/
42+
public function __get($key)
43+
{
44+
$node = reset($this->collection);
45+
if ($node instanceof Node)
46+
{
47+
return $node->$key;
48+
}
49+
}
50+
51+
/**
52+
* Applies the magic string method to the first node in
53+
* the collection.
54+
*
55+
* @return string
56+
*/
57+
public function __toString()
58+
{
59+
$node = reset($this->collection);
60+
if ($node instanceof Node)
61+
{
62+
return (string) $node;
63+
}
64+
}
65+
1866
/**
1967
* Returns the count of the collection.
2068
*
@@ -84,4 +132,16 @@ public function offsetGet($offset)
84132
{
85133
return isset($this->collection[$offset]) ? $this->collection[$offset] : null;
86134
}
135+
136+
/**
137+
* Similar to jQuery "each" method. Calls the callback with each
138+
* Node in this collection.
139+
*/
140+
public function each($callback)
141+
{
142+
foreach ($this->collection as $key => $value)
143+
{
144+
$callback($value, $key);
145+
}
146+
}
87147
}

tests/CollectionTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
use PHPHtmlParser\Selector;
4+
use PHPHtmlParser\Dom\HtmlNode;
5+
use PHPHtmlParser\Dom\Tag;
6+
7+
class CollectionTest extends PHPUnit_Framework_TestCase {
8+
9+
public function testEach()
10+
{
11+
$root = new HtmlNode(new Tag('root'));
12+
$parent = new HtmlNode(new Tag('div'));
13+
$child1 = new HtmlNode(new Tag('a'));
14+
$child2 = new HtmlNode(new Tag('p'));
15+
$child3 = new HtmlNode(new Tag('a'));
16+
$root->addChild($parent);
17+
$parent->addChild($child1);
18+
$parent->addChild($child2);
19+
$child2->addChild($child3);
20+
21+
$selector = new Selector('a');
22+
$collection = $selector->find($root);
23+
$count = 0;
24+
$collection->each(function ($node) use (&$count) {
25+
++$count;
26+
});
27+
$this->assertEquals(2, $count);
28+
}
29+
30+
public function testCallMagic()
31+
{
32+
$root = new HtmlNode(new Tag('root'));
33+
$parent = new HtmlNode(new Tag('div'));
34+
$child1 = new HtmlNode(new Tag('a'));
35+
$child2 = new HtmlNode(new Tag('p'));
36+
$child3 = new HtmlNode(new Tag('a'));
37+
$root->addChild($parent);
38+
$parent->addChild($child1);
39+
$parent->addChild($child2);
40+
$child2->addChild($child3);
41+
42+
$selector = new Selector('div * a');
43+
$this->assertEquals($child3->id(), $selector->find($root)->id());
44+
}
45+
}

0 commit comments

Comments
 (0)