summaryrefslogtreecommitdiff
path: root/framework/Collections/TPagedDataSource.php
blob: 7c6c4a2189ee14ab3e13f9e2e29e81b279f341c2 (plain)
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
<?php
/**
 * TPagedDataSource, TPagedListIterator, TPagedMapIterator classes
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @version $Id: TPagedDataSource.php 3245 2013-01-07 20:23:32Z ctrlaltca $
 * @package System.Collections
 */

/**
 * TPagedDataSource class
 *
 * TPagedDataSource implements an integer-indexed collection class with paging functionality.
 *
 * Data items in TPagedDataSource can be traversed using <b>foreach</b>
 * PHP statement like the following,
 * <code>
 * foreach($pagedDataSource as $dataItem)
 * </code>
 * The data are fetched from {@link setDataSource DataSource}. Only the items
 * within the specified page will be returned and traversed.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Id: TPagedDataSource.php 3245 2013-01-07 20:23:32Z ctrlaltca $
 * @package System.Collections
 * @since 3.0
 */
class TPagedDataSource extends TComponent implements IteratorAggregate,Countable
{
	/**
	 * @var mixed original data source
	 */
	private $_dataSource=null;
	/**
	 * @var integer number of items in each page
	 */
	private $_pageSize=10;
	/**
	 * @var integer current page index
	 */
	private $_currentPageIndex=0;
	/**
	 * @var boolean whether to allow paging
	 */
	private $_allowPaging=false;
	/**
	 * @var boolean whether to allow custom paging
	 */
	private $_allowCustomPaging=false;
	/**
	 * @var integer user-assigned number of items in data source
	 */
	private $_virtualCount=0;

	/**
	 * @return mixed original data source. Defaults to null.
	 */
	public function getDataSource()
	{
		return $this->_dataSource;
	}

	/**
	 * @param mixed original data source
	 */
	public function setDataSource($value)
	{
		if(!($value instanceof TMap) && !($value instanceof TList))
		{
			if(is_array($value))
				$value=new TMap($value);
			else if($value instanceof Traversable)
				$value=new TList($value);
			else if($value!==null)
				throw new TInvalidDataTypeException('pageddatasource_datasource_invalid');
		}
		$this->_dataSource=$value;
	}

	/**
	 * @return integer number of items in each page. Defaults to 10.
	 */
	public function getPageSize()
	{
		return $this->_pageSize;
	}

	/**
	 * @param integer number of items in each page
	 */
	public function setPageSize($value)
	{
		if(($value=TPropertyValue::ensureInteger($value))>0)
			$this->_pageSize=$value;
		else
			throw new TInvalidDataValueException('pageddatasource_pagesize_invalid');
	}

	/**
	 * @return integer current page index. Defaults to 0.
	 */
	public function getCurrentPageIndex()
	{
		return $this->_currentPageIndex;
	}

	/**
	 * @param integer current page index
	 */
	public function setCurrentPageIndex($value)
	{
		if(($value=TPropertyValue::ensureInteger($value))<0)
			$value=0;
		$this->_currentPageIndex=$value;
	}

	/**
	 * @return boolean whether to allow paging. Defaults to false.
	 */
	public function getAllowPaging()
	{
		return $this->_allowPaging;
	}

	/**
	 * @param boolean whether to allow paging
	 */
	public function setAllowPaging($value)
	{
		$this->_allowPaging=TPropertyValue::ensureBoolean($value);
	}

	/**
	 * @return boolean whether to allow custom paging. Defaults to false.
	 */
	public function getAllowCustomPaging()
	{
		return $this->_allowCustomPaging;
	}

	/**
	 * @param boolean whether to allow custom paging
	 */
	public function setAllowCustomPaging($value)
	{
		$this->_allowCustomPaging=TPropertyValue::ensureBoolean($value);
	}

	/**
	 * @return integer user-assigned number of items in data source Defaults to 0.
	 */
	public function getVirtualItemCount()
	{
		return $this->_virtualCount;
	}

	/**
	 * @param integer user-assigned number of items in data source
	 */
	public function setVirtualItemCount($value)
	{
		if(($value=TPropertyValue::ensureInteger($value))>=0)
			$this->_virtualCount=$value;
		else
			throw new TInvalidDataValueException('pageddatasource_virtualitemcount_invalid');
	}

	/**
	 * @return integer number of items in current page
	 */
	public function getCount()
	{
		if($this->_dataSource===null)
			return 0;
		if(!$this->_allowPaging)
			return $this->getDataSourceCount();
		if(!$this->_allowCustomPaging && $this->getIsLastPage())
			return $this->getDataSourceCount()-$this->getFirstIndexInPage();
		return $this->_pageSize;
	}

	/**
	 * Returns the number of items in the current page.
	 * This method is required by Countable interface.
	 * @return integer number of items in the current page.
	 */
	public function count()
	{
		return $this->getCount();
	}

	/**
	 * @return integer number of pages
	 */
	public function getPageCount()
	{
		if($this->_dataSource===null)
			return 0;
		$count=$this->getDataSourceCount();
		if(!$this->_allowPaging || $count<=0)
			return 1;
		return (int)(($count+$this->_pageSize-1)/$this->_pageSize);
	}

	/**
	 * @return boolean whether the current page is the first page Defaults to false.
	 */
	public function getIsFirstPage()
	{
		if($this->_allowPaging)
			return $this->_currentPageIndex===0;
		else
			return true;
	}

	/**
	 * @return boolean whether the current page is the last page
	 */
	public function getIsLastPage()
	{
		if($this->_allowPaging)
			return $this->_currentPageIndex===$this->getPageCount()-1;
		else
			return true;
	}

	/**
	 * @return integer the index of the item in data source, where the item is the first in
	 * current page
	 */
	public function getFirstIndexInPage()
	{
		if($this->_dataSource!==null && $this->_allowPaging && !$this->_allowCustomPaging)
			return $this->_currentPageIndex*$this->_pageSize;
		else
			return 0;
	}

	/**
	 * @return integer number of items in data source, if available
	 */
	public function getDataSourceCount()
	{
		if($this->_dataSource===null)
			return 0;
		else if($this->_allowCustomPaging)
			return $this->_virtualCount;
		else
			return $this->_dataSource->getCount();
	}

	/**
	 * @return Iterator iterator
	 */
	public function getIterator()
	{
		if($this->_dataSource instanceof TList)
			return new TPagedListIterator($this->_dataSource,$this->getFirstIndexInPage(),$this->getCount());
		else if($this->_dataSource instanceof TMap)
			return new TPagedMapIterator($this->_dataSource,$this->getFirstIndexInPage(),$this->getCount());
		else
			return null;
	}
}



/**
 * TPagedListIterator class
 *
 * TPagedListIterator implements Iterator interface.
 *
 * TPagedListIterator is used by {@link TPagedDataSource}. It allows TPagedDataSource
 * to return a new iterator for traversing the items in a {@link TList} object.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Id: TPagedDataSource.php 3245 2013-01-07 20:23:32Z ctrlaltca $
 * @package System.Collections
 * @since 3.0
 */
class TPagedListIterator implements Iterator
{
	private $_list;
	private $_startIndex;
	private $_count;
	private $_index;

	/**
	 * Constructor.
	 * @param TList the data to be iterated through
	 * @param integer start index
	 * @param integer number of items to be iterated through
	 */
	public function __construct(TList $list,$startIndex,$count)
	{
		$this->_list=$list;
		$this->_index=0;
		$this->_startIndex=$startIndex;
		if($startIndex+$count>$list->getCount())
			$this->_count=$list->getCount()-$startIndex;
		else
			$this->_count=$count;
	}

	/**
	 * Rewinds internal array pointer.
	 * This method is required by the interface Iterator.
	 */
	public function rewind()
	{
		$this->_index=0;
	}

	/**
	 * Returns the key of the current array item.
	 * This method is required by the interface Iterator.
	 * @return integer the key of the current array item
	 */
	public function key()
	{
		return $this->_index;
	}

	/**
	 * Returns the current array item.
	 * This method is required by the interface Iterator.
	 * @return mixed the current array item
	 */
	public function current()
	{
		return $this->_list->itemAt($this->_startIndex+$this->_index);
	}

	/**
	 * Moves the internal pointer to the next array item.
	 * This method is required by the interface Iterator.
	 */
	public function next()
	{
		$this->_index++;
	}

	/**
	 * Returns whether there is an item at current position.
	 * This method is required by the interface Iterator.
	 * @return boolean
	 */
	public function valid()
	{
		return $this->_index<$this->_count;
	}
}

/**
 * TPagedMapIterator class
 *
 * TPagedMapIterator implements Iterator interface.
 *
 * TPagedMapIterator is used by {@link TPagedDataSource}. It allows TPagedDataSource
 * to return a new iterator for traversing the items in a {@link TMap} object.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @version $Id: TPagedDataSource.php 3245 2013-01-07 20:23:32Z ctrlaltca $
 * @package System.Collections
 * @since 3.0
 */
class TPagedMapIterator implements Iterator
{
	private $_map;
	private $_startIndex;
	private $_count;
	private $_index;
	private $_iterator;

	/**
	 * Constructor.
	 * @param array the data to be iterated through
	 */
	public function __construct(TMap $map,$startIndex,$count)
	{
		$this->_map=$map;
		$this->_index=0;
		$this->_startIndex=$startIndex;
		if($startIndex+$count>$map->getCount())
			$this->_count=$map->getCount()-$startIndex;
		else
			$this->_count=$count;
		$this->_iterator=$map->getIterator();
	}

	/**
	 * Rewinds internal array pointer.
	 * This method is required by the interface Iterator.
	 */
	public function rewind()
	{
		$this->_iterator->rewind();
		for($i=0;$i<$this->_startIndex;++$i)
			$this->_iterator->next();
		$this->_index=0;
	}

	/**
	 * Returns the key of the current array item.
	 * This method is required by the interface Iterator.
	 * @return integer the key of the current array item
	 */
	public function key()
	{
		return $this->_iterator->key();
	}

	/**
	 * Returns the current array item.
	 * This method is required by the interface Iterator.
	 * @return mixed the current array item
	 */
	public function current()
	{
		return $this->_iterator->current();
	}

	/**
	 * Moves the internal pointer to the next array item.
	 * This method is required by the interface Iterator.
	 */
	public function next()
	{
		$this->_index++;
		$this->_iterator->next();
	}

	/**
	 * Returns whether there is an item at current position.
	 * This method is required by the interface Iterator.
	 * @return boolean
	 */
	public function valid()
	{
		return $this->_index<$this->_count;
	}
}