Merge pull request #4036 from dokuwiki/issue4033
[dokuwiki.git] / _test / vendor / scotteh / php-dom-wrapper / src / NodeList.php
blob8b46257044ddcb46a5434c556b7f47d1c8f439c9
1 <?php declare(strict_types=1);
3 namespace DOMWrap;
5 use DOMWrap\Traits\{
6 CommonTrait,
7 TraversalTrait,
8 ManipulationTrait
9 };
10 use DOMWrap\Collections\NodeCollection;
12 /**
13 * Node List
15 * @package DOMWrap
16 * @license http://opensource.org/licenses/BSD-3-Clause BSD 3 Clause
18 class NodeList extends NodeCollection
20 use CommonTrait;
21 use TraversalTrait;
22 use ManipulationTrait {
23 ManipulationTrait::__call as __manipulationCall;
26 /** @var Document */
27 protected $document;
29 /**
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;
39 /**
40 * @param string $name
41 * @param array $arguments
43 * @return mixed
45 public function __call(string $name, array $arguments) {
46 try {
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);
56 return $result;
59 /**
60 * {@inheritdoc}
62 public function collection(): NodeList {
63 return $this;
66 /**
67 * {@inheritdoc}
69 public function document(): ?\DOMDocument {
70 return $this->document;
73 /**
74 * {@inheritdoc}
76 public function result(NodeList $nodeList) {
77 return $nodeList;
80 /**
81 * @return NodeList
83 public function reverse(): NodeList {
84 array_reverse($this->nodes);
86 return $this;
89 /**
90 * @return mixed
92 public function first() {
93 return !empty($this->nodes) ? $this->rewind() : null;
96 /**
97 * @return mixed
99 public function last() {
100 return $this->end();
104 * @return mixed
106 public function end() {
107 return !empty($this->nodes) ? end($this->nodes) : null;
111 * @param int $key
113 * @return mixed
115 public function get(int $key) {
116 if (isset($this->nodes[$key])) {
117 return $this->nodes[$key];
120 return null;
124 * @param int $key
125 * @param mixed $value
127 * @return self
129 public function set(int $key, $value): self {
130 $this->nodes[$key] = $value;
132 return $this;
136 * @param callable $function
138 * @return self
140 public function each(callable $function): self {
141 foreach ($this->nodes as $index => $node) {
142 $result = $function($node, $index);
144 if ($result === false) {
145 break;
149 return $this;
153 * @param callable $function
155 * @return NodeList
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) {
164 $nodes[] = $result;
168 return $nodes;
172 * @param callable $function
173 * @param mixed|null $initial
175 * @return iterable
177 public function reduce(callable $function, $initial = null) {
178 return array_reduce($this->nodes, $function, $initial);
182 * @return array
184 public function toArray() {
185 return $this->nodes;
189 * @param iterable $nodes
191 public function fromArray(iterable $nodes = null) {
192 $this->nodes = [];
194 if (is_iterable($nodes)) {
195 foreach ($nodes as $node) {
196 $this->nodes[] = $node;
202 * @param NodeList|array $elements
204 * @return NodeList
206 public function merge($elements = []): NodeList {
207 if (!is_array($elements)) {
208 $elements = $elements->toArray();
211 return $this->newNodeList(array_merge($this->toArray(), $elements));
215 * @param int $start
216 * @param int $end
218 * @return NodeList
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
229 * @return self
231 public function push(\DOMNode $node): self {
232 $this->nodes[] = $node;
234 return $this;
238 * @return \DOMNode
240 public function pop(): \DOMNode {
241 return array_pop($this->nodes);
245 * @param \DOMNode $node
247 * @return self
249 public function unshift(\DOMNode $node): self {
250 array_unshift($this->nodes, $node);
252 return $this;
256 * @return \DOMNode
258 public function shift(): \DOMNode {
259 return array_shift($this->nodes);
263 * @param \DOMNode $node
265 * @return bool
267 public function exists(\DOMNode $node): bool {
268 return in_array($node, $this->nodes, true);
272 * @param \DOMNode $node
274 * @return self
276 public function delete(\DOMNode $node): self {
277 $index = array_search($node, $this->nodes, true);
279 if ($index !== false) {
280 unset($this->nodes[$index]);
283 return $this;
287 * @return bool
289 public function isRemoved(): bool {
290 return false;