1 <?php
2 /* --------------------------------------------------------------
3 AbstractCollection.inc.php 2015-01-24 gm
4 Gambio GmbH
5 http://www.gambio.de
6 Copyright (c) 2015 Gambio GmbH
7 Released under the GNU General Public License (Version 2)
8 [http://www.gnu.org/licenses/gpl-2.0.html]
9 --------------------------------------------------------------
10 */
11
12
13 /**
14 * Class AbstractCollection
15 *
16 * @category System
17 * @package Shared
18 */
19 abstract class AbstractCollection implements IteratorAggregate, \Countable
20 {
21 /**
22 * Content Collection
23 *
24 * @var array
25 */
26 protected $collectionContentArray = array();
27
28
29 /**
30 * Initialize the collection instance.
31 *
32 * @throws InvalidArgumentException
33 *
34 * @param array|mixed|null $argumentsArray
35 */
36 public function __construct($argumentsArray = null)
37 {
38 $argsArray = func_get_args();
39
40 if(is_array($argumentsArray) && count($argsArray) === 1)
41 {
42 $argsArray = $argumentsArray;
43 }
44
45 foreach($argsArray as $argsItem)
46 {
47 try
48 {
49 $this->_add($argsItem);
50 }
51 catch(InvalidArgumentException $e)
52 {
53 throw $e;
54 }
55 }
56 }
57
58
59 /**
60 * Get collection item count.
61 *
62 * @return int
63 */
64 public function count()
65 {
66 return count($this->collectionContentArray);
67 }
68
69
70 /**
71 * Get the collection as an array.
72 *
73 * @return array
74 */
75 public function getArray()
76 {
77 return $this->collectionContentArray;
78 }
79
80
81 /**
82 * Get specific collection item by index.
83 *
84 * @throws InvalidArgumentException if index is not numeric | OutOfBoundsException if index is out of bounds
85 *
86 * @param $p_index
87 *
88 * @return mixed
89 */
90 public function getItem($p_index)
91 {
92 if(is_numeric($p_index) == false)
93 {
94 throw new InvalidArgumentException('Given $p_index not numeric');
95 }
96
97 if($p_index < 0 || $p_index >= sizeof($this->collectionContentArray))
98 {
99 throw new OutOfBoundsException('$p_index is out of bounds');
100 }
101
102 return $this->collectionContentArray[$p_index];
103 }
104
105
106 /**
107 * Determine whether the collection is empty or not.
108 *
109 * @return bool
110 */
111 public function isEmpty()
112 {
113 return 0 === count($this->collectionContentArray);
114 }
115
116
117 /**
118 * Add a new item.
119 * This method must be used by child-collection classes.
120 *
121 * @param mixed $item Item which should add to the collection
122 *
123 * @throws \InvalidArgumentException When $item has an invalid type.
124 */
125 protected function _add($item)
126 {
127 if($this->_itemIsValid($item) === false)
128 {
129 $exceptionText = $this->_getExceptionText();
130 throw new InvalidArgumentException($exceptionText);
131 }
132 else
133 {
134 $this->collectionContentArray[] = $item;
135 }
136 }
137
138
139 /**
140 * Check if a new item has the valid collection type.
141 *
142 * @param mixed $dataItem
143 *
144 * @return bool
145 */
146 protected function _itemIsValid($dataItem)
147 {
148 // The first condition checks if the $dataItem is a string AND the valid type is equal to string.
149 // The second checks if $dataItem is an object and match the expected type,
150 // If one condition is true, the method return true, otherwise false.
151 return ((is_string($dataItem) && strtolower($this->_getValidType()) === 'string')
152 || (is_object($dataItem)
153 && is_a($dataItem,
154 $this->_getValidType())));
155 }
156
157
158 /**
159 * Get exception text.
160 *
161 * @return string
162 */
163 protected function _getExceptionText()
164 {
165 return 'Given item has invalid type ('. $this->_getValidType() .' needed)';
166 }
167
168
169 public function getIterator()
170 {
171 return new ArrayIterator($this->collectionContentArray);
172 }
173
174
175 /**
176 * Get valid type.
177 *
178 * This method must be implemented in the child-collection classes.
179 *
180 * @return string
181 */
182 abstract protected function _getValidType();
183 }
184