Skip to content

Commit

Permalink
Merge pull request #123 from PhpGt/122-iterator-count
Browse files Browse the repository at this point in the history
Iterators are now countable
  • Loading branch information
g105b authored Apr 17, 2018
2 parents 636f6a4 + e34d75e commit 343babc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
22 changes: 12 additions & 10 deletions src/NodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
class NodeList implements Iterator, ArrayAccess, Countable {
use LiveProperty;

private $domNodeList;
protected $domNodeList;
protected $iteratorKey;

public function __construct(DOMNodeList $domNodeList) {
$this->domNodeList = $domNodeList;
Expand All @@ -21,11 +22,14 @@ public function __construct(DOMNodeList $domNodeList) {
* @return int Number of Elements
*/
private function prop_get_length():int {
$key = $this->iteratorKey;

$length = 0;
foreach($this as $element) {
$length++;
}

$this->iteratorKey = $key;
return $length;
}

Expand Down Expand Up @@ -53,34 +57,32 @@ public function item($index) {

// Iterator --------------------------------------------------------------------

private $key = 0;

public function current():Element {
return $this->domNodeList[$this->key];
return $this->domNodeList[$this->iteratorKey];
}

public function key():int {
return $this->key;
return $this->iteratorKey;
}

public function next() {
$this->key++;
$this->iteratorKey++;
$this->incrementKeyToNextElement();
}

public function rewind() {
$this->key = 0;
$this->iteratorKey = 0;
$this->incrementKeyToNextElement();
}

public function valid():bool {
return isset($this->domNodeList[$this->key]);
return isset($this->domNodeList[$this->iteratorKey]);
}

private function incrementKeyToNextElement() {
while($this->valid()
&& !$this->domNodeList[$this->key] instanceof Element) {
$this->key++;
&& !$this->domNodeList[$this->iteratorKey] instanceof Element) {
$this->iteratorKey++;
}
}

Expand Down
17 changes: 14 additions & 3 deletions test/unit/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ class Helper {
</body>
</html>
HTML;

const HTML_SECTIONS_WITHIN_FORM = <<<HTML
<!doctype html>
<form id="example-form">
<section><h1>Section 1</h1></section>
<section><h1>Section 2</h1></section>
<section><h1>Section 3</h1></section>
<section><h1>Section 4</h1></section>
</form>
HTML;

const HTML_TEXT = <<<HTML
<p>Thru-hiking is great! <strong>No insipid election coverage!</strong>
However, <a href="http://en.wikipedia.org/wiki/Absentee_ballot">casting a
Expand Down Expand Up @@ -170,9 +181,9 @@ class Helper {
</breakfast-menu>
XML;

/**
* Below this line is all code that is used in documentation.
*/
/**
* Below this line is all code that is used in documentation.
*/

// https://github.com/PhpGt/Dom/wiki/Classes-that-make-up-DOM#moving-an-attribute-between-elements
const DOCS_ATTR_GETATTRIBUTENODE = <<<HTML
Expand Down
19 changes: 19 additions & 0 deletions test/unit/NodeListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,23 @@ public function testNodeListIndexOutOfBounds() {

self::assertNull($paragraphList[$count]);
}

public function testCountDoesNotBreakIterator() {
$document = new HTMLDocument(Helper::HTML_SECTIONS_WITHIN_FORM);
$form = $document->getElementById("example-form");
$sectionList = $form->querySelectorAll("section");
$count = count($sectionList);
$actualCount = 0;

foreach($sectionList as $i => $section) {
$actualCount ++;
$section->setAttribute(
"data-section-count",
count($sectionList)
);
}

self::assertGreaterThan(1, $actualCount);
self::assertEquals($count, $actualCount);
}
}

0 comments on commit 343babc

Please sign in to comment.