OSDN Git Service

Добавлена возможность создавать новую запись предмета оборудования на основании уже...
[invent/invent.git] / vendor / phar-io / manifest / src / xml / ElementCollection.php
index 284e77b..1d13a91 100644 (file)
@@ -1,4 +1,4 @@
-<?php
+<?php declare(strict_types = 1);
 /*
  * This file is part of PharIo\Manifest.
  *
@@ -7,40 +7,26 @@
  * 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++;
     }
 
@@ -49,10 +35,26 @@ abstract class ElementCollection implements \Iterator {
     }
 
     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;
+        }
+    }
 }