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) 2012 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: /**
28: * Xmlconnect form element collection
29: *
30: * @category Mage
31: * @package Mage_XmlConnect
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_XmlConnect_Model_Simplexml_Form_Element_Collection implements ArrayAccess, IteratorAggregate
35: {
36: /**
37: * Elements storage
38: *
39: * @var array
40: */
41: private $_elements;
42:
43: /**
44: * Elements container
45: *
46: * @var Mage_XmlConnect_Model_Simplexml_Form_Abstract
47: */
48: private $_container;
49:
50: /**
51: * Class constructor
52: *
53: * @param Mage_XmlConnect_Model_Simplexml_Form_Abstract $container
54: */
55: public function __construct($container)
56: {
57: $this->_elements = array();
58: $this->_container = $container;
59: }
60:
61: /**
62: * Implementation of IteratorAggregate::getIterator()
63: *
64: * @return ArrayIterator
65: */
66: public function getIterator()
67: {
68: return new ArrayIterator($this->_elements);
69: }
70:
71: /**
72: * Implementation of ArrayAccess:offsetSet()
73: *
74: * @param mixed $key
75: * @param mixed $value
76: */
77: public function offsetSet($key, $value)
78: {
79: $this->_elements[$key] = $value;
80: }
81:
82: /**
83: * Implementation of ArrayAccess:offsetGet()
84: *
85: * @param mixed $key
86: */
87: public function offsetGet($key)
88: {
89: return $this->_elements[$key];
90: }
91:
92: /**
93: * Implementation of ArrayAccess:offsetUnset()
94: *
95: * @param mixed $key
96: */
97: public function offsetUnset($key)
98: {
99: unset($this->_elements[$key]);
100: }
101:
102: /**
103: * Implementation of ArrayAccess:offsetExists()
104: *
105: * @param mixed $key
106: * @return bool
107: */
108: public function offsetExists($key)
109: {
110: return isset($this->_elements[$key]);
111: }
112:
113: /**
114: * Add element to collection
115: *
116: * @param Mage_XmlConnect_Model_Simplexml_Form_Abstract $element
117: * @param bool|string $after
118: * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract
119: */
120: public function add(Mage_XmlConnect_Model_Simplexml_Form_Abstract $element, $after = false)
121: {
122: // Set the Form for the node
123: if ($this->_container->getForm() instanceof Mage_XmlConnect_Model_Simplexml_Form) {
124: $element->setContainer($this->_container);
125: $element->setForm($this->_container->getForm());
126: }
127:
128: if ($after === false) {
129: $this->_elements[] = $element;
130: } elseif ($after === '^') {
131: array_unshift($this->_elements, $element);
132: } elseif (is_string($after)) {
133: $newOrderElements = array();
134: foreach ($this->_elements as $index => $currElement) {
135: if ($currElement->getId() == $after) {
136: $newOrderElements[] = $currElement;
137: $newOrderElements[] = $element;
138: $this->_elements = array_merge($newOrderElements, array_slice($this->_elements, ++$index));
139: return $element;
140: }
141: $newOrderElements[] = $currElement;
142: }
143: $this->_elements[] = $element;
144: }
145:
146: return $element;
147: }
148:
149: /**
150: * Sort elements by values using a user-defined comparison function
151: *
152: * @param mixed $callback
153: * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
154: */
155: public function usort($callback)
156: {
157: usort($this->_elements, $callback);
158: return $this;
159: }
160:
161: /**
162: * Remove element from collection
163: *
164: * @param mixed $elementId
165: * @return Mage_XmlConnect_Model_Simplexml_Form_Element_Collection
166: */
167: public function remove($elementId)
168: {
169: foreach ($this->_elements as $index => $element) {
170: if ($elementId == $element->getId()) {
171: unset($this->_elements[$index]);
172: }
173: }
174: return $this;
175: }
176:
177: /**
178: * Count elements in collection
179: *
180: * @return int
181: */
182: public function count()
183: {
184: return count($this->_elements);
185: }
186:
187: /**
188: * Find element by ID
189: *
190: * @param mixed $elementId
191: * @return Mage_XmlConnect_Model_Simplexml_Form_Abstract|null
192: */
193: public function searchById($elementId)
194: {
195: foreach ($this->_elements as $element) {
196: if ($element->getId() == $elementId) {
197: return $element;
198: }
199: }
200: return null;
201: }
202: }
203: