http://phing.info/

Source Code Coverage

Designed for use with PHPUnit2, Xdebug and Phing.

Methods: 9 LOC: 202 Statements: 37

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


Report generated at 2008-01-20T12:13:39-05:00