Collection.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Data\Form\Element;
  7. use Magento\Framework\Data\Form;
  8. use Magento\Framework\Data\Form\AbstractForm;
  9. /**
  10. * Form element collection
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. class Collection implements \ArrayAccess, \IteratorAggregate, \Countable
  15. {
  16. /**
  17. * Elements storage
  18. *
  19. * @var array
  20. */
  21. private $_elements;
  22. /**
  23. * Elements container
  24. *
  25. * @var AbstractForm
  26. */
  27. private $_container;
  28. /**
  29. * Class constructor
  30. *
  31. * @param AbstractForm $container
  32. */
  33. public function __construct(AbstractForm $container)
  34. {
  35. $this->_elements = [];
  36. $this->_container = $container;
  37. }
  38. /**
  39. * Implementation of \IteratorAggregate::getIterator()
  40. *
  41. * @return \ArrayIterator
  42. */
  43. public function getIterator()
  44. {
  45. return new \ArrayIterator($this->_elements);
  46. }
  47. /**
  48. * Implementation of \ArrayAccess:offsetSet()
  49. *
  50. * @param mixed $key
  51. * @param mixed $value
  52. * @return void
  53. */
  54. public function offsetSet($key, $value)
  55. {
  56. $this->_elements[$key] = $value;
  57. }
  58. /**
  59. * Implementation of \ArrayAccess:offsetGet()
  60. *
  61. * @param mixed $key
  62. * @return AbstractElement
  63. */
  64. public function offsetGet($key)
  65. {
  66. return $this->_elements[$key];
  67. }
  68. /**
  69. * Implementation of \ArrayAccess:offsetUnset()
  70. *
  71. * @param mixed $key
  72. * @return void
  73. */
  74. public function offsetUnset($key)
  75. {
  76. unset($this->_elements[$key]);
  77. }
  78. /**
  79. * Implementation of \ArrayAccess:offsetExists()
  80. *
  81. * @param mixed $key
  82. * @return boolean
  83. */
  84. public function offsetExists($key)
  85. {
  86. return isset($this->_elements[$key]);
  87. }
  88. /**
  89. * Add element to collection
  90. *
  91. * @todo get it straight with $after
  92. * @param AbstractElement $element
  93. * @param bool|string $after
  94. * @return AbstractElement
  95. */
  96. public function add(AbstractElement $element, $after = false)
  97. {
  98. // Set the Form for the node
  99. if ($this->_container->getForm() instanceof Form) {
  100. $element->setContainer($this->_container);
  101. $element->setForm($this->_container->getForm());
  102. }
  103. if ($after === false) {
  104. $this->_elements[] = $element;
  105. } elseif ($after === '^') {
  106. array_unshift($this->_elements, $element);
  107. } elseif (is_string($after)) {
  108. $newOrderElements = [];
  109. foreach ($this->_elements as $index => $currElement) {
  110. if ($currElement->getId() == $after) {
  111. $newOrderElements[] = $currElement;
  112. $newOrderElements[] = $element;
  113. $this->_elements = array_merge($newOrderElements, array_slice($this->_elements, $index + 1));
  114. return $element;
  115. }
  116. $newOrderElements[] = $currElement;
  117. }
  118. $this->_elements[] = $element;
  119. }
  120. return $element;
  121. }
  122. /**
  123. * Sort elements by values using a user-defined comparison function
  124. *
  125. * @param mixed $callback
  126. * @return $this
  127. */
  128. public function usort($callback)
  129. {
  130. usort($this->_elements, $callback);
  131. return $this;
  132. }
  133. /**
  134. * Remove element from collection
  135. *
  136. * @param mixed $elementId
  137. * @return $this
  138. */
  139. public function remove($elementId)
  140. {
  141. foreach ($this->_elements as $index => $element) {
  142. if ($elementId == $element->getId()) {
  143. unset($this->_elements[$index]);
  144. }
  145. }
  146. // Renumber elements for further correct adding and removing other elements
  147. $this->_elements = array_merge($this->_elements, []);
  148. return $this;
  149. }
  150. /**
  151. * Count elements in collection
  152. *
  153. * @return int
  154. */
  155. public function count()
  156. {
  157. return count($this->_elements);
  158. }
  159. /**
  160. * Find element by ID
  161. *
  162. * @param mixed $elementId
  163. * @return AbstractElement
  164. */
  165. public function searchById($elementId)
  166. {
  167. foreach ($this->_elements as $element) {
  168. if ($element->getId() == $elementId) {
  169. return $element;
  170. }
  171. }
  172. return null;
  173. }
  174. }