balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

SplFixedArray.php (4116B)


      1 <?php
      2 
      3 if (class_exists('SplFixedArray')) {
      4     return;
      5 }
      6 
      7 /**
      8  * The SplFixedArray class provides the main functionalities of array. The
      9  * main differences between a SplFixedArray and a normal PHP array is that
     10  * the SplFixedArray is of fixed length and allows only integers within
     11  * the range as indexes. The advantage is that it allows a faster array
     12  * implementation.
     13  */
     14 class SplFixedArray implements Iterator, ArrayAccess, Countable
     15 {
     16     /** @var array<int, mixed> */
     17     private $internalArray = array();
     18 
     19     /** @var int $size */
     20     private $size = 0;
     21 
     22     /**
     23      * SplFixedArray constructor.
     24      * @param int $size
     25      */
     26     public function __construct($size = 0)
     27     {
     28         $this->size = $size;
     29         $this->internalArray = array();
     30     }
     31 
     32     /**
     33      * @return int
     34      */
     35     public function count()
     36     {
     37         return count($this->internalArray);
     38     }
     39 
     40     /**
     41      * @return array
     42      */
     43     public function toArray()
     44     {
     45         ksort($this->internalArray);
     46         return (array) $this->internalArray;
     47     }
     48 
     49     /**
     50      * @param array $array
     51      * @param bool $save_indexes
     52      * @return SplFixedArray
     53      * @psalm-suppress MixedAssignment
     54      */
     55     public static function fromArray(array $array, $save_indexes = true)
     56     {
     57         $self = new SplFixedArray(count($array));
     58         if($save_indexes) {
     59             foreach($array as $key => $value) {
     60                 $self[(int) $key] = $value;
     61             }
     62         } else {
     63             $i = 0;
     64             foreach (array_values($array) as $value) {
     65                 $self[$i] = $value;
     66                 $i++;
     67             }
     68         }
     69         return $self;
     70     }
     71 
     72     /**
     73      * @return int
     74      */
     75     public function getSize()
     76     {
     77         return $this->size;
     78     }
     79 
     80     /**
     81      * @param int $size
     82      * @return bool
     83      */
     84     public function setSize($size)
     85     {
     86         $this->size = $size;
     87         return true;
     88     }
     89 
     90     /**
     91      * @param string|int $index
     92      * @return bool
     93      */
     94     public function offsetExists($index)
     95     {
     96         return array_key_exists((int) $index, $this->internalArray);
     97     }
     98 
     99     /**
    100      * @param string|int $index
    101      * @return mixed
    102      */
    103     public function offsetGet($index)
    104     {
    105         /** @psalm-suppress MixedReturnStatement */
    106         return $this->internalArray[(int) $index];
    107     }
    108 
    109     /**
    110      * @param string|int $index
    111      * @param mixed $newval
    112      * @psalm-suppress MixedAssignment
    113      */
    114     public function offsetSet($index, $newval)
    115     {
    116         $this->internalArray[(int) $index] = $newval;
    117     }
    118 
    119     /**
    120      * @param string|int $index
    121      */
    122     public function offsetUnset($index)
    123     {
    124         unset($this->internalArray[(int) $index]);
    125     }
    126 
    127     /**
    128      * Rewind iterator back to the start
    129      * @link https://php.net/manual/en/splfixedarray.rewind.php
    130      * @return void
    131      * @since 5.3.0
    132      */
    133     public function rewind()
    134     {
    135         reset($this->internalArray);
    136     }
    137 
    138     /**
    139      * Return current array entry
    140      * @link https://php.net/manual/en/splfixedarray.current.php
    141      * @return mixed The current element value.
    142      * @since 5.3.0
    143      */
    144     public function current()
    145     {
    146         /** @psalm-suppress MixedReturnStatement */
    147         return current($this->internalArray);
    148     }
    149 
    150     /**
    151      * Return current array index
    152      * @return int The current array index.
    153      */
    154     public function key()
    155     {
    156         return key($this->internalArray);
    157     }
    158 
    159     /**
    160      * @return void
    161      */
    162     public function next()
    163     {
    164         next($this->internalArray);
    165     }
    166 
    167     /**
    168      * Check whether the array contains more elements
    169      * @link https://php.net/manual/en/splfixedarray.valid.php
    170      * @return bool true if the array contains any more elements, false otherwise.
    171      */
    172     public function valid()
    173     {
    174         if (empty($this->internalArray)) {
    175             return false;
    176         }
    177         $result = next($this->internalArray) !== false;
    178         prev($this->internalArray);
    179         return $result;
    180     }
    181 
    182     /**
    183      * Do nothing.
    184      */
    185     public function __wakeup()
    186     {
    187         // NOP
    188     }
    189 }