[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/sodium_compat/src/PHP52/ -> SplFixedArray.php (source)

   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          return $this->internalArray[(int) $index];
 106      }
 107  
 108      /**
 109       * @param string|int $index
 110       * @param mixed $newval
 111       * @psalm-suppress MixedAssignment
 112       */
 113      public function offsetSet($index, $newval)
 114      {
 115          $this->internalArray[(int) $index] = $newval;
 116      }
 117  
 118      /**
 119       * @param string|int $index
 120       */
 121      public function offsetUnset($index)
 122      {
 123          unset($this->internalArray[(int) $index]);
 124      }
 125  
 126      /**
 127       * Rewind iterator back to the start
 128       * @link https://php.net/manual/en/splfixedarray.rewind.php
 129       * @return void
 130       * @since 5.3.0
 131       */
 132      public function rewind()
 133      {
 134          reset($this->internalArray);
 135      }
 136  
 137      /**
 138       * Return current array entry
 139       * @link https://php.net/manual/en/splfixedarray.current.php
 140       * @return mixed The current element value.
 141       * @since 5.3.0
 142       */
 143      public function current()
 144      {
 145          return current($this->internalArray);
 146      }
 147  
 148      /**
 149       * Return current array index
 150       * @return int The current array index.
 151       */
 152      public function key()
 153      {
 154          return key($this->internalArray);
 155      }
 156  
 157      /**
 158       * @return void
 159       */
 160      public function next()
 161      {
 162          next($this->internalArray);
 163      }
 164  
 165      /**
 166       * Check whether the array contains more elements
 167       * @link https://php.net/manual/en/splfixedarray.valid.php
 168       * @return bool true if the array contains any more elements, false otherwise.
 169       */
 170      public function valid()
 171      {
 172          if (empty($this->internalArray)) {
 173              return false;
 174          }
 175          $result = next($this->internalArray) !== false;
 176          prev($this->internalArray);
 177          return $result;
 178      }
 179  
 180      /**
 181       * Do nothing.
 182       */
 183      public function __wakeup()
 184      {
 185          // NOP
 186      }
 187  }


Generated : Fri Apr 19 08:20:01 2024 Cross-referenced by PHPXref