/includes/src/Varien/Data/Form/Element/Collection.php

https://bitbucket.org/kdms/sh-magento · PHP · 210 lines · 86 code · 17 blank · 107 comment · 8 complexity · 323d5c9d54688884322b2a0c23d903ee MD5 · raw file

  1. <?php
  2. /**
  3. * Magento Enterprise Edition
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Magento Enterprise Edition License
  8. * that is bundled with this package in the file LICENSE_EE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://www.magentocommerce.com/license/enterprise-edition
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_Data
  23. * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://www.magentocommerce.com/license/enterprise-edition
  25. */
  26. /**
  27. * Form element collection
  28. *
  29. * @category Varien
  30. * @package Varien_Data
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Varien_Data_Form_Element_Collection implements ArrayAccess, IteratorAggregate
  34. {
  35. /**
  36. * Elements storage
  37. *
  38. * @var array
  39. */
  40. private $_elements;
  41. /**
  42. * Elements container
  43. *
  44. * @var Varien_Data_Form_Abstract
  45. */
  46. private $_container;
  47. /**
  48. * Class constructor
  49. *
  50. * @param Varien_Data_Form_Abstract $container
  51. */
  52. public function __construct($container)
  53. {
  54. $this->_elements = array();
  55. $this->_container = $container;
  56. }
  57. /**
  58. * Implementation of IteratorAggregate::getIterator()
  59. *
  60. * @return ArrayIterator
  61. */
  62. public function getIterator()
  63. {
  64. return new ArrayIterator($this->_elements);
  65. }
  66. /**
  67. * Implementation of ArrayAccess:offsetSet()
  68. *
  69. * @param mixed $key
  70. * @param mixed $value
  71. */
  72. public function offsetSet($key, $value)
  73. {
  74. $this->_elements[$key] = $value;
  75. }
  76. /**
  77. * Implementation of ArrayAccess:offsetGet()
  78. *
  79. * @param mixed $key
  80. */
  81. public function offsetGet($key)
  82. {
  83. return $this->_elements[$key];
  84. }
  85. /**
  86. * Implementation of ArrayAccess:offsetUnset()
  87. *
  88. * @param mixed $key
  89. */
  90. public function offsetUnset($key)
  91. {
  92. unset($this->_elements[$key]);
  93. }
  94. /**
  95. * Implementation of ArrayAccess:offsetExists()
  96. *
  97. * @param mixed $key
  98. * @return boolean
  99. */
  100. public function offsetExists($key)
  101. {
  102. return isset($this->_elements[$key]);
  103. }
  104. /**
  105. * Add element to collection
  106. *
  107. * @todo get it straight with $after
  108. * @param Varien_Data_Form_Element_Abstract $element
  109. * @param bool|string $after
  110. *
  111. * @return Varien_Data_Form_Element_Collection
  112. */
  113. public function add(Varien_Data_Form_Element_Abstract $element, $after = false)
  114. {
  115. // Set the Form for the node
  116. if ($this->_container->getForm() instanceof Varien_Data_Form) {
  117. $element->setContainer($this->_container);
  118. $element->setForm($this->_container->getForm());
  119. }
  120. if ($after === false) {
  121. $this->_elements[] = $element;
  122. }
  123. elseif ($after === '^') {
  124. array_unshift($this->_elements, $element);
  125. }
  126. elseif (is_string($after)) {
  127. $newOrderElements = array();
  128. foreach ($this->_elements as $index => $currElement) {
  129. if ($currElement->getId() == $after) {
  130. $newOrderElements[] = $currElement;
  131. $newOrderElements[] = $element;
  132. $this->_elements = array_merge($newOrderElements, array_slice($this->_elements, $index + 1));
  133. return $element;
  134. }
  135. $newOrderElements[] = $currElement;
  136. }
  137. $this->_elements[] = $element;
  138. }
  139. return $element;
  140. }
  141. /**
  142. * Sort elements by values using a user-defined comparison function
  143. *
  144. * @param mixed $callback
  145. * @return Varien_Data_Form_Element_Collection
  146. */
  147. public function usort($callback)
  148. {
  149. usort($this->_elements, $callback);
  150. return $this;
  151. }
  152. /**
  153. * Remove element from collection
  154. *
  155. * @param mixed $elementId
  156. * @return Varien_Data_Form_Element_Collection
  157. */
  158. public function remove($elementId)
  159. {
  160. foreach ($this->_elements as $index => $element) {
  161. if ($elementId == $element->getId()) {
  162. unset($this->_elements[$index]);
  163. }
  164. }
  165. // Renumber elements for further correct adding and removing other elements
  166. $this->_elements = array_merge($this->_elements, array());
  167. return $this;
  168. }
  169. /**
  170. * Count elements in collection
  171. *
  172. * @return int
  173. */
  174. public function count()
  175. {
  176. return count($this->_elements);
  177. }
  178. /**
  179. * Find element by ID
  180. *
  181. * @param mixed $elementId
  182. * @return Varien_Data_Form_Element_Abstract|null
  183. */
  184. public function searchById($elementId)
  185. {
  186. foreach ($this->_elements as $element) {
  187. if ($element->getId() == $elementId) {
  188. return $element;
  189. }
  190. }
  191. return null;
  192. }
  193. }