Advertisement
fruffl

ltd./TypeSafe Table

Aug 22nd, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_System
  6.      * @package    ILLI
  7.      * @link       http://illi.be
  8.      * @license    http://l.illi.be
  9.      * @copyright  ILLI Conference
  10.      */
  11.     NAMESPACE ILLI\System;
  12.     USE IteratorAggregate;
  13.  
  14.     /**
  15.      * ILLI Table
  16.      *
  17.      * array in Storage is limited by init-array; type-safe values (SCALAR IS NOT A TYPE!)
  18.      *
  19.      * @category   ILLI_System
  20.      * @package    ILLI
  21.      * @subpackage System
  22.      * @namespace  ILLI\System
  23.      * @link       http://illi.be
  24.      * @license    http://l.illi.be
  25.      * @copyright  ILLI Conference
  26.      * @since      2.0.1
  27.      * @version    3.0.1
  28.      */
  29.     CLASS Table EXTENDS Object IMPLEMENTS IteratorAggregate
  30.     {
  31.         USE \ILLI\Core\tStorage
  32.         {
  33.             tStorage___register         as private  __storageRegister;
  34.             tStorage___unregister           as private  __storageUnregister;
  35.             tStorage_get                as protected    storageGet;
  36.             tStorage_getIterator            as public   getIterator;
  37.             tStorage_push               as private  storagePush;
  38.             tStorage_pop                as private  storagePop;
  39.             tStorage_peek               as public   storagePeek;
  40.         }
  41.          
  42.         public function __construct(array $data = array())
  43.         {
  44.             parent::__construct();
  45.            
  46.             if(NULL === $this->storageGet())
  47.                 switch(TRUE):
  48.                     case($this instanceOf \ILLI\Core\iStack):
  49.                         $this->__storageRegister(new \ILLI\Core\Stack(new \ILLI\Core\Collection($data)));
  50.                         break;
  51.                     case($this instanceOf \ILLI\Core\iQueue):
  52.                         $this->__storageRegister(new \ILLI\Core\Queue(new \ILLI\Core\Collection($data)));
  53.                         break;
  54.                     case($this instanceOf \ILLI\Core\iArrayList):
  55.                     default:
  56.                         $this->__storageRegister(new \ILLI\Core\ArrayList(new \ILLI\Core\Collection($data)));
  57.                         break;
  58.                 endswitch;
  59.         }
  60.        
  61.         public function __set($index, $value)
  62.         {
  63.             if(FALSE === $this->storageGet()->getCollection()->offsetExists($index))
  64.                 return;
  65.            
  66.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  67.                 return $this->adapterCallAlias(__METHOD__, $index, $value);
  68.            
  69.             $filtered   = $this->filterApply(\ILLI\Core\iFilter::ARGUMENTS, __METHOD__, ['index' => $index, 'value' => $value]);
  70.             $index      = $filtered['index'];
  71.             $value      = $filtered['value'];
  72.            
  73.             $old        = $this->storageGet()->getCollection()->offsetGet($index);
  74.             $orig       =  is_object($old) ? get_class($old) : getType($old);
  75.             $new        =  is_object($value) ? get_class($value) : getType($value);
  76.            
  77.             if($orig !== $new)
  78.                 throw new \Exception('Type mismatch for '.$index.'. original:'.$orig.'; new: '.$new);
  79.            
  80.            
  81.             $this->storageGet()->getCollection()->offsetSet($index, $value);
  82.            
  83.             $this->observerNotify(__METHOD__, $index, $value);
  84.         }
  85.        
  86.         public function __get($index)
  87.         {
  88.             if(FALSE === $this->storageGet()->getCollection()->offsetExists($index))
  89.                 return NULL;
  90.                
  91.             if(TRUE === $this->adapterTriggerExists(__METHOD__))
  92.                 return $this->adapterCallAlias(__METHOD__, $index);
  93.                
  94.             $filtered   = $this->filterApply(\ILLI\Core\iFilter::ARGUMENTS, __METHOD__, ['index' => $index]);
  95.             $index      = $filtered['index'];
  96.             $value      = $this->storageGet()->getCollection()->offsetGet($index);
  97.            
  98.             $this->observerNotify(__METHOD__, $index, $value);
  99.            
  100.             $value = $this->filterApply(\ILLI\Core\iFilter::RESULT, __METHOD__, $value);
  101.            
  102.             return $value;
  103.         }
  104.        
  105.         public function toArray()
  106.         {
  107.             return $this->storageGet()->getCollection()->toArray();
  108.         }
  109.        
  110.         public function __destruct()
  111.         {
  112.             $this->__storageUnregister();
  113.             parent::__destruct();
  114.         }
  115.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement