Advertisement
fruffl

Class ProtoArray

Mar 5th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.21 KB | None | 0 0
  1. <?PHP
  2.     /**
  3.      * ILLI
  4.      *
  5.      * @category   ILLI_System
  6.      * @package    ILLI
  7.      * @subpackage System
  8.      * @link       http://illi.be
  9.      * @license    http://l.illi.be
  10.      * @copyright  ILLI Conference
  11.      */
  12.     NAMESPACE ILLI\System;
  13.  
  14.     /**
  15.      * ILLI System Abstract Prototype Array
  16.      *
  17.      * Basic Array: index : offset : value
  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.0-1
  27.      * @version    2.0.0-1
  28.      * @abstract
  29.      */
  30.     CLASS ProtoArray EXTENDS Proto IMPLEMENTS iCountable, iArrayAccess, iSerializable, iIterator
  31.     {
  32.         /**
  33.          * iterator modes
  34.          * [fifo: 0] [lifo: 0x00000002] [filo: 0x00000004] [lilo: 0x00000006] [default: 0]
  35.          * @var int
  36.          */
  37.         const IT_MODE_FI        = 0x00000000;
  38.        
  39.         /**
  40.          * @see IT_MODE_FI
  41.          * @var int
  42.          */
  43.         const IT_MODE_FO        = 0x00000000;
  44.        
  45.         /**
  46.          * @see IT_MODE_FI
  47.          * @var int
  48.          */
  49.         const IT_MODE_LI        = 0x00000002;
  50.        
  51.         /**
  52.          * @see IT_MODE_FI
  53.          * @var int
  54.          */
  55.         const IT_MODE_LO        = 0x00000004;
  56.        
  57.         /**
  58.          * typedef values: if set, strict type for values
  59.          * [mixed: 0] [safe: 0x00000032] [default: 0]
  60.          * @var int
  61.          */
  62.         const VALUE_TYPE_UNSAFE     = 0x00000000;
  63.        
  64.         /**
  65.          * @see VALUE_TYPE_UNSAFE
  66.          * @var int
  67.          */
  68.         const VALUE_TYPE_SAFE       = 0x00000032;
  69.        
  70.         /**
  71.          * typedef: zerobased or assoc array
  72.          * [array: 0] [assoc: 0x00000256] [default: 0]
  73.          * @var int
  74.          */
  75.         const ARRAY_MODE_ARRAY      = 0x00000000;
  76.        
  77.         /**
  78.          * @see ARRAY_MODE_ASSOC
  79.          * @var int
  80.          */
  81.         const ARRAY_MODE_ASSOC      = 0x00000256;
  82.        
  83.         /**
  84.          * array_merge: order of priority
  85.          * [new before existing: 0] [existing before new: 0x00001024] [default: 0]
  86.          * @var int
  87.          */
  88.         const PERM_VALUE_AS_SUPER   = 0x00000000;
  89.        
  90.         /**
  91.          * @see PERM_VALUE_AS_SUPER
  92.          * @var int
  93.          */
  94.         const PERM_ARRAY_AS_SUPER   = 0x00001024;
  95.        
  96.         /**
  97.          * serializable private properties
  98.          * @var array
  99.          * @static
  100.          */
  101.         private static $__serializable  =
  102.                         [
  103.                             '__array',
  104.                             '__offsets',
  105.                             '__indexes',
  106.                             '__bitFlag'
  107.                         ];
  108.        
  109.         /**
  110.          * bitflag
  111.          * @var int
  112.          * @see IT_MODE_FI
  113.          * @see IT_MODE_FO
  114.          * @see VALUE_TYPE_UNSAFE
  115.          * @see ARRAY_MODE_ARRAY
  116.          * @see PERM_VALUE_AS_SUPER
  117.          */
  118.         private $__bitFlag      = 0x00000000;
  119.        
  120.         /**
  121.          * data array
  122.          * @var array
  123.          */
  124.         private $__array        = [];
  125.        
  126.         /**
  127.          * offset cache: array keys from $__array
  128.          * @var array
  129.          */
  130.         private $__offsets      = [];
  131.        
  132.         /**
  133.          * index cache: flipped $__offsets
  134.          * @var array
  135.          */
  136.         private $__indexes      = [];
  137.        
  138.         /**
  139.          * iterator index
  140.          * @var int32
  141.          */
  142.         private $__itIndex      = 0;
  143.        
  144.         /**
  145.          * @param array|ProtoArray $array initial value
  146.          */
  147.         public function __construct($array = [])
  148.         {
  149.             self::$__tSerializableProperties = self::$__serializable;
  150.            
  151.             if($array instanceOf ProtoArray)
  152.                 $array = $array->toArray();
  153.            
  154.             $this->__array = $array;
  155.             $this->updateEvent();
  156.         }
  157.        
  158.         /**
  159.          * synchronize $__array with $__indexes and $__offsets
  160.          *
  161.          * its essential to update the both arrays whenever you edit $__array
  162.          *
  163.          * @see $__array
  164.          * @see $__indexes
  165.          * @see $__offsets
  166.          */
  167.         protected function updateEvent()
  168.         {
  169.             $this->__offsets = array_keys($this->__array);
  170.             $this->__indexes = array_flip($this->__offsets);
  171.             return $this;
  172.         }
  173.        
  174.         // iSerializable
  175.        
  176.         /**
  177.          * @see tSerializable
  178.          */
  179.         USE tSerializable;
  180.        
  181.         // Countable
  182.        
  183.         public function count()
  184.         {
  185.             return sizeOf($this->__array);
  186.         }
  187.        
  188.         // Iterator
  189.        
  190.         public function rewind()
  191.         {
  192.             $this->__itIndex = $this->firstIndex();
  193.             return $this->__itIndex;
  194.         }
  195.        
  196.         public function current()
  197.         {
  198.             return $this->indexGet($this->__itIndex);
  199.         }
  200.        
  201.         public function key()
  202.         {
  203.             return $this->indexToOffset($this->__itIndex);
  204.         }
  205.        
  206.         public function next()
  207.         {
  208.             return ++$this->__itIndex;
  209.         }
  210.        
  211.         public function prev()
  212.         {
  213.             return --$this->__itIndex;
  214.         }
  215.        
  216.         public function valid()
  217.         {
  218.             return $this->indexExists($this->__itIndex);
  219.         }
  220.        
  221.         // ArrayAccess
  222.        
  223.         public function offsetSet($offset, $value)
  224.         {
  225.             $this->__array[$offset] = $value;
  226.             return $this->updateEvent();
  227.         }
  228.        
  229.         public function offsetExists($offset)
  230.         {
  231.             return array_key_exists($offset, $this->__array);
  232.         }
  233.        
  234.         public function offsetUnset($offset)
  235.         {
  236.             if(FALSE === $this->offsetExists($offset))
  237.                 return $this;
  238.            
  239.             unset($this->__array[$offset]);
  240.             return $this->updateEvent();
  241.         }
  242.        
  243.         public function offsetGet($offset)
  244.         {
  245.             return (TRUE === $this->offsetExists($offset))
  246.                 ? $this->__array[$offset]
  247.                 : null;
  248.         }
  249.        
  250.         // IndexAccess
  251.            
  252.         /**
  253.          * index-based alias for offsetExists
  254.          */
  255.         public function indexExists($index)
  256.         {
  257.             return array_key_exists($index, $this->__offsets);
  258.         }
  259.            
  260.         /**
  261.          * index-based alias for offsetSet
  262.          */
  263.         public function indexSet($index, $value)
  264.         {
  265.             if(FALSE === $this->indexExists($index))
  266.                 throw new ArgumentOutOfRangeException(E::ARGUMENT_OUT_OF_ARRAY_INDEX,
  267.                     ['key' => $index]);
  268.            
  269.             return $this->offsetSet($this->indexToOffset($index), $value);
  270.         }
  271.            
  272.         /**
  273.          * index-based alias for offsetUnset
  274.          */
  275.         public function indexUnset($index)
  276.         {
  277.             if(FALSE === $this->indexExists($index))
  278.                 throw new ArgumentOutOfRangeException(E::ARGUMENT_OUT_OF_ARRAY_INDEX,
  279.                     ['key' => $index]);
  280.            
  281.             return $this->offsetUnset($this->indexToOffset($index));
  282.         }
  283.            
  284.         /**
  285.          * index-based alias for offsetGet
  286.          */
  287.         public function indexGet($index)
  288.         {
  289.             if(FALSE === $this->indexExists($index))
  290.                 throw new ArgumentOutOfRangeException(E::ARGUMENT_OUT_OF_ARRAY_INDEX,
  291.                     ['key' => $index]);
  292.            
  293.             return $this->offsetGet($this->indexToOffset($index));
  294.         }
  295.        
  296.         // Conversation
  297.            
  298.         /**
  299.          * get the index by offset
  300.          */
  301.         public function offsetToIndex($offset)
  302.         {
  303.             if(FALSE === $this->offsetExists($offset))
  304.                 throw new ArgumentOutOfRangeException(E::ARGUMENT_OUT_OF_ARRAY_INDEX,
  305.                     ['key' => $offset]);
  306.                
  307.             return $this->__indexes[$offset];
  308.         }
  309.        
  310.         /**
  311.          * get the offset by index
  312.          */
  313.         public function indexToOffset($index)
  314.         {
  315.             if(FALSE === $this->indexExists($index))
  316.                 throw new ArgumentOutOfRangeException(E::ARGUMENT_OUT_OF_ARRAY_INDEX,
  317.                     ['key' => $index]);
  318.            
  319.             return $this->__offsets[$index];
  320.         }
  321.        
  322.         /**
  323.          * bottom value
  324.          */
  325.         public function firstValue()
  326.         {
  327.             return $this->__array[$this->firstOffset()];
  328.         }
  329.        
  330.         /**
  331.          * top value
  332.          */
  333.         public function lastValue()
  334.         {
  335.             return $this->__array[$this->lastOffset()];
  336.         }
  337.        
  338.         /**
  339.          * bottom offset
  340.          */
  341.         public function firstOffset()
  342.         {
  343.             return $this->__offsets[$this->firstIndex()];
  344.         }
  345.        
  346.         /**
  347.          * top offset
  348.          */
  349.         public function lastOffset()
  350.         {
  351.             return $this->__offsets[$this->lastIndex()];
  352.         }
  353.        
  354.         /**
  355.          * bottom index
  356.          */
  357.         public function firstIndex()
  358.         {
  359.             return 0;
  360.         }
  361.        
  362.         /**
  363.          * top index
  364.          */
  365.         public function lastIndex()
  366.         {
  367.             return $this->count() - 1;
  368.         }
  369.        
  370.         /**
  371.          * returns the value by offset or by top-offset
  372.          */
  373.         public function peekValueByOffset($offset)
  374.         {
  375.             return (FALSE === $this->offsetExists($offset))
  376.                 ? $this->lastValue()
  377.                 : $this->offsetGet($offset);
  378.         }
  379.        
  380.         /**
  381.          * returns the value by index or by top-index
  382.          */
  383.         public function peekValueByIndex($index)
  384.         {
  385.             return (FALSE === $this->indexExists($index))
  386.                 ? $this->lastValue()
  387.                 : $this->indexGet($index);
  388.         }
  389.        
  390.         /**
  391.          * returns the index by offset or by top-offset
  392.          */
  393.         public function peekIndexByOffset($offset)
  394.         {
  395.             return (FALSE === $this->offsetExists($offset))
  396.                 ? $this->lastIndex()
  397.                 : $this->offsetToIndex($offset);
  398.         }
  399.        
  400.         /**
  401.          * returns the offset by index or by top-index
  402.          */
  403.         public function peekOffsetByIndex($index)
  404.         {
  405.             return (FALSE === $this->indexExists($index))
  406.                 ? $this->lastOffset()
  407.                 : $this->indexToOffset($index);
  408.         }
  409.        
  410.         // generic array functions
  411.        
  412.         public function push($value)
  413.         {
  414.         }
  415.        
  416.         public function toArray()
  417.         {
  418.             return $this->__array;
  419.         }
  420.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement