Collection.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace PHPHtmlParser\Dom;
  3. use ArrayAccess;
  4. use ArrayIterator;
  5. use Countable;
  6. use IteratorAggregate;
  7. use PHPHtmlParser\Exceptions\EmptyCollectionException;
  8. /**
  9. * Class Collection
  10. *
  11. * @package PHPHtmlParser\Dom
  12. */
  13. class Collection implements IteratorAggregate, ArrayAccess, Countable
  14. {
  15. /**
  16. * The collection of Nodes.
  17. *
  18. * @param array
  19. */
  20. protected $collection = [];
  21. /**
  22. * Attempts to call the method on the first node in
  23. * the collection.
  24. *
  25. * @param string $method
  26. * @param array $arguments
  27. * @return mixed
  28. * @throws EmptyCollectionException
  29. */
  30. public function __call(string $method, array $arguments)
  31. {
  32. $node = reset($this->collection);
  33. if ($node instanceof AbstractNode) {
  34. return call_user_func_array([$node, $method], $arguments);
  35. } else {
  36. throw new EmptyCollectionException('The collection does not contain any Nodes.');
  37. }
  38. }
  39. /**
  40. * Attempts to apply the magic get to the first node
  41. * in the collection.
  42. *
  43. * @param mixed $key
  44. * @return mixed
  45. * @throws EmptyCollectionException
  46. */
  47. public function __get($key)
  48. {
  49. $node = reset($this->collection);
  50. if ($node instanceof AbstractNode) {
  51. return $node->$key;
  52. } else {
  53. throw new EmptyCollectionException('The collection does not contain any Nodes.');
  54. }
  55. }
  56. /**
  57. * Applies the magic string method to the first node in
  58. * the collection.
  59. *
  60. * @return string
  61. * @throws EmptyCollectionException
  62. */
  63. public function __toString(): string
  64. {
  65. $node = reset($this->collection);
  66. if ($node instanceof AbstractNode) {
  67. return (string)$node;
  68. } else {
  69. return '';
  70. }
  71. }
  72. /**
  73. * Returns the count of the collection.
  74. *
  75. * @return int
  76. */
  77. public function count(): int
  78. {
  79. return count($this->collection);
  80. }
  81. /**
  82. * Returns an iterator for the collection.
  83. *
  84. * @return ArrayIterator
  85. */
  86. public function getIterator(): ArrayIterator
  87. {
  88. return new ArrayIterator($this->collection);
  89. }
  90. /**
  91. * Set an attribute by the given offset
  92. *
  93. * @param mixed $offset
  94. * @param mixed $value
  95. */
  96. public function offsetSet($offset, $value): void
  97. {
  98. if (is_null($offset)) {
  99. $this->collection[] = $value;
  100. } else {
  101. $this->collection[$offset] = $value;
  102. }
  103. }
  104. /**
  105. * Checks if an offset exists.
  106. *
  107. * @param mixed $offset
  108. * @return bool
  109. */
  110. public function offsetExists($offset): bool
  111. {
  112. return isset($this->collection[$offset]);
  113. }
  114. /**
  115. * Unset a collection Node.
  116. *
  117. * @param mixed $offset
  118. */
  119. public function offsetUnset($offset): void
  120. {
  121. unset($this->collection[$offset]);
  122. }
  123. /**
  124. * Gets a node at the given offset, or null
  125. *
  126. * @param mixed $offset
  127. * @return mixed
  128. */
  129. public function offsetGet($offset)
  130. {
  131. return isset($this->collection[$offset]) ? $this->collection[$offset] : null;
  132. }
  133. /**
  134. * Returns this collection as an array.
  135. *
  136. * @return array
  137. */
  138. public function toArray(): array
  139. {
  140. return $this->collection;
  141. }
  142. /**
  143. * Similar to jQuery "each" method. Calls the callback with each
  144. * Node in this collection.
  145. *
  146. * @param callable $callback
  147. */
  148. public function each(callable $callback)
  149. {
  150. foreach ($this->collection as $key => $value) {
  151. $callback($value, $key);
  152. }
  153. }
  154. }