Advertisement
fruffl

PHP Datatype Class

Sep 18th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.30 KB | None | 0 0
  1. <?PHP
  2.     NAMESPACE ILLI\Core\Type;
  3.     USE ILLI\Core\Spl\Fsm;
  4.     USE ILLI\Core\Std\Clsid;
  5.     USE ILLI\Core\Type\__flag_T;
  6.     USE ILLI\Core\Type\T\GC;
  7.     USE ILLI\Core\Type\T\Data;
  8.     USE ILLI\Core\Type\ILimitable;
  9.     USE ILLI\Core\Type\IListable;
  10.     USE ILLI\Core\Std\Exception;
  11.     USE ILLI\Core\Util\UUID;
  12.    
  13.     ABSTRACT CLASS T
  14.     {
  15.         private static $__gc    = [];
  16.        
  17.         protected $__super  = NULL;
  18.         protected $__context    = NULL;
  19.         protected $__clsid  = NULL;
  20.         protected $__prop   = 0b00;
  21.         protected $__Definition = NULL;
  22.        
  23.         private $__Data = NULL;
  24.        
  25.         function __construct(array $__type, $__context = UUID::NIL, $__super = UUID::NIL)
  26.         {
  27.             $this->__context    = UUID::normalize($__context);
  28.             $this->__super      = UUID::normalize($__super);
  29.            
  30.             $G          = &self::$__gc[$this->__super][get_called_class()][$this->__context];
  31.             $this->__Definition = &$G[0x00];
  32.             $this->__clsid      = &$G[0x01];
  33.            
  34.             #+ create data matrix:
  35.             $this->__Data instanceOf Data ?: $this->__Data = new Data;
  36.            
  37.             #+ load or create type declaration for this class
  38.             if(NULL === ($T = &$G[0x00]))
  39.                 $T = new GC((array) $__type);
  40.            
  41.             #+ set property flag by type declaration
  42.             switch(TRUE):
  43.                 case 0x00 === $T->getSize():        $this->__prop |= __flag_T::T_VOID;          break;
  44.                 case 0x01 === $T->getSize():        $this->__prop |= __flag_T::T_SHALLOW;           break;
  45.                 case 0x02 === $T->getSize():        $this->__prop |= __flag_T::T_PAIR;          break;
  46.                 case 0x02  <  $T->getSize():        $this->__prop |= __flag_T::T_ROW;           break;
  47.             endswitch;
  48.            
  49.             foreach(class_implements($this) as $interface)
  50.                 switch($interface):
  51.                     case IListable::class:      $this->__prop |= __flag_T::T_LISTABLE;          break;
  52.                     case ILimitable::class:     $this->__prop |= __flag_T::T_LIMITABLE;         break;
  53.                 endswitch;
  54.            
  55.             #+ set matrix dimension
  56.             switch(TRUE):
  57.                 case $this instanceOf IListable:    $this->__Data->setSize($T->getSize());          break;
  58.                 case 0x00 === $T->getSize():        $this->__Data->setDimension(0x00, 0x00);        break;
  59.                 case 0x00  <  $T->getSize():        $this->__Data->setDimension($T->getSize(), 0x01);   break;
  60.             endswitch;
  61.                
  62.            
  63.             #+ clsid
  64.             NULL !== ($C = &$G[0x01]) ?:
  65.                 $C = (new Clsid(__CLASS__, get_called_class(), [
  66.                     'definition'    => $T->invoke('invoke', ['clsid']), // Fsc->Fsc->Define::clsid()
  67.                     'prop'      => $this->__prop
  68.                 ], $this->__context, $this->__super))->uuid();
  69.         }
  70.        
  71.         /**
  72.          * add or remove lines in data FSM
  73.          *
  74.          * @param int   $__length   Lines : y range
  75.          * @throws Exception when interface T_LISTABLE is not implemented
  76.          * @throws Exception when interface T_LIMITABLE is implemented
  77.          */
  78.         function setLength($__length)
  79.         {
  80.             if(__flag_T::T_LISTABLE !== ($this->__prop & __flag_T::T_LISTABLE)
  81.             || __flag_T::T_LIMITABLE === ($this->__prop & __flag_T::T_LIMITABLE))
  82.                 throw new Exception('Unable to add or remove lines (y offset): {:class} does not implement interface {:IListable} or implements {:ILimitable}.',
  83.                     ['class' => get_called_class(), 'ILimitable' => ILimitable::class, 'IListable' => IListable::class]);
  84.            
  85.             $this->__Data->setLength($__length);
  86.            
  87.             return $this;
  88.         }
  89.        
  90.         function inRange($__field = 0x00, $__line = 0x00)
  91.         {
  92.             return $__field >= 0x00
  93.                 && $__line  >= 0x00
  94.                 && $__field <= $this->__Data->getSize() - 1
  95.                 && $__line  <= $this->__Data->getLength() - 1;
  96.         }
  97.        
  98.         /**
  99.          * store value at specific offset in data FSM
  100.          *
  101.          * n = 0 ... defined fields - 1
  102.          * i = 0 ... infinite
  103.          * f = 0 ... finite
  104.          *
  105.          *      T           T<IListable>            T<IListable:ILimited>
  106.          *
  107.          *     & T_PAIR [0,0] [1,0]     [0,i] [1,i]         [0,f] [1,f]
  108.          *      & T_ROW [n,0]           [n,i]               [n,f]
  109.          *  & T_SHALLOW [0,0]           [0,i]               [0,f]
  110.          *
  111.          * @param mixed     $__value    expected type is defined in Field Declaration
  112.          * @param int       $__field    Field : offset x, default 0
  113.          * @param int       $__line     Line  : offset y, default 0
  114.          *
  115.          * @internal this method should be protected
  116.          * @testing default address for $__prop
  117.          * @todo
  118.          */
  119.         function set($__value, $__field = 0x00, $__line = 0x00)
  120.         {
  121.             if(FALSE === $this->inRange($__field, $__line))
  122.                 throw new Exception('Unable to set data: address {:class}<x:{:field},y:{:line}> not found.',
  123.                     ['field' => $__field, 'line' => $__line, 'class' => get_called_class()]);
  124.            
  125.             $this->__Data->offsetGet($__field)->offsetSet($__line, $__value);
  126.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement