Files
apimacro/vendor/phar-io/manifest/src/xml/ElementCollection.php

69 lines
1.7 KiB
PHP
Raw Normal View History

2024-05-07 12:17:25 +02:00
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
2024-05-17 12:24:19 +00:00
* Copyright (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de> and contributors
2024-05-07 12:17:25 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
2024-05-17 12:24:19 +00:00
*
2024-05-07 12:17:25 +02:00
*/
namespace PharIo\Manifest;
use DOMElement;
use DOMNodeList;
2024-05-17 12:24:19 +00:00
use Iterator;
use ReturnTypeWillChange;
use function count;
use function get_class;
use function sprintf;
2024-05-07 12:17:25 +02:00
2024-05-17 12:24:19 +00:00
/** @template-implements Iterator<int,DOMElement> */
abstract class ElementCollection implements Iterator {
2024-05-07 12:17:25 +02:00
/** @var DOMElement[] */
private $nodes = [];
/** @var int */
private $position;
public function __construct(DOMNodeList $nodeList) {
$this->position = 0;
$this->importNodes($nodeList);
}
2024-05-17 12:24:19 +00:00
#[ReturnTypeWillChange]
2024-05-07 12:17:25 +02:00
abstract public function current();
public function next(): void {
$this->position++;
}
public function key(): int {
return $this->position;
}
public function valid(): bool {
2024-05-17 12:24:19 +00:00
return $this->position < count($this->nodes);
2024-05-07 12:17:25 +02:00
}
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(
2024-05-17 12:24:19 +00:00
sprintf('\DOMElement expected, got \%s', get_class($node))
2024-05-07 12:17:25 +02:00
);
}
$this->nodes[] = $node;
}
}
}