Advertisement
fruffl

Enum Example

Aug 23rd, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 17.17 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_Core
  6.      * @package    ILLI
  7.      * @link       http://illi.be
  8.      * @license    http://l.illi.be
  9.      * @copyright  ILLI Conference
  10.      */
  11.     NAMESPACE ILLI\Core;
  12.     USE ReflectionClass;
  13.  
  14.     /**
  15.      * ILLI Enumeration Parser
  16.      *
  17.      * @category   ILLI_Core
  18.      * @package    ILLI
  19.      * @subpackage Core
  20.      * @namespace  ILLI\Core
  21.      * @link       http://illi.be
  22.      * @license    http://l.illi.be
  23.      * @copyright  ILLI Conference
  24.      * @since      2.0.1
  25.      * @version    3.0.1
  26.      */
  27.     TRAIT tEnum
  28.     {
  29.         /**
  30.          * defined constants in class
  31.          *
  32.          * CONST_NAME is defined by class
  33.          *
  34.          * @var array spl class => CONST_NAME => [ \ILLI\System\Bitwise | value ]
  35.          * @static
  36.          */
  37.         private static $__tEnum_constants       = [];
  38.        
  39.         /**
  40.          * defined constants in class in variable format
  41.          *
  42.          * normalized CONST_NAME: MY_MATH_CONST -> $myMathConst
  43.          *
  44.          * @var array spl class => constName => [ \ILLI\System\Bitwise | value ]
  45.          * @static
  46.          */
  47.         private static $__tEnum_variables       = [];
  48.        
  49.         /**
  50.          * dictionary: relation between value and CONST_NAME and $constName
  51.          *
  52.          * @var array spl class => value => array('const' => CONST_NAME, 'var' => constName)
  53.          * @static
  54.          */
  55.         private static $__tEnum_dict            = [];
  56.        
  57.         /**
  58.          * callable stack
  59.          *
  60.          * CONST_NAME and $constName in lowercase, sorted by value for __callStatic
  61.          *
  62.          * @var array spl class => constname | const_name => [ \ILLI\System\Bitwise | value ]
  63.          * @static
  64.          */
  65.         private static $__tEnum_callable        = [];
  66.        
  67.         /**
  68.          * Enumerable implements interface \ILLI\System\iProviderBitwise
  69.          *
  70.          * in that case all values will be converted to \ILLI\System\Bitwise
  71.          *
  72.          * @var bool
  73.          */
  74.         private static $__tEnum_isBitwise       = [];
  75.        
  76.         /**
  77.          * the current bit
  78.          *
  79.          * @var \ILLI\System\Bitwise
  80.          */
  81.         private $__tEnum_Flag               = NULL;
  82.        
  83.         /**
  84.          * name of the invoked Constant
  85.          *
  86.          * @var string
  87.          * @see \ILLI\System\tEum\tEnum_invokeConstant
  88.          */
  89.         private $__tEnum_constantRequest        = '';
  90.        
  91.         /**
  92.          * name of the invoked variable
  93.          *
  94.          * @var string
  95.          * @see \ILLI\System\tEum\tEnum_invokeConstant
  96.          */
  97.         private $__tEnum_variableRequest        = '';
  98.        
  99.         /**
  100.          * name of the invoke
  101.          *
  102.          * @var string
  103.          * @see \ILLI\System\tEum\tEnum_invokeConstant
  104.          */
  105.         private $__tEnum_methodRequest          = '';
  106.        
  107.         /**
  108.          * the current bit flag as variable stock (reversed bitflag)
  109.          *
  110.          * @var array array(2 => 'myConst1', 4 => 'myConst2'); (6)
  111.          */
  112.         private $__tEnum_variablesHook          = [];
  113.        
  114.         /**
  115.          * the current bit flag as variable stock (reversed bitflag)
  116.          *
  117.          * @var array array(2 => 'MY_CONST_1', 4 => 'MY_CONST_2'); (6)
  118.          */
  119.         private $__tEnum_constantsHook          = [];
  120.        
  121.         /**
  122.          * the current bit flag as decimal stock (reversed bitflag)
  123.          *
  124.          * @var array array(2, 4); (6)
  125.          */
  126.         private $__tEnum_integerHook            = [];
  127.        
  128.         /**
  129.          * the current bit flag as bitmask
  130.          *
  131.          * @var array array(1, 1, 0); (6)
  132.          */
  133.         private $__tEnum_bitwiseHook            = [];
  134.        
  135.         private function tEnum___register(Enum $ENUM)
  136.         {
  137.             $reflector = new ReflectionClass(get_called_class());
  138.            
  139.             self::$__tEnum_constants        [get_called_class()] = $reflector->getConstants();
  140.             self::$__tEnum_variables        [get_called_class()] = [];
  141.             self::$__tEnum_dict         [get_called_class()] = [];
  142.             self::$__tEnum_isBitwise        [get_called_class()] = ($this instanceOf iProviderBitwise);
  143.            
  144.             if(FALSE === $reflector->isFinal())
  145.                 throw new \Exception('Enumeration '.get_called_class().' is not final.');
  146.                
  147.             if(FALSE === (bool) self::$__tEnum_constants)
  148.                 throw new \Exception('Enumeration '.get_called_class().' has no constants.');
  149.            
  150.             foreach(self::$__tEnum_constants[get_called_class()] as $const => $value)
  151.             {
  152.                 // make constants as variable/function accessible; format: MY_CONST => $myConst
  153.                 $var = lcfirst(implode('', array_map('ucfirst', array_map('strtolower', explode('_', $const)))));
  154.                 self::$__tEnum_dict[get_called_class()][$value] = ['const' => $const, 'var' => $var];
  155.                
  156.                
  157.                 if(TRUE === self::$__tEnum_isBitwise[get_called_class()])
  158.                 {
  159.                     if(FALSE === is_integer($value))
  160.                         throw new \Exception('Value for Bitwise Flag must be of type integer. Type for '.$value.' is '.getType($value).'.');
  161.                        
  162.                     $value = new Bitwise($value);
  163.                     self::$__tEnum_constants[get_called_class()][$const] = $value;
  164.                 }
  165.                
  166.                 self::$__tEnum_variables[get_called_class()][$var] = $value;
  167.             }
  168.            
  169.             foreach(self::$__tEnum_constants[get_called_class()] as $avail => $value)
  170.                 self::$__tEnum_callable[get_called_class()][strtolower($avail)] = $value;
  171.                
  172.             foreach(self::$__tEnum_variables[get_called_class()] as $avail => $value)
  173.                 self::$__tEnum_callable[get_called_class()][strtolower($avail)] = $value;
  174.                
  175.             return $this;
  176.         }
  177.        
  178.         private function tEnum___unregister()
  179.         {
  180.             unset(self::$__tEnum_constants  [get_called_class()]);
  181.             unset(self::$__tEnum_variables  [get_called_class()]);
  182.             unset(self::$__tEnum_dict   [get_called_class()]);
  183.             unset(self::$__tEnum_callable   [get_called_class()]);
  184.             unset(self::$__tEnum_isBitwise  [get_called_class()]);
  185.             return $this;
  186.         }
  187.        
  188.         public function tEnum_getDefinedConstants()
  189.         {
  190.             return array_keys(self::$__tEnum_constants[get_called_class()]);
  191.         }
  192.        
  193.         public function tEnum_getDefinedVariables()
  194.         {
  195.             return array_keys(self::$__tEnum_variables[get_called_class()]);
  196.         }
  197.        
  198.         public function tEnum_getEnumeratedConstants()
  199.         {
  200.             return $this->__tEnum_constantsHook;
  201.         }
  202.        
  203.         public function tEnum_getEnumeratedVariables()
  204.         {
  205.             return $this->__tEnum_variablesHook;
  206.         }
  207.        
  208.         public function tEnum_getEnumeratedIntegers()
  209.         {
  210.             return $this->__tEnum_integerHook;
  211.         }
  212.        
  213.         public function tEnum_getEnumeratedBitwise()
  214.         {
  215.             return $this->__tEnum_integerHook;
  216.         }
  217.        
  218.         /**
  219.          * invoke a constant as function
  220.          *
  221.          * <code>
  222.          * <?PHP
  223.          *  $foo = MyEnumDef::myConst1();
  224.          * ?>
  225.          * </code>
  226.          *
  227.          */
  228.         public function tEnum_invokeConstant($index)
  229.         {
  230.             $this->__tEnum_methodRequest        = $index;
  231.             $index = strtolower($index);
  232.            
  233.             if(FALSE === array_key_exists($index, self::$__tEnum_callable[get_called_class()]))
  234.                 throw new \Exception('Enumeration '.$index.' is not valid.');
  235.            
  236.             $this->__tEnum_Flag     = self::$__tEnum_callable[get_called_class()][$index];
  237.             $this->__tEnum_integerHook  = self::$__tEnum_callable[get_called_class()][$index]->toStock();
  238.             $this->__tEnum_bitwiseHook  = self::$__tEnum_callable[get_called_class()][$index]->toArray();
  239.             $this->__tEnum_variablesHook    = [];
  240.             $this->__tEnum_constantsHook    = [];
  241.            
  242.             $summary = 0;
  243.             foreach($this->__tEnum_integerHook as $dec)
  244.             {
  245.                 $this->__tEnum_variablesHook[$dec] = self::$__tEnum_dict[get_called_class()][$dec]['var'];
  246.                 $this->__tEnum_constantsHook[$dec] = self::$__tEnum_dict[get_called_class()][$dec]['const'];
  247.                
  248.                 $summary += $dec;
  249.             }
  250.            
  251.            
  252.             $this->__tEnum_variableRequest      = self::$__tEnum_dict[get_called_class()][$summary]['var'];
  253.             $this->__tEnum_constantRequest      = self::$__tEnum_dict[get_called_class()][$summary]['const'];
  254.             /*
  255.             var_dump([
  256.                 '__tEnum_Flag'      => $this->__tEnum_Flag,
  257.                 '__tEnum_integerHook'   => $this->__tEnum_integerHook,
  258.                 '__tEnum_bitwiseHook'   => $this->__tEnum_bitwiseHook,
  259.                 '__tEnum_variablesHook' => $this->__tEnum_variablesHook,
  260.                 '__tEnum_constantsHook' => $this->__tEnum_constantsHook,
  261.                 '__tEnum_constants' => self::$__tEnum_constants,
  262.                 '__tEnum_variables' => self::$__tEnum_variables,
  263.                 '__tEnum_callable'  => self::$__tEnum_callable,
  264.                 '__tEnum_dict'      => self::$__tEnum_dict,
  265.             ]);
  266.             */
  267.            
  268.             return $this;
  269.         }
  270.     }
  271.  
  272.  
  273.     /**
  274.      * ILLI
  275.      *
  276.      * @category   ILLI_Core
  277.      * @package    ILLI
  278.      * @link       http://illi.be
  279.      * @license    http://l.illi.be
  280.      * @copyright  ILLI Conference
  281.      */
  282.     NAMESPACE ILLI\Core;
  283.  
  284.     /**
  285.      * ILLI Enum
  286.      *
  287.      * <code>
  288.      * <?PHP
  289.      *  FINAL CLASS eMyEnumeration EXTENDS Enum
  290.      *  {
  291.      *      const MONDAY = 1;
  292.      *  }
  293.      * ?>
  294.      * </code>
  295.      *
  296.      * @category   ILLI_Core
  297.      * @package    ILLI
  298.      * @subpackage Core
  299.      * @namespace  ILLI\Core
  300.      * @link       http://illi.be
  301.      * @license    http://l.illi.be
  302.      * @copyright  ILLI Conference
  303.      * @since      2.0.1
  304.      * @version    3.0.1
  305.      */
  306.     ABSTRACT CLASS Enum EXTENDS Core IMPLEMENTS iStaticStorage
  307.     {
  308.         USE tStaticSplHashRegister
  309.         {
  310.             tSplHashRegister_register       as private  __splRegister;
  311.             tSplHashRegister_unregister     as private  __splUnregister;
  312.             tSplHashRegister_getCode        as private  splGetHashCode;
  313.             tSplHashRegister_getInstances       as private  splGetInstances;
  314.             tSplHashRegister_getSiblings        as private  splGetSiblingInstances;
  315.         }
  316.        
  317.         USE tInstance
  318.         {
  319.             tInstance_unregister            as private  __instanceUnregister;
  320.             tInstance_get               as private  __instanceGet;
  321.             tInstance_register          as private  instanceRegister;
  322.             tInstance_getName           as private  instanceGetName;
  323.             tInstance_exists            as private  instanceExists;
  324.             tInstance_getNames          as private  instanceGetNames;
  325.         }
  326.        
  327.         USE tStaticStorage
  328.         {
  329.             tStorage___register         as private  __storageRegister;
  330.             tStorage___unregister           as private  __storageUnregister;
  331.             tStorage_get                as private  storageGet;
  332.             tStorage_getIterator            as public   getIterator;
  333.             tStorage_push               as private  storagePush;
  334.             tStorage_pop                as private  storagePop;
  335.             tStorage_peek               as private  storagePeek;
  336.         }
  337.        
  338.         USE tEnum
  339.         {
  340.             tEnum___register            as private  __enumRegister;
  341.             tEnum___unregister          as private  __enumUnregister;
  342.             tEnum_invokeConstant            as private  __enumerate;
  343.             tEnum_getDefinedConstants       as public   enumGetDefinedConstants;
  344.             tEnum_getDefinedVariables       as public   enumGetDefinedVariables;
  345.             tEnum_getEnumeratedConstants        as public   enumGetConstantsArray;
  346.             tEnum_getEnumeratedVariables        as public   enumGetVariablesArray;
  347.             tEnum_getEnumeratedIntegers     as public   enumGetIntegersArray;
  348.             tEnum_getEnumeratedBitwise      as public   enumGetBitwiseArray;
  349.         }
  350.        
  351.         public function __destruct()
  352.         {
  353.             $this->__splUnregister();
  354.             $this->__instanceUnregister($this);
  355.             $this->__storageUnregister();
  356.             $this->__enumUnregister($this);
  357.         }
  358.        
  359.         public static function instanceGet()
  360.         {
  361.             if(self::splGetHashCode() === NULL)
  362.                 self::__staticConstruct();
  363.            
  364.             return self::__instanceGet();
  365.         }
  366.        
  367.         public static function __callStatic($index, $args)
  368.         {
  369.             if(self::splGetHashCode() === NULL)
  370.                 self::__staticConstruct();
  371.                
  372.             self::__instanceGet()->__enumerate($index);
  373.            
  374.             return self::__instanceGet();
  375.         }
  376.        
  377.         private function __construct()
  378.         {
  379.         }
  380.        
  381.         private function __clone()
  382.         {
  383.         }
  384.        
  385.         private static function __staticConstruct()
  386.         {
  387.             self::__instanceGet()->__splRegister();
  388.             self::__instanceGet()->__enumRegister(self::__instanceGet());
  389.             //self::__instanceGet()->__storageRegister(new FacadeArrayList(new Collection));
  390.         }
  391.            
  392.         public function __get($constant)
  393.         {
  394.         }
  395.     }
  396.  
  397.     /**
  398.      * ILLI
  399.      *
  400.      * @category   ILLI_System
  401.      * @package    ILLI
  402.      * @link       http://illi.be
  403.      * @license    http://l.illi.be
  404.      * @copyright  ILLI Conference
  405.      */
  406.     NAMESPACE ILLI\System;
  407.  
  408.     /**
  409.      * ILLI Uri Component Enumeration
  410.      *
  411.      * @category   ILLI_System
  412.      * @package    ILLI
  413.      * @subpackage System
  414.      * @namespace  ILLI\System
  415.      * @link       http://illi.be
  416.      * @license    http://l.illi.be
  417.      * @copyright  ILLI Conference
  418.      * @since      2.0.1
  419.      * @version    3.0.1
  420.      */
  421.     FINAL CLASS eUriComponents EXTENDS \ILLI\Core\Enum IMPLEMENTS \ILLI\Core\iProviderBitwise
  422.     {
  423.         /**
  424.          * @const int ILLI\System\Uri::$scheme
  425.          */
  426.         const SCHEME        = 1;
  427.        
  428.         /**
  429.          * @const int ILLI\System\Uri::$userInfo
  430.          */
  431.         const USER_INFO     = 2;
  432.        
  433.         /**
  434.          * @const int ILLI\System\Uri::$host
  435.          */
  436.         const HOST      = 4;
  437.        
  438.         /**
  439.          * @const int ILLI\System\Uri::$port
  440.          */
  441.         const PORT      = 8;
  442.        
  443.         /**
  444.          * @const int ILLI\System\Uri::$localPath
  445.          */
  446.         const LOCAL_PATH    = 16;
  447.        
  448.         /**
  449.          * @const int ILLI\System\Uri::$query
  450.          */
  451.         const QUERY     = 32;
  452.        
  453.         /**
  454.          * @const int ILLI\System\Uri::$fragment
  455.          */
  456.         const FRAGMENT      = 64;
  457.        
  458.         /**
  459.          * @const int ILLI\System\Uri::$port || default port || -1
  460.          */
  461.         const STRONG_PORT   = 128;
  462.        
  463.         /**
  464.          * @const int normalized ILLI\System\Uri::$host
  465.          */
  466.         const NORMALIZED_HOST   = 256;
  467.        
  468.         /**
  469.          * @const int ILLI\System\eUriComponent::SCHEME
  470.          *      | ILLI\System\eUriComponent::USER_INFO
  471.          *      | ILLI\System\eUriComponent::HOST
  472.          *      | ILLI\System\eUriComponent::PORT
  473.          *      | ILLI\System\eUriComponent::LOCAL_PATH
  474.          *      | ILLI\System\eUriComponent::QUERY
  475.          */
  476.         const ABSOLUTE_URL  = 63;
  477.        
  478.         /**
  479.          * @const int ILLI\System\eUriComponent::HOST
  480.          *      | ILLI\System\eUriComponent::STRONG_PORT
  481.          */
  482.         const HOST_AND_PORT = 132;
  483.        
  484.         /**
  485.          * @const int ILLI\System\eUriComponent::USER_INFO
  486.          *      | ILLI\System\eUriComponent::HOST
  487.          *      | ILLI\System\eUriComponent::STRONG_PORT
  488.          */
  489.         const STRONG_AUTHORITY  = 134;
  490.        
  491.         /**
  492.          * @const int ILLI\System\eUriComponent::SCHEME
  493.          *      | ILLI\System\eUriComponent::HOST
  494.          *      | ILLI\System\eUriComponent::STRONG_PORT
  495.          */
  496.         const SCHEME_AND_SERVER = 133;
  497.        
  498.         /**
  499.          * @const int ILLI\System\eUriComponent::SCHEME
  500.          *      | ILLI\System\eUriComponent::HOST
  501.          *      | ILLI\System\eUriComponent::PORT
  502.          *      | ILLI\System\eUriComponent::LOCAL_PATH
  503.          *      | ILLI\System\eUriComponent::QUERY
  504.          */
  505.         const HTTP_REQUEST_URL  = 61;
  506.        
  507.         /**
  508.          * @const int ILLI\System\eUriComponent::LOCAL_PATH
  509.          *      | ILLI\System\eUriComponent::QUERY
  510.          */
  511.         const PATH_AND_QUERY    = 48;
  512.     }
  513.  
  514.     /**
  515.      * ILLI
  516.      *
  517.      * @category   ILLI_System
  518.      * @package    ILLI
  519.      * @link       http://illi.be
  520.      * @license    http://l.illi.be
  521.      * @copyright  ILLI Conference
  522.      */
  523.     NAMESPACE ILLI\System;
  524.  
  525.     /**
  526.      * ILLI Uri
  527.      *
  528.      * @category   ILLI_System
  529.      * @package    ILLI
  530.      * @subpackage System
  531.      * @namespace  ILLI\System
  532.      * @link       http://illi.be
  533.      * @license    http://l.illi.be
  534.      * @copyright  ILLI Conference
  535.      * @since      2.0.1
  536.      * @version    3.0.1
  537.      */
  538.     CLASS Uri EXTENDS \ILLI\Core\Table IMPLEMENTS \ILLI\Core\iProviderArrayList
  539.     {
  540.         private $__UriComponents = NULL;
  541.        
  542.         const SCHEME_DELIMETER  = '://';
  543.        
  544.         /**
  545.          * Addressing files on local or network file systems
  546.          *
  547.          * Since this usually used for local files the host from RFC 1738 is often empty leading
  548.          * to a starting triple /. RFC 3986 allows an absolute path with no host part.
  549.          *
  550.          * @const string rfc1738; rfc3986
  551.          * @link http://tools.ietf.org/html/rfc1738
  552.          * @link http://tools.ietf.org/html/rfc3986
  553.          */
  554.         const SCHEME_FILE   = 'file';
  555.        
  556.         /**
  557.          * File Transfer Protocol
  558.          *
  559.          * @const string rfc1738
  560.          * @link http://tools.ietf.org/html/rfc1738
  561.          */
  562.         const SCHEME_FTP    = 'ftp';
  563.        
  564.         /**
  565.          * Gopher Protocol
  566.          *
  567.          * @const string rfc4266
  568.          * @link http://tools.ietf.org/html/rfc4266
  569.          */
  570.         const SCHEME_GOPHER = 'gopher';
  571.        
  572.         /**
  573.          * Hyper Text Transfer Protocol
  574.          *
  575.          * @const string rfc1738
  576.          * @link http://tools.ietf.org/html/rfc1738
  577.          */
  578.         const SCHEME_HTTP   = 'http';
  579.        
  580.         /**
  581.          * Hyper Text Transfer Protocol SSL/TLS
  582.          *
  583.          * @const string rfc2817
  584.          * @link http://tools.ietf.org/html/rfc2817
  585.          */
  586.         const SCHEME_HTTPS  = 'https';
  587.        
  588.         /**
  589.          * SMTP e-mail addresses and default content
  590.          *
  591.          * Headers are optional, but often include subject=; body=
  592.          * can be used to pre-fill the body of the message.
  593.          *
  594.          * @const string rfc6068
  595.          * @link http://tools.ietf.org/html/rfc6068
  596.          */
  597.         const SCHEME_MAILTO = 'mailto';
  598.        
  599.         public function __construct($uri = NULL)
  600.         {
  601.             $this->__UriComponents = eUriComponents::instanceGet();
  602.            
  603.             $default = [
  604.                 'absolutePath'      => '',
  605.                 'absoluteUri'       => '',
  606.                 'authority'     => '',
  607.                 'dnsSafeHost'       => '',
  608.                 'fragment'      => '',
  609.                 'host'          => '',
  610.                 'hostNameType'      => '',
  611.                 'isAbsolute'        => FALSE,
  612.                 'isDefaultPort'     => FALSE,
  613.                 'isFile'        => FALSE,
  614.                 'isLoopBack'        => FALSE,
  615.                 'isUnc'         => FALSE,
  616.                 'isUserEscaped'     => FALSE,
  617.                 'localPath'     => '',
  618.                 'originalString'    => '',
  619.                 'pathAndQuery'      => '',
  620.                 'port'          => '',
  621.                 'query'         => '',
  622.                 'scheme'        => '',
  623.                 'segments'      => [],
  624.                 'userInfo'      => new UserInfo
  625.             ];
  626.            
  627.             parent::__construct($default);
  628.            
  629.             $self = $this;
  630.            
  631.             $this
  632.             ->filterRegisterEventClosure(\ILLI\Core\eFilter::ARGUMENTS, 'ILLI\Core\Table::__set', function($data) use ($self)
  633.             {
  634.                 $index = $data['index'];
  635.                 $value = $data['value'];
  636.                 switch($index):
  637.                     case 'scheme':
  638.                         $value = strtolower($value);
  639.                         break;
  640.                     case 'host':
  641.                         if(strpos($value, '/') !== false)
  642.                         {
  643.                             list($host, $self->absolutePath) = explode('/', $value, 2);
  644.                             $value = $host;
  645.                         }
  646.                        
  647.                         $value = strtolower($value);
  648.                         break;
  649.                     case 'absolutePath':
  650.                         $value = ($value[0] === '/') ? $value : '/'.$value;
  651.                         $value = str_replace('\\', '/', $value);
  652.                         $value = str_replace('//', '/', $value);
  653.                         break;
  654.                     case 'query':
  655.                         $value = (array) $value;
  656.                         break;
  657.                 endswitch;
  658.                
  659.                 return ['index' => $index, 'value' => $value];
  660.             });
  661.         }
  662.        
  663.         public function __call($name, array $args = array())
  664.         {
  665.             $c = $this->__UriComponents;
  666.             $c::$name();
  667.             return $this;
  668.         }
  669.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement