http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 9 LOC: 208 Statements: 37

Source file Statements Methods Total coverage
Enum.php 91.9% 100.0% 93.5%
   
1
<?php
2
/**
3
 * Xyster Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://www.opensource.org/licenses/bsd-license.php
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to xyster@devweblog.org so we can send you a copy immediately.
14
 *
15
 * @category  Xyster
16
 * @package   Xyster
17
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
18
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
19
 * @version   $Id$
20
 */
21
22
/**
23
 * @see Zend_Loader
24
 */
25 1
require_once 'Zend/Loader.php';
26
27
/**
28
 * Enumerable type object
29
 *
30
 * PHP contains no enum-type class internally, so to mirror the convenience such
31
 * a class offers, we created the Xyster_Enum.  Enum classes are used to
32
 * represent set types of things, for instance, if we created a class to
33
 * represent different operating systems, it might provide enum methods like
34
 * this:
35
 *
36
 * <code>
37
 * $unix = OperatingSystem::Unix();
38
 * $win = OperatingSystem::Windows();
39
 * $mac = OperatingSystem::Mac();
40
 * echo $unix->getName(); // prints Unix
41
 * </code>
42
 *
43
 * @category  Xyster
44
 * @package   Xyster
45
 * @copyright Copyright (c) 2007 Irrational Logic (http://devweblog.org)
46
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
47
 */
48
abstract class Xyster_Enum
49
{
50
    /**
51
     * The enum name
52
     *
53
     * @var string
54
     */
55
	private $_name;
56
	/**
57
	 * The enum value (usually a number, but doesn't have to be)
58
	 *
59
	 * @var mixed
60
	 */
61
	private $_value;
62
63
	/**
64
	 * Static cache for factoried enum instances
65
	 *
66
	 * @var array
67
	 */
68
	static private $_instances = array();
69
70
	/**
71
	 * Creates a new enum derived class
72
	 *
73
	 * @param string $name  Enum name
74
	 * @param mixed $value	Enum value
75
	 */
76
	final protected function __construct( $name, $value )
77
	{
78 5
		$this->_name = $name;
79 5
		$this->_value = $value;
80
	}
81
82
	/**
83
	 * Cannot be cloned
84
	 *
85
	 * @magic
86
	 */
87
	public function __clone()
88
	{
89 1
		throw new Exception;
90
	}
91
92
	/**
93
	 * Gets the string name of this enum type
94
	 *
95
	 * @return string  Name of this enum
96
	 */
97
	public function getName()
98
	{
99 6
		return $this->_name;
100
	}
101
	/**
102
	 * Gets the value of this enum type
103
	 *
104
	 * @return mixed  Value of this enum
105
	 */
106
	public function getValue()
107
	{
108 25
		return $this->_value;
109
	}
110
	/**
111
	 * Gets details of the object
112
	 *
113
	 * @magic
114
	 * @return string  Details of object
115
	 */
116
	public function __toString()
117
	{
118 6
		return get_class($this)." [".$this->_value.",".$this->_name."]";
119
	}
120
121
	/**
122
	 * Returns the corresponding enum object based on name
123
	 *
124
	 * <code>
125
	 * $color = Xyster_Enum::parse('Colors','red'); // name is case insensitive
126
	 * </code>
127
	 *
128
	 * @param string $className  Name of the Xyster_Enum-derived class to instantiate
129
	 * @param string $name  Name to parse
130
	 * @return Xyster_Enum  Translated object
131
	 * @throws Exception  if $name was not found in $className
132
	 */
133
	static public function parse( $className, $name )
134
	{
135 7
		foreach( self::values($className) as $constValue=>$constName ) {
136 7
			if ( strcasecmp( $constName, $name ) == 0 ) {
137 6
				return self::_factory($className, $constName);
138 0
			}
139 6
		}
140 1
		throw new Exception();
141
	}
142
	/**
143
	 * Returns the corresponding enum object based on value
144
	 *
145
	 * <code>
146
	 * $color = Xyster_Enum::valueOf('Colors',0);
147
	 * </code>
148
	 *
149
	 * @param string $className  Name of the Xyster_Enum-derived class to instantiate
150
	 * @param string $value  Value to parse
151
	 * @return Xyster_Enum  Translated object
152
	 * @throws Exception  if $className isn't derived from Xyster_Enum
153
	 */
154
	static public function valueOf( $className, $value )
155
	{
156 20
		foreach( self::values($className) as $constValue=>$constName ) {
157 20
			if ( strcasecmp( $constValue, $value ) == 0 ) {
158 19
				return self::_factory($className, $constName);
159 0
			}
160 19
		}
161 1
		throw new Exception();
162
	}
163
	/**
164
	 * Gets an array of the name and value pairs available from an enum
165
	 *
166
	 * The associative array contains the enum values as keys and the enum
167
	 * constant names as values.  For example, the Colors enum might return:
168
	 * <code>
169
	 * return array( 0=>'Red', 1=>'Orange', 2=>'Yellow', 3=>'Green' );
170
	 * </code>
171
	 *
172
	 * @param string $className
173
	 * @return array
174
	 * @throws Exception  if $className isn't derived from Xyster_Enum
175
	 */
176
	static public function values( $className )
177
	{
178 22
	    Zend_Loader::loadClass($className);
179 22
		if ( !is_subclass_of($className, __CLASS__) ) {
180 1
			throw new Exception();
181 0
		}
182 21
		$rc = new ReflectionClass($className);
183 21
		return array_flip($rc->getConstants());
184
	}
185
186
	/**
187
	 * Factories and returns a singleton enum
188
	 *
189
	 * @param string $className
190
	 * @param mixed $name
191
	 * @return Xyster_Enum
192
	 */
193
	static protected function _factory( $className = null, $name = null )
194
	{
195 42
		if ( $className == null ) {
196 33
			$bt = debug_backtrace();
197 33
			$className = $bt[1]['class'];
198 33
			$name = $bt[1]['function'];
199 33
		}
200 42
		if ( !isset(self::$_instances[$className][$name]) ) {
201 5
			$rc = new ReflectionClass($className);
202 5
			self::$_instances[$className][$name] =
203 5
			    new $className( $name, $rc->getConstant($name) );
204 5
		}
205 42
		return self::$_instances[$className][$name];
206
	}
207
}
208
?>


Report generated at 2007-11-05T09:09:02-05:00