-<?php
+<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
-
namespace PharIo\Manifest;
use DOMElement;
use DOMNodeList;
abstract class ElementCollection implements \Iterator {
- /**
- * @var DOMNodeList
- */
- private $nodeList;
+ /** @var DOMElement[] */
+ private $nodes = [];
+ /** @var int */
private $position;
- /**
- * ElementCollection constructor.
- *
- * @param DOMNodeList $nodeList
- */
public function __construct(DOMNodeList $nodeList) {
- $this->nodeList = $nodeList;
$this->position = 0;
+ $this->importNodes($nodeList);
}
abstract public function current();
- /**
- * @return DOMElement
- */
- protected function getCurrentElement() {
- return $this->nodeList->item($this->position);
- }
-
- public function next() {
+ public function next(): void {
$this->position++;
}
}
public function valid() {
- return $this->position < $this->nodeList->length;
+ return $this->position < \count($this->nodes);
}
- public function rewind() {
+ public function rewind(): void {
$this->position = 0;
}
+
+ protected function getCurrentElement(): DOMElement {
+ return $this->nodes[$this->position];
+ }
+
+ private function importNodes(DOMNodeList $nodeList): void {
+ foreach ($nodeList as $node) {
+ if (!$node instanceof DOMElement) {
+ throw new ElementCollectionException(
+ \sprintf('\DOMElement expected, got \%s', \get_class($node))
+ );
+ }
+
+ $this->nodes[] = $node;
+ }
+ }
}