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
7 
10 
16 class Collection implements \ArrayAccess, \IteratorAggregate, \Countable
17 {
23  private $_elements;
24 
30  private $_container;
31 
37  public function __construct(AbstractForm $container)
38  {
39  $this->_elements = [];
40  $this->_container = $container;
41  }
42 
48  public function getIterator()
49  {
50  return new \ArrayIterator($this->_elements);
51  }
52 
60  public function offsetSet($key, $value)
61  {
62  $this->_elements[$key] = $value;
63  }
64 
71  public function offsetGet($key)
72  {
73  return $this->_elements[$key];
74  }
75 
82  public function offsetUnset($key)
83  {
84  unset($this->_elements[$key]);
85  }
86 
93  public function offsetExists($key)
94  {
95  return isset($this->_elements[$key]);
96  }
97 
106  public function add(AbstractElement $element, $after = false)
107  {
108  // Set the Form for the node
109  if ($this->_container->getForm() instanceof Form) {
110  $element->setContainer($this->_container);
111  $element->setForm($this->_container->getForm());
112  }
113 
114  if ($after === false) {
115  $this->_elements[] = $element;
116  } elseif ($after === '^') {
117  array_unshift($this->_elements, $element);
118  } elseif (is_string($after)) {
119  $newOrderElements = [];
120  foreach ($this->_elements as $index => $currElement) {
121  if ($currElement->getId() == $after) {
122  $newOrderElements[] = $currElement;
123  $newOrderElements[] = $element;
124  $this->_elements = array_merge($newOrderElements, array_slice($this->_elements, $index + 1));
125  return $element;
126  }
127  $newOrderElements[] = $currElement;
128  }
129  $this->_elements[] = $element;
130  }
131 
132  return $element;
133  }
134 
141  public function usort($callback)
142  {
143  usort($this->_elements, $callback);
144  return $this;
145  }
146 
153  public function remove($elementId)
154  {
155  foreach ($this->_elements as $index => $element) {
156  if ($elementId == $element->getId()) {
157  unset($this->_elements[$index]);
158  }
159  }
160  // Renumber elements for further correct adding and removing other elements
161  $this->_elements = array_merge($this->_elements, []);
162  return $this;
163  }
164 
170  public function count()
171  {
172  return count($this->_elements);
173  }
174 
181  public function searchById($elementId)
182  {
183  foreach ($this->_elements as $element) {
184  if ($element->getId() == $elementId) {
185  return $element;
186  }
187  }
188  return null;
189  }
190 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
add(AbstractElement $element, $after=false)
Definition: Collection.php:106
$value
Definition: gender.phtml:16
$index
Definition: list.phtml:44
$element
Definition: element.phtml:12