Skip to content

Commit fbab2e1

Browse files
committed
Added a toArray() method to the collection and a test
1 parent dbaf2ca commit fbab2e1

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ You can also mount a static facade for the Dom object.
6666
PHPHtmlParser\StaticDom::mount();
6767

6868
Dom::load('tests/big.hmtl');
69-
$objects Dom::find('.content-border');
69+
$objects = Dom::find('.content-border');
7070

7171
```
7272

src/PHPHtmlParser/Dom/Collection.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,21 @@ public function offsetGet($offset)
133133
return isset($this->collection[$offset]) ? $this->collection[$offset] : null;
134134
}
135135

136+
/**
137+
* Returns this collection as an array.
138+
*
139+
* @return array
140+
*/
141+
public function toArray()
142+
{
143+
return $this->collection;
144+
}
145+
136146
/**
137147
* Similar to jQuery "each" method. Calls the callback with each
138148
* Node in this collection.
149+
*
150+
* @param callback $callback
139151
*/
140152
public function each($callback)
141153
{

tests/CollectionTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,23 @@ public function testCallMagic()
4242
$selector = new Selector('div * a');
4343
$this->assertEquals($child3->id(), $selector->find($root)->id());
4444
}
45+
46+
public function testToArray()
47+
{
48+
$root = new HtmlNode(new Tag('root'));
49+
$parent = new HtmlNode(new Tag('div'));
50+
$child1 = new HtmlNode(new Tag('a'));
51+
$child2 = new HtmlNode(new Tag('p'));
52+
$child3 = new HtmlNode(new Tag('a'));
53+
$root->addChild($parent);
54+
$parent->addChild($child1);
55+
$parent->addChild($child2);
56+
$child2->addChild($child3);
57+
58+
$selector = new Selector('a');
59+
$collection = $selector->find($root);
60+
$array = $collection->toArray();
61+
$lastA = end($array);
62+
$this->assertEquals($child3->id(), $lastA->id());
63+
}
4564
}

0 commit comments

Comments
 (0)