Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Collection.php
Go to the documentation of this file.
1 <?php
13 
16 
21 class Collection implements \ArrayAccess, \IteratorAggregate, \Countable
22 {
26  private $_nodes;
27 
31  private $_container;
32 
36  public function __construct($container)
37  {
38  $this->_nodes = [];
39  $this->_container = $container;
40  }
41 
47  public function getNodes()
48  {
49  return $this->_nodes;
50  }
51 
57  public function getIterator()
58  {
59  return new \ArrayIterator($this->_nodes);
60  }
61 
69  public function offsetSet($key, $value)
70  {
71  $this->_nodes[$key] = $value;
72  }
73 
79  public function offsetGet($key)
80  {
81  return $this->_nodes[$key];
82  }
83 
89  public function offsetUnset($key)
90  {
91  unset($this->_nodes[$key]);
92  }
93 
99  public function offsetExists($key)
100  {
101  return isset($this->_nodes[$key]);
102  }
103 
109  public function add(Node $node)
110  {
111  $node->setParent($this->_container);
112 
113  // Set the Tree for the node
114  if ($this->_container->getTree() instanceof Tree) {
115  $node->setTree($this->_container->getTree());
116  }
117 
118  $this->_nodes[$node->getId()] = $node;
119 
120  return $node;
121  }
122 
129  public function delete($node)
130  {
131  if (isset($this->_nodes[$node->getId()])) {
132  unset($this->_nodes[$node->getId()]);
133  }
134  return $this;
135  }
136 
142  public function count()
143  {
144  return count($this->_nodes);
145  }
146 
152  public function lastNode()
153  {
154  if (!empty($this->_nodes)) {
155  $result = end($this->_nodes);
156  reset($this->_nodes);
157  } else {
158  $result = null;
159  }
160 
161  return $result;
162  }
163 
170  public function searchById($nodeId)
171  {
172  if (isset($this->_nodes[$nodeId])) {
173  return $this->_nodes[$nodeId];
174  }
175  return null;
176  }
177 }
$value
Definition: gender.phtml:16