Created
October 29, 2014 12:25
-
-
Save orhanveli/c7177e090b5d21e42cc1 to your computer and use it in GitHub Desktop.
php enum implementations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*** | |
| * Class DfTypedEnum | |
| * | |
| * http://stackoverflow.com/a/15010599/287084 | |
| * | |
| */ | |
| abstract class TypedEnum | |
| { | |
| /** @var array cache of all enum instances by class name and integer value */ | |
| private static $allEnumMembers = array(); | |
| /** @var mixed */ | |
| private $code; | |
| /** @var string */ | |
| private $description; | |
| /** | |
| * Return an enum instance of the concrete type on which this static method is called, assuming an instance | |
| * exists for the passed in value. Otherwise an exception is thrown. | |
| * | |
| * @param $code | |
| * @return AbstractEnum | |
| * @throws Exception | |
| */ | |
| public static function getByCode($code) | |
| { | |
| $concreteMembers = &self::getConcreteMembers(); | |
| if (array_key_exists($code, $concreteMembers)) { | |
| return $concreteMembers[$code]; | |
| } | |
| $called = get_called_class(); | |
| throw new Exception("Value '$code' does not exist for enum '$called'"); | |
| } | |
| public static function getAllMembers() | |
| { | |
| return self::getConcreteMembers(); | |
| } | |
| /** | |
| * Create, cache and return an instance of the concrete enum type for the supplied primitive value. | |
| * | |
| * @param mixed $code code to uniquely identify this enum | |
| * @param string $description | |
| * @throws Exception | |
| * @return AbstractEnum | |
| */ | |
| protected static function enum($code, $description) | |
| { | |
| $concreteMembers = &self::getConcreteMembers(); | |
| $thisClassName = get_called_class(); | |
| if (array_key_exists($code, $concreteMembers)) { | |
| throw new Exception("Value '$code' has already been added to enum '".get_called_class()."'"); | |
| } | |
| $concreteMembers[$code] = $concreteEnumInstance = new static($code, $description); | |
| self::$allEnumMembers[$thisClassName] = $concreteMembers; | |
| return $concreteEnumInstance; | |
| } | |
| /** | |
| * @return AbstractEnum[] | |
| */ | |
| private static function &getConcreteMembers() { | |
| $thisClassName = get_called_class(); | |
| if (!array_key_exists($thisClassName, self::$allEnumMembers)) { | |
| $concreteMembers = array(); | |
| self::$allEnumMembers[$thisClassName] = $concreteMembers; | |
| } | |
| // $ref = new ReflectionClass($thisClassName); | |
| // $statics = $ref->getStaticProperties(); | |
| // | |
| // foreach ($statics as $name => $value) { | |
| // $ref->setStaticPropertyValue($name, new $class($value)); | |
| // $concreteMembers[] | |
| // } | |
| return self::$allEnumMembers[$thisClassName]; | |
| } | |
| private function __construct($code, $description) | |
| { | |
| $this->code = $code; | |
| $this->description = $description; | |
| } | |
| public function getCode() | |
| { | |
| return $this->code; | |
| } | |
| public function getDescription() | |
| { | |
| return $this->description; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ContentStatus extends DfTypedEnum { | |
| public static $Draft; | |
| public static $Published; | |
| public static function _init() | |
| { | |
| self::$Draft = self::enum(0, 'Taslak'); | |
| self::$Published = self::enum(1, 'Yayınlanmış'); | |
| } | |
| } | |
| //should be init | |
| ContentStatus::_init(); | |
| echo ContentStatus::$Draft->getCode().' : '.ContentStatus::$Draft->getDescription().PHP_EOL.PHP_EOL; | |
| var_dump(ContentStatus::getAllMembers()); | |
| echo PHP_EOL.ContentStatus::getByCode(1)->getDescription().PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment