1 <?php
declare(strict_types
=1);
10 use DOMWrap\Collections\NodeCollection
;
16 * @license http://opensource.org/licenses/BSD-3-Clause BSD 3 Clause
18 class NodeList
extends NodeCollection
22 use ManipulationTrait
{
23 ManipulationTrait
::__call
as __manipulationCall
;
30 * @param Document $document
31 * @param iterable $nodes
33 public function __construct(Document
$document = null, iterable
$nodes = null) {
34 parent
::__construct($nodes);
36 $this->document
= $document;
41 * @param array $arguments
45 public function __call(string $name, array $arguments) {
47 $result = $this->__manipulationCall($name, $arguments);
48 } catch (\BadMethodCallException
$e) {
49 if (!$this->first() ||
!method_exists($this->first(), $name)) {
50 throw new \
BadMethodCallException("Call to undefined method " . get_class($this) . '::' . $name . "()");
53 $result = call_user_func_array([$this->first(), $name], $arguments);
62 public function collection(): NodeList
{
69 public function document(): ?\DOMDocument
{
70 return $this->document
;
76 public function result(NodeList
$nodeList) {
83 public function reverse(): NodeList
{
84 array_reverse($this->nodes
);
92 public function first() {
93 return !empty($this->nodes
) ?
$this->rewind() : null;
99 public function last() {
106 public function end() {
107 return !empty($this->nodes
) ?
end($this->nodes
) : null;
115 public function get(int $key) {
116 if (isset($this->nodes
[$key])) {
117 return $this->nodes
[$key];
125 * @param mixed $value
129 public function set(int $key, $value): self
{
130 $this->nodes
[$key] = $value;
136 * @param callable $function
140 public function each(callable
$function): self
{
141 foreach ($this->nodes
as $index => $node) {
142 $result = $function($node, $index);
144 if ($result === false) {
153 * @param callable $function
157 public function map(callable
$function): NodeList
{
158 $nodes = $this->newNodeList();
160 foreach ($this->nodes
as $node) {
161 $result = $function($node);
163 if (!is_null($result) && $result !== false) {
172 * @param callable $function
173 * @param mixed|null $initial
177 public function reduce(callable
$function, $initial = null) {
178 return array_reduce($this->nodes
, $function, $initial);
184 public function toArray() {
189 * @param iterable $nodes
191 public function fromArray(iterable
$nodes = null) {
194 if (is_iterable($nodes)) {
195 foreach ($nodes as $node) {
196 $this->nodes
[] = $node;
202 * @param NodeList|array $elements
206 public function merge($elements = []): NodeList
{
207 if (!is_array($elements)) {
208 $elements = $elements->toArray();
211 return $this->newNodeList(array_merge($this->toArray(), $elements));
220 public function slice(int $start, int $end = null): NodeList
{
221 $nodeList = array_slice($this->toArray(), $start, $end);
223 return $this->newNodeList($nodeList);
227 * @param \DOMNode $node
231 public function push(\DOMNode
$node): self
{
232 $this->nodes
[] = $node;
240 public function pop(): \DOMNode
{
241 return array_pop($this->nodes
);
245 * @param \DOMNode $node
249 public function unshift(\DOMNode
$node): self
{
250 array_unshift($this->nodes
, $node);
258 public function shift(): \DOMNode
{
259 return array_shift($this->nodes
);
263 * @param \DOMNode $node
267 public function exists(\DOMNode
$node): bool {
268 return in_array($node, $this->nodes
, true);
272 * @param \DOMNode $node
276 public function delete(\DOMNode
$node): self
{
277 $index = array_search($node, $this->nodes
, true);
279 if ($index !== false) {
280 unset($this->nodes
[$index]);
289 public function isRemoved(): bool {