PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/XmlConnect/Model/Simplexml/Form/Element/Collection.php

https://bitbucket.org/acidel/buykoala
PHP | 202 lines | 83 code | 15 blank | 104 comment | 8 complexity | 6b7a5b97a4703197e9a5c976f2cddecd MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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 Mage
  22. * @package Mage_XmlConnect
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Xmlconnect form element collection
  28. *
  29. * @category Mage
  30. * @package Mage_XmlConnect
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_XmlConnect_Model_Simplexml_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 Mage_XmlConnect_Model_Simplexml_Form_Abstract
  45. */
  46. private $_container;
  47. /**
  48. * Class constructor
  49. *
  50. * @param Mage_XmlConnect_Model_Simplexml_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 bool
  99. */
  100. public function offsetExists($key)
  101. {
  102. return isset($this->_elements[$key]);
  103. }
  104. /**
  105. * Add element to collection
  106. *
  107. * @param Mage_XmlConnect_Model_Simplexml_Form_Abstract $element
  108. * @param bool|string $after
  109. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
  110. */
  111. public function add(Mage_XmlConnect_Model_Simplexml_Form_Abstract $element, $after = false)
  112. {
  113. // Set the Form for the node
  114. if ($this->_container->getForm() instanceof Mage_XmlConnect_Model_Simplexml_Form) {
  115. $element->setContainer($this->_container);
  116. $element->setForm($this->_container->getForm());
  117. }
  118. if ($after === false) {
  119. $this->_elements[] = $element;
  120. } elseif ($after === '^') {
  121. array_unshift($this->_elements, $element);
  122. } elseif (is_string($after)) {
  123. $newOrderElements = array();
  124. foreach ($this->_elements as $index => $currElement) {
  125. if ($currElement->getId() == $after) {
  126. $newOrderElements[] = $currElement;
  127. $newOrderElements[] = $element;
  128. $this->_elements = array_merge($newOrderElements, array_slice($this->_elements, ++$index));
  129. return $element;
  130. }
  131. $newOrderElements[] = $currElement;
  132. }
  133. $this->_elements[] = $element;
  134. }
  135. return $element;
  136. }
  137. /**
  138. * Sort elements by values using a user-defined comparison function
  139. *
  140. * @param mixed $callback
  141. * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
  142. */
  143. public function usort($callback)
  144. {
  145. usort($this->_elements, $callback);
  146. return $this;
  147. }
  148. /**
  149. * Remove element from collection
  150. *
  151. * @param mixed $elementId
  152. * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
  153. */
  154. public function remove($elementId)
  155. {
  156. foreach ($this->_elements as $index => $element) {
  157. if ($elementId == $element->getId()) {
  158. unset($this->_elements[$index]);
  159. }
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Count elements in collection
  165. *
  166. * @return int
  167. */
  168. public function count()
  169. {
  170. return count($this->_elements);
  171. }
  172. /**
  173. * Find element by ID
  174. *
  175. * @param mixed $elementId
  176. * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract|null
  177. */
  178. public function searchById($elementId)
  179. {
  180. foreach ($this->_elements as $element) {
  181. if ($element->getId() == $elementId) {
  182. return $element;
  183. }
  184. }
  185. return null;
  186. }
  187. }