summaryrefslogtreecommitdiff
path: root/framework/TEnumerable.php
blob: 8dd428577981d9bee8605a3dc4a79fbd5b8fc9d8 (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
<?php
/**
 * TComponent, TPropertyValue classes
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 *
 * Global Events, intra-object events, Class behaviors, expanded behaviors
 * @author Brad Anderson <javalizard@mac.com>
 *
 * @link http://www.pradosoft.com/
 * @copyright Copyright &copy; 2005-2014 PradoSoft
 * @license http://www.pradosoft.com/license/
 * @package System
 */


/**
 * TEnumerable class.
 * TEnumerable is the base class for all enumerable types.
 * To define an enumerable type, extend TEnumberable and define string constants.
 * Each constant represents an enumerable value.
 * The constant name must be the same as the constant value.
 * For example,
 * <code>
 * class TTextAlign extends TEnumerable
 * {
 *     const Left='Left';
 *     const Right='Right';
 * }
 * </code>
 * Then, one can use the enumerable values such as TTextAlign::Left and
 * TTextAlign::Right.
 *
 * @author Qiang Xue <qiang.xue@gmail.com>
 * @package System
 * @since 3.0
 */
class TEnumerable implements Iterator
{
	private $_enums=array();

	public function __construct() {
		$reflection=new ReflectionClass($this);
		$this->_enums=$reflection->getConstants();
	}

	public function current() {
		return current($this->_enums);
	}

	public function key() {
		return key($this->_enums);
	}

	public function next() {
		return next($this->_enums);
	}

	public function rewind() {
		reset($this->_enums);
	}

	public function valid() {
		return $this->current()!==false;
	}
}