forked from maintainable/framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.php
327 lines (290 loc) · 8.12 KB
/
Collection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* @category Mad
* @package Mad_Model
* @copyright (c) 2007-2009 Maintainable Software, LLC
* @license http://opensource.org/licenses/bsd-license.php BSD
*/
/**
* When a using a {@link Mad_Model_Base} wrapper around a result set of objects, we return
* multiple models as a Mad_Model_Collection object which behaves just as if it were an array.
*
* The advantage of using this object instead of an actual array is that we don't need
* to iterate through the entire result set to use only a subset of the results. This
* allows us to only access the elements of the result set as needed.
*
* @category Mad
* @package Mad_Model
* @copyright (c) 2007-2009 Maintainable Software, LLC
* @license http://opensource.org/licenses/bsd-license.php BSD
*/
class Mad_Model_Collection implements ArrayAccess, Iterator, Countable
{
/**
* The {@link Mad_Model_Base} class used to instantiate new objects
* @var object
*/
protected $_model;
/**
* As we access each row, we cache them in an array for later access.
* @var array
*/
protected $_collection = array();
/**
* The position in the iterator over the objects.
* @var int
*/
protected $_position = 0;
/*##########################################################################
# Construct/Destruct
##########################################################################*/
/**
* Construct a Mad_Model_Collection from a model instance and the collection of objects which
* is wither a QueryResult object or array of model objects.
*
* @param object $model
* @param object $collection
*/
public function __construct(Mad_Model_Base $model, $collection=null)
{
// object types in this collection
$this->_model = $model;
// Iterate over query result
if (!current($collection) instanceof Mad_Model_Base) {
$this->_initResults($model, $collection);
// Use existing array
} else {
$this->_collection = $collection;
}
}
/**
* Loop thru and print out classname and primary key of objects
* @return string
*/
public function __toString()
{
if (!$first = current($this->_collection)) { return 'Empty Collection'; }
$str = get_class($first)." Collection\n";
foreach ($this as $item) {
$items[] = get_class($item). ': '.$item->id;
}
return $str . (isset($items) ? "\n ".implode("\n ", $items) : '');
}
/*##########################################################################
# Accessors
##########################################################################*/
public function getCollection()
{
return $this->_collection;
}
/**
* Serialize the collection to XML.
*/
public function toXml($options = array())
{
if (!isset($options['root'])) {
$options['root'] = Mad_Support_Inflector::pluralize(get_class($this->_model));
}
$conversion = new Mad_Support_ArrayConversion;
return $conversion->toXml($this, $options);
}
/*##########################################################################
# Countable Interface
##########################################################################*/
/**
* Count elements in the array. This has to force load all the object into memory
* to get the count.
*
* <code>
* <?php
* ...
* $folders->count()
* ...
*
* // This will work with php5.1
* count($folders);
* ...
* ?>
* </code>
* @return int
*/
public function count()
{
return count($this->_collection);
}
/*##########################################################################
# Iterator Interface
##########################################################################*/
/**
* Get the current object from the collection
*
* <code>
* <?php
* ...
* current($folders);
* ...
* ?>
* </code>
* @return Mad_Model_Base
*/
public function current()
{
return $this->offsetGet($this->_position);
}
/**
* Get the current position in the Collection
*
* <code>
* <?php
* ...
* key($folders);
* ...
* ?>
* </code>
*
* @return int
*/
public function key()
{
return $this->_position;
}
/**
* Get the next element on the Collection
*
* <code>
* <?php
* ...
* next($folders);
* ...
* ?>
* </code>
*
* @return Mad_Model_Base
*/
public function next()
{
$this->_position++;
return $this->current();
}
/**
* Rewind collection to first element
*
* <code>
* <?php
* ...
* rewind($folders);
* ...
* ?>
* </code>
*
*/
public function rewind()
{
$this->_position = 0;
return $this->current();
}
/**
* Check if the current element exists
* @return boolean
*/
public function valid()
{
return $this->offsetExists($this->_position);
}
/*##########################################################################
# ArrayAccess Interface
##########################################################################*/
/**
* Check if the given offset exists
*
* @param int $offset
* @return boolean
*/
public function offsetExists($offset)
{
return isset($this->_collection[$offset]);
}
/**
* Return the element for the given offset.
*
* <code>
* <?php
* ...
* $folder2 = $folders[1];
* ...
* ?>
* </code>
*
* @param int $offset
* @return Mad_Model_Base
*/
public function offsetGet($offset)
{
if ($this->offsetExists($offset)) {
return $this->_collection[$offset];
}
}
/**
* Collection is readonly, so this is not allowed (method required by interface)
*
* @param int $offset
* @param mixed $value
*/
public function offsetSet($offset, $value)
{
// Can only add Models to the collection
if ($value instanceof Mad_Model_Base) {
$this->_collection[] = $value;
}
}
/**
* Collection is readonly, so this is not allowed (method required by interface)
*
* @param int $offset
*/
public function offsetUnset($offset) {}
/**
* Iterate over each object in this collection, either accessing
* a property or calling a method, and return all of the
* results in an array.
*
* The first argument ($property) is interpreted as a property name.
* However, if the first argument ends with "()" then it will be
* interpreted as a method and varargs be passed to that method.
*
* @param string $property Property to access on each member
* @return array Results collected
*/
public function collect($property) {
$values = array();
if (substr($property, -2, 2) == '()') {
// method call
$method = rtrim($property, '()');
$args = func_get_args();
array_shift($args);
foreach ($this as $member) {
$callback = array($member, $method);
$values[] = call_user_func_array($callback, $args);
}
} else {
// property access
foreach ($this as $member) {
$values[] = $member->$property;
}
}
return $values;
}
/**
* Initialize result set into object collection
*
* @param object $model
* @param array $results
*/
protected function _initResults(Mad_Model_Base $model, $results)
{
while ($row = current($results)) {
$this->_collection[] = $model->instantiate($row);
next($results);
}
}
}