Collection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Tree node collection
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Framework\Data\Tree\Node;
  12. use Magento\Framework\Data\Tree;
  13. use Magento\Framework\Data\Tree\Node;
  14. /**
  15. * @api
  16. * @since 100.0.2
  17. */
  18. class Collection implements \ArrayAccess, \IteratorAggregate, \Countable
  19. {
  20. /**
  21. * @var array
  22. */
  23. private $_nodes;
  24. /**
  25. * @var Node
  26. */
  27. private $_container;
  28. /**
  29. * @param Node $container
  30. */
  31. public function __construct($container)
  32. {
  33. $this->_nodes = [];
  34. $this->_container = $container;
  35. }
  36. /**
  37. * Get the nodes
  38. *
  39. * @return array
  40. */
  41. public function getNodes()
  42. {
  43. return $this->_nodes;
  44. }
  45. /**
  46. * Implementation of \IteratorAggregate::getIterator()
  47. *
  48. * @return \ArrayIterator
  49. */
  50. public function getIterator()
  51. {
  52. return new \ArrayIterator($this->_nodes);
  53. }
  54. /**
  55. * Implementation of \ArrayAccess:offsetSet()
  56. *
  57. * @param string $key
  58. * @param mixed $value
  59. * @return void
  60. */
  61. public function offsetSet($key, $value)
  62. {
  63. $this->_nodes[$key] = $value;
  64. }
  65. /**
  66. * Implementation of \ArrayAccess:offsetGet()
  67. * @param string $key
  68. * @return mixed
  69. */
  70. public function offsetGet($key)
  71. {
  72. return $this->_nodes[$key];
  73. }
  74. /**
  75. * Implementation of \ArrayAccess:offsetUnset()
  76. * @param string $key
  77. * @return void
  78. */
  79. public function offsetUnset($key)
  80. {
  81. unset($this->_nodes[$key]);
  82. }
  83. /**
  84. * Implementation of \ArrayAccess:offsetExists()
  85. * @param string $key
  86. * @return bool
  87. */
  88. public function offsetExists($key)
  89. {
  90. return isset($this->_nodes[$key]);
  91. }
  92. /**
  93. * Adds a node to this node
  94. * @param Node $node
  95. * @return Node
  96. */
  97. public function add(Node $node)
  98. {
  99. $node->setParent($this->_container);
  100. // Set the Tree for the node
  101. if ($this->_container->getTree() instanceof Tree) {
  102. $node->setTree($this->_container->getTree());
  103. }
  104. $this->_nodes[$node->getId()] = $node;
  105. return $node;
  106. }
  107. /**
  108. * Delete
  109. *
  110. * @param Node $node
  111. * @return $this
  112. */
  113. public function delete($node)
  114. {
  115. if (isset($this->_nodes[$node->getId()])) {
  116. unset($this->_nodes[$node->getId()]);
  117. }
  118. return $this;
  119. }
  120. /**
  121. * Return count
  122. *
  123. * @return int
  124. */
  125. public function count()
  126. {
  127. return count($this->_nodes);
  128. }
  129. /**
  130. * Return the last node
  131. *
  132. * @return mixed
  133. */
  134. public function lastNode()
  135. {
  136. if (!empty($this->_nodes)) {
  137. $result = end($this->_nodes);
  138. reset($this->_nodes);
  139. } else {
  140. $result = null;
  141. }
  142. return $result;
  143. }
  144. /**
  145. * Search by Id
  146. *
  147. * @param string $nodeId
  148. * @return null
  149. */
  150. public function searchById($nodeId)
  151. {
  152. if (isset($this->_nodes[$nodeId])) {
  153. return $this->_nodes[$nodeId];
  154. }
  155. return null;
  156. }
  157. }