1: <?php
2:
3: namespace ModHelper;
4:
5: /**
6: * @package ModHelper
7: * @since 1.0
8: */
9: class Collection implements \IteratorAggregate, \ArrayAccess
10: {
11: /**
12: * The array that holds all the items collected by the object.
13: *
14: * @var array
15: * @access private
16: */
17: private $items = array();
18:
19: /**
20: * Assigns a value to the specified offset
21: *
22: * @param string The offset to assign the value to
23: * @param mixed The value to set
24: * @access public
25: * @abstracting ArrayAccess
26: * @since 1.1
27: * @version 1.1
28: */
29: public function offsetSet($offset, $value)
30: {
31: if (is_null($offset)) {
32: $this->items[] = $value;
33: } else {
34: $this->items[$offset] = $value;
35: }
36: }
37:
38: /**
39: * Whether or not an offset exists
40: *
41: * @param string An offset to check for
42: * @access public
43: * @return boolean
44: * @abstracting ArrayAccess
45: * @since 1.1
46: * @version 1.1
47: */
48: public function offsetExists($offset)
49: {
50: return isset($this->items[$offset]);
51: }
52:
53: /**
54: * Unsets an offset
55: *
56: * @param string The offset to unset
57: * @access public
58: * @abstracting ArrayAccess
59: * @since 1.1
60: * @version 1.1
61: */
62: public function offsetUnset($offset)
63: {
64: unset($this->items[$offset]);
65: }
66:
67: /**
68: * Returns the value at specified offset
69: *
70: * @param string The offset to retrieve
71: * @access public
72: * @return mixed
73: * @abstracting ArrayAccess
74: * @since 1.1
75: * @version 1.1
76: */
77: public function offsetGet($offset)
78: {
79: return isset($this->items[$offset]) ? $this->items[$offset] : null;
80: }
81:
82: /**
83: * Appends a value to the object
84: *
85: * This method is chainable.
86: *
87: * @param mixed The value to set
88: * @access public
89: * @since 1.0
90: * @version 1.0
91: */
92: public function addValue($item)
93: {
94: $this->items[] = $item;
95: return $this;
96: }
97:
98: /**
99: * Retrieve an external iterator.
100: *
101: * @access public
102: * @abstracting IteratorAggregate
103: * @since 1.0
104: * @version 1.1
105: */
106: public function getIterator()
107: {
108: foreach ($this->items as $id => $item) {
109: yield $id => $item;
110: }
111: }
112: }