Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /**
- * ILLI
- *
- * @category ILLI
- * @package ILLI
- * @subpackage SystemType
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- */
- /**
- * ILLI System Type Enum
- *
- * @category ILLI
- * @package ILLI
- * @subpackage SystemType
- * @link http://illi.be
- * @license http://l.illi.be
- * @copyright ILLI Conference
- *
- * @todo bit flag for
- * strict type
- * unique values
- * allow __default as NULL
- *
- * @todo array_shift in getDefinedName() and getDefaultName()? sure?
- *
- * @author ~luc <luc[at]s.illi.be>
- */
- ABSTRACT CLASS ILLI_System_Type_Enum EXTENDS ILLI_System_Type_Abstract
- {
- private static $__reflectionBase = NULL;
- private static $__constantsBase = array();
- private $__definedConstants = NULL;
- private $__definedDefault = NULL;
- private $__initValue = NULL;
- private $__definedValue = NULL;
- final public function __construct($default = NULL)
- {
- if(NULL === self::$__reflectionBase)
- {
- self::$__reflectionBase = new ReflectionClass(__CLASS__);
- self::$__constantsBase = self::$__reflectionBase->getConstants();
- }
- $ref = new ReflectionClass($this->getQualifiedName());
- $con = $ref->getConstants();
- // hide constants from base
- $diff = array_intersect_assoc(self::$__constantsBase, $con);
- foreach($diff as $const => $val)
- unset($con[$const]);
- $this->__definedConstants = new ILLI_System_Type_AssocArray($con);
- if(sizeOf($con) <= 1)
- throw new ILLI_Exception_ArgumentExpected
- (
- ILLI_System_E::ARGUMENT_EXPECTED_ENUMERABLE_CONSTANT_EXPECTED,
- array($this->getQualifiedName())
- );
- if(FALSE === $this->__definedConstants->offsetExists('__DEFAULT'))
- throw new ILLI_Exception_ArgumentExpected
- (
- ILLI_System_E::ARGUMENT_EXPECTED_ENUMERABLE_DEFAULT_EXPECTED,
- array
- (
- $this->getQualifiedName(),
- '__DEFAULT'
- )
- );
- $this->__definedDefault = $this->__definedConstants->offsetGet('__DEFAULT');
- if(NULL === $this->__definedDefault)
- throw new ILLI_Exception_ArgumentExpected
- (
- ILLI_System_E::ARGUMENT_EXPECTED_ENUMERABLE_IS_NULL,
- array
- (
- $this->getQualifiedName(),
- '__DEFAULT'
- )
- );
- $this->__definedConstants->offsetUnset('__DEFAULT');
- $cache = array();
- foreach($this->__definedConstants as $const => $val)
- {
- if(array_key_exists($val, $cache))
- throw new ILLI_Exception_ArgumentExpected
- (
- ILLI_System_E::ARGUMENT_EXPECTED_ENUMERABLE_UNIQUE_EXPECTED,
- array
- (
- $this->getQualifiedName(),
- $const,
- $val,
- $cache[$val]
- )
- );
- $cache[$val] = $const;
- }
- if(FALSE === $this->isDefinedValue($this->__definedDefault))
- throw new ILLI_Exception_ArgumentExpected
- (
- ILLI_SYSTEM_E::ARGUMENT_EXPECTED_ENUMERABLE_DEFINED_EXPECTED,
- array
- (
- $this->getQualifiedName(),
- '__DEFAULT',
- $this->__definedDefault
- )
- );
- if(NULL === $default)
- $default = $this->__definedDefault;
- $this->define($default);
- $this->__initValue = $default;
- }
- final public function define($value)
- {
- try
- {
- if(FALSE === $this->isDefinedValue($value))
- throw new ILLI_Exception_ArgumentOutOfRange
- (
- ILLI_SYSTEM_E::ARGUMENT_OUT_OF_ENUM_INDEX,
- array($value)
- );
- }
- catch(ILLI_Exception_ArgumentOutOfRange $e)
- {
- throw $e;
- }
- catch(ILLI_Exception_Argument $e)
- {
- throw new ILLI_Exception_ArgumentOutOfRange
- (
- ILLI_SYSTEM_E::ARGUMENT_OUT_OF_ENUM_INDEX,
- $e,
- array($const)
- );
- }
- $this->__definedValue = $value;
- return $this;
- }
- final public function getValues()
- {
- return $this->__definedConstants->getValues();
- }
- final public function getNames($value = NULL)
- {
- return $this->__definedConstants->getOffsets($value);
- }
- final public function getValue($const)
- {
- try
- {
- if(FALSE === $this->isDefinedConst($const))
- throw new ILLI_Exception_ArgumentOutOfRange
- (
- ILLI_SYSTEM_E::ARGUMENT_OUT_OF_ENUM_INDEX,
- array($const)
- );
- }
- catch(ILLI_Exception_ArgumentOutOfRange $e)
- {
- throw $e;
- }
- catch(ILLI_Exception_Argument $e)
- {
- throw new ILLI_Exception_ArgumentOutOfRange
- (
- ILLI_SYSTEM_E::ARGUMENT_OUT_OF_ENUM_INDEX,
- $e,
- array($const)
- );
- }
- return $this->__definedConstants->offsetGet($const);
- }
- final public function reset()
- {
- $this->define($this->__initValue);
- return $this;
- }
- final public function getDefinedValue()
- {
- return $this->__definedValue;
- }
- final public function getDefinedName()
- {
- $res = $this->getDefinedNames();
- return array_shift($res);
- }
- final public function getDefinedNames()
- {
- return $this->getNames($this->__definedValue);
- }
- final public function getDefaultValue()
- {
- return $this->__initValue;
- }
- final public function getDefaultName()
- {
- $res = $this->getDefaultNames();
- return array_shift($res);
- }
- final public function getDefaultNames()
- {
- return $this->getNames($this->__initValue);
- }
- public function __toString()
- {
- return (string) $this->getDefinedValue();
- }
- public function __invoke()
- {
- return $this->getDefinedValue();
- }
- private function isDefinedValue($value)
- {
- if(NULL === $value)
- throw new ILLI_Exception_ArgumentNull;
- if(!is_integer($value))
- throw new ILLI_Exception_ArgumentExpected
- (
- ILLI_System_E::ARGUMENT_EXPECTED_INTEGER
- );
- foreach($this->__definedConstants as $key => $val)
- if($value === $val)
- return TRUE;
- return FALSE;
- }
- private function isDefinedConst($const)
- {
- if(NULL === $const)
- throw new ILLI_Exception_ArgumentNull;
- if(!is_string($const))
- throw new ILLI_Exception_ArgumentExpected
- (
- ILLI_System_E::ARGUMENT_EXPECTED_STRING
- );
- return $this->__definedConstants->offsetExists($const);
- }
- }
- class Fruit extends ILLI_System_Type_Enum
- {
- const __DEFAULT = 1;
- const APPLE = 1;
- const BANANA = 2;
- const ORANGE = 3;
- }
- function eat(Fruit $aFruit)
- {
- if (Fruit::APPLE == $aFruit()) {
- echo "Eating an apple.\n";
- } elseif (Fruit::ORANGE == $aFruit()) {
- echo "Eating an orange.\n";
- } elseif (Fruit::BANANA == $aFruit()) {
- echo "Eating an banana.\n";
- }
- }
- $myApple = new Fruit();
- eat($myApple); // Eating an apple.
- $myOrange = new Fruit(Fruit::ORANGE);
- eat($myOrange); // Eating an orange.
- eat($myOrange->define(Fruit::BANANA)); // switch to banana.
- eat($myOrange->reset()); // reset to orange.
- $myCopy = $myOrange;
- var_dump($myCopy);
- /**
- Eating an apple.
- Eating an orange.
- Eating an banana.
- Eating an orange.
- object(Fruit)#5 (4) {
- ["__definedConstants":"ILLI_System_Type_Enum":private]=>
- object(ILLI_System_Type_AssocArray)#7 (2) {
- ["__array":"ILLI_System_Type_ArrayAbstract":private]=>
- array(3) {
- ["APPLE"]=>
- int(1)
- ["BANANA"]=>
- int(2)
- ["ORANGE"]=>
- int(3)
- }
- ["__protMode":"ILLI_System_Type_ArrayAbstract":private]=>
- int(0)
- }
- ["__definedDefault":"ILLI_System_Type_Enum":private]=>
- int(1)
- ["__initValue":"ILLI_System_Type_Enum":private]=>
- int(3)
- ["__definedValue":"ILLI_System_Type_Enum":private]=>
- int(3)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement