Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TDummyDataSource, TDummyDataSourceIterator classes
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TDummyDataSource.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Collections
11
 */
12
 
13
/**
14
 * TDummyDataSource class
15
 *
16
 * TDummyDataSource implements a dummy data collection with a specified number
17
 * of dummy data items. The number of virtual items can be set via
18
 * {@link setCount Count} property. You can traverse it using <b>foreach</b>
19
 * PHP statement like the following,
20
 * <code>
21
 * foreach($dummyDataSource as $dataItem)
22
 * </code>
23
 *
24
 * @author Qiang Xue <qiang.xue@gmail.com>
25
 * @version $Id: TDummyDataSource.php 2541 2008-10-21 15:05:13Z qiang.xue $
26
 * @package System.Collections
27
 * @since 3.0
28
 */
29
class TDummyDataSource extends TComponent implements IteratorAggregate, Countable
30
{
31
	private $_count;
32
 
33
	/**
34
	 * Constructor.
35
	 * @param integer number of (virtual) items in the data source.
36
	 */
37
	public function __construct($count)
38
	{
39
		$this->_count=$count;
40
	}
41
 
42
	/**
43
	 * @return integer number of (virtual) items in the data source.
44
	 */
45
	public function getCount()
46
	{
47
		return $this->_count;
48
	}
49
 
50
	/**
51
	 * @return Iterator iterator
52
	 */
53
	public function getIterator()
54
	{
55
		return new TDummyDataSourceIterator($this->_count);
56
	}
57
 
58
	/**
59
	 * Returns the number of (virtual) items in the data source.
60
	 * This method is required by Countable interface.
61
	 * @return integer number of (virtual) items in the data source.
62
	 */
63
	public function count()
64
	{
65
		return $this->getCount();
66
	}
67
}
68
 
69
/**
70
 * TDummyDataSourceIterator class
71
 *
72
 * TDummyDataSourceIterator implements Iterator interface.
73
 *
74
 * TDummyDataSourceIterator is used by {@link TDummyDataSource}.
75
 * It allows TDummyDataSource to return a new iterator
76
 * for traversing its dummy items.
77
 *
78
 * @author Qiang Xue <qiang.xue@gmail.com>
79
 * @version $Id: TDummyDataSource.php 2541 2008-10-21 15:05:13Z qiang.xue $
80
 * @package System.Collections
81
 * @since 3.0
82
 */
83
class TDummyDataSourceIterator implements Iterator
84
{
85
	private $_index;
86
	private $_count;
87
 
88
	/**
89
	 * Constructor.
90
	 * @param integer number of (virtual) items in the data source.
91
	 */
92
	public function __construct($count)
93
	{
94
		$this->_count=$count;
95
		$this->_index=0;
96
	}
97
 
98
	/**
99
	 * Rewinds internal array pointer.
100
	 * This method is required by the interface Iterator.
101
	 */
102
	public function rewind()
103
	{
104
		$this->_index=0;
105
	}
106
 
107
	/**
108
	 * Returns the key of the current array item.
109
	 * This method is required by the interface Iterator.
110
	 * @return integer the key of the current array item
111
	 */
112
	public function key()
113
	{
114
		return $this->_index;
115
	}
116
 
117
	/**
118
	 * Returns the current array item.
119
	 * This method is required by the interface Iterator.
120
	 * @return mixed the current array item
121
	 */
122
	public function current()
123
	{
124
		return null;
125
	}
126
 
127
	/**
128
	 * Moves the internal pointer to the next array item.
129
	 * This method is required by the interface Iterator.
130
	 */
131
	public function next()
132
	{
133
		$this->_index++;
134
	}
135
 
136
	/**
137
	 * Returns whether there is an item at current position.
138
	 * This method is required by the interface Iterator.
139
	 * @return boolean
140
	 */
141
	public function valid()
142
	{
143
		return $this->_index<$this->_count;
144
	}
145
}
146