Advertisement
fruffl

Enumerable in PHP

Feb 25th, 2012
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI
  6.      * @package    ILLI
  7.      * @subpackage SystemType
  8.      * @link       http://illi.be
  9.      * @license    http://l.illi.be
  10.      * @copyright  ILLI Conference
  11.      */
  12.  
  13.     /**
  14.      * ILLI System Type Enum
  15.      *
  16.      * @category   ILLI
  17.      * @package    ILLI
  18.      * @subpackage SystemType
  19.      * @link       http://illi.be
  20.      * @license    http://l.illi.be
  21.      * @copyright  ILLI Conference
  22.      */
  23.     ABSTRACT CLASS ILLI_System_Type_Enum EXTENDS ILLI_System_Type_Abstract
  24.     {
  25.         private static $__reflectionBase    = NULL;
  26.         private static $__constantsBase     = array();
  27.        
  28.         private $__reflectionClass      = NULL;    
  29.         private $__constantsClass       = array();
  30.        
  31.         private $__definedConstants     = NULL;
  32.        
  33.         private $__definedDefault       = NULL;
  34.         private $__initValue            = NULL;
  35.         private $__definedValue         = NULL;
  36.        
  37.         const STRICT_INTEGER            = 0x00000001;
  38.         const STRICT_STRING         = 0x00000002;
  39.         const STRICT_FLOAT          = 0x00000004;
  40.        
  41.         const STRICT_SCALAR         = 0x00000007;
  42.        
  43.        
  44.         const STRICT_TYPE           = 0x00000001;
  45.        
  46.         private $mode               = 0;
  47.        
  48.        
  49.         final public function __construct($default = NULL)
  50.         {
  51.             if(NULL === self::$__reflectionBase)
  52.             {
  53.                 self::$__reflectionBase  = new ReflectionClass(__CLASS__);
  54.                 self::$__constantsBase   = self::$__reflectionBase->getConstants();
  55.             }
  56.             $this->__reflectionClass = new ReflectionClass($this->getQualifiedName());
  57.             $this->__constantsClass  = $this->__reflectionClass->getConstants();
  58.            
  59.             // hide constants from base
  60.             $diff = array_intersect_assoc(self::$__constantsBase, $this->__constantsClass);        
  61.             foreach($diff as $const => $val)
  62.                 unset($this->__constantsClass[$const]);
  63.            
  64.             $this->__definedConstants = new ILLI_System_Type_AssocArray($this->__constantsClass);
  65.            
  66.             if(sizeOf($this->__constantsClass) <= 1)
  67.                 throw new ILLI_Exception_ArgumentExpected
  68.                 (
  69.                     ILLI_System_E::ARGUMENT_EXPECTED_ENUMERABLE_CONSTANT_EXPECTED,
  70.                     array($this->getQualifiedName())
  71.                 );
  72.                
  73.             if(FALSE === $this->__definedConstants->offsetExists('__DEFAULT'))
  74.                 throw new ILLI_Exception_ArgumentExpected
  75.                 (
  76.                     ILLI_System_E::ARGUMENT_EXPECTED_ENUMERABLE_DEFAULT_EXPECTED,
  77.                     array
  78.                     (
  79.                         $this->getQualifiedName(),
  80.                         '__DEFAULT'
  81.                     )
  82.                 );
  83.                
  84.             $this->__definedDefault = $this->__definedConstants->offsetGet('__DEFAULT');
  85.            
  86.             if(NULL === $this->__definedDefault)
  87.                 throw new ILLI_Exception_ArgumentExpected
  88.                 (
  89.                     ILLI_System_E::ARGUMENT_EXPECTED_ENUMERABLE_IS_NULL,
  90.                     array
  91.                     (
  92.                         $this->getQualifiedName(),
  93.                         '__DEFAULT'
  94.                     )
  95.                 );
  96.            
  97.             $this->__definedConstants->offsetUnset('__DEFAULT');
  98.            
  99.             if(FALSE === $this->isDefinedValue($this->__definedDefault))
  100.                 throw new ILLI_Exception_ArgumentExpected
  101.                 (
  102.                     ILLI_SYSTEM_E::ARGUMENT_EXPECTED_ENUMERABLE_DEFINED_EXPECTED,
  103.                     array
  104.                     (
  105.                         $this->getQualifiedName(),
  106.                         '__DEFAULT',
  107.                         $this->__definedDefault
  108.                     )
  109.                 );
  110.            
  111.             if(NULL === $default)
  112.                 $default = $this->__definedDefault;
  113.                
  114.             $this->define($default);
  115.             $this->__initValue = $default;
  116.         }
  117.        
  118.         final public function define($value)
  119.         {
  120.             try
  121.             {
  122.                 if(FALSE === $this->isDefinedValue($value))
  123.                     throw new ILLI_Exception_ArgumentOutOfRange
  124.                     (
  125.                         ILLI_SYSTEM_E::ARGUMENT_OUT_OF_ENUM_INDEX,
  126.                         array($value)
  127.                     );
  128.             }
  129.             catch(ILLI_Exception_ArgumentOutOfRange $e)
  130.             {
  131.                 throw $e;
  132.             }
  133.             catch(ILLI_Exception_Argument $e)
  134.             {
  135.                 throw new ILLI_Exception_ArgumentOutOfRange
  136.                 (
  137.                     ILLI_SYSTEM_E::ARGUMENT_OUT_OF_ENUM_INDEX,
  138.                     $e,
  139.                     array($const)
  140.                 );
  141.             }
  142.            
  143.             $this->__definedValue = $value;
  144.         }
  145.        
  146.         final public function getValues()
  147.         {
  148.             return $this->__definedConstants->getValues();
  149.         }
  150.        
  151.         final public function getNames($value = NULL)
  152.         {
  153.             return $this->__definedConstants->getOffsets($value);
  154.         }
  155.        
  156.         final public function getValue($const)
  157.         {
  158.             try
  159.             {
  160.                 if(FALSE === $this->isDefinedConst($const))
  161.                     throw new ILLI_Exception_ArgumentOutOfRange
  162.                     (
  163.                         ILLI_SYSTEM_E::ARGUMENT_OUT_OF_ENUM_INDEX, 
  164.                         array($const)
  165.                     );
  166.             }
  167.             catch(ILLI_Exception_ArgumentOutOfRange $e)
  168.             {
  169.                 throw $e;
  170.             }
  171.             catch(ILLI_Exception_Argument $e)
  172.             {
  173.                 throw new ILLI_Exception_ArgumentOutOfRange
  174.                 (
  175.                     ILLI_SYSTEM_E::ARGUMENT_OUT_OF_ENUM_INDEX,
  176.                     $e,
  177.                     array($const)
  178.                 );
  179.             }
  180.                
  181.             return $this->__definedConstants->offsetGet($const);
  182.         }
  183.        
  184.         final public function reset()
  185.         {
  186.             $this->define($this->__initValue);
  187.         }
  188.        
  189.         final public function getDefinedValue()
  190.         {
  191.             return $this->__definedValue;
  192.         }
  193.        
  194.         final public function getDefinedName()
  195.         {
  196.             $res = $this->getDefinedNames();
  197.             return array_shift($res);
  198.         }
  199.        
  200.         final public function getDefinedNames()
  201.         {
  202.             return $this->getNames($this->__definedValue);
  203.         }
  204.        
  205.         final public function getDefaultValue()
  206.         {
  207.             return $this->__initValue;
  208.         }
  209.        
  210.         final public function getDefaultName()
  211.         {
  212.             $res = $this->getDefaultNames();
  213.             return array_shift($res);
  214.         }
  215.        
  216.         final public function getDefaultNames()
  217.         {
  218.             return $this->getNames($this->__initValue);
  219.         }
  220.        
  221.         public function __toString()
  222.         {
  223.             return (string) $this->getDefinedValue();
  224.         }
  225.        
  226.         public function __invoke()
  227.         {
  228.             return $this->getDefinedValue();
  229.         }
  230.        
  231.         private function isDefinedValue($value)
  232.         {
  233.             if(NULL === $value)
  234.                 throw new ILLI_Exception_ArgumentNull;
  235.            
  236.             if(!is_integer($value))
  237.                 throw new ILLI_Exception_ArgumentExpected
  238.                 (
  239.                     ILLI_System_E::ARGUMENT_EXPECTED_INTEGER
  240.                 );
  241.                
  242.             foreach($this->__definedConstants as $key => $val)
  243.                 if($value === $val)
  244.                     return TRUE;
  245.                
  246.             return FALSE;
  247.         }
  248.        
  249.         private function isDefinedConst($const)
  250.         {
  251.             if(NULL === $const)
  252.                 throw new ILLI_Exception_ArgumentNull;
  253.            
  254.             if(!is_string($const))
  255.                 throw new ILLI_Exception_ArgumentExpected
  256.                 (
  257.                     ILLI_System_E::ARGUMENT_EXPECTED_STRING
  258.                 );
  259.            
  260.             return $this->__definedConstants->offsetExists($const);
  261.         }
  262.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement