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