Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  /**
   3   * Copyright 2007-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * @todo - Incorporate stuff from Horde_Array?
   6   *       - http://docs.python.org/lib/typesmapping.html
   7   *
   8   * @category   Horde
   9   * @package    Support
  10   * @license    http://www.horde.org/licenses/bsd
  11   */
  12  class Horde_Support_Array implements ArrayAccess, Countable, IteratorAggregate
  13  {
  14      /**
  15       * Array variables
  16       */
  17      protected $_array = array();
  18  
  19      /**
  20       */
  21      public function __construct($vars = array())
  22      {
  23          if (is_array($vars)) {
  24              $this->update($vars);
  25          }
  26      }
  27  
  28      /**
  29       */
  30      public function get($key, $default = null)
  31      {
  32          return isset($this->_array[$key]) ? $this->_array[$key] : $default;
  33      }
  34  
  35      /**
  36       * Gets the value at $offset. If no value exists at that offset, or the
  37       * value $offset is NULL, then $default is set as the value of $offset.
  38       *
  39       * @param string $offset   Offset to retrieve and set if unset
  40       * @param string $default  Default value if $offset does not exist
  41       *
  42       * @return mixed Value at $offset or $default
  43       */
  44      public function getOrSet($offset, $default = null)
  45      {
  46          $value = $this->offsetGet($offset);
  47          if (is_null($value)) {
  48              $this->offsetSet($offset, $value = $default);
  49          }
  50          return $value;
  51      }
  52  
  53      /**
  54       * Gets the value at $offset and deletes it from the array. If no value
  55       * exists at $offset, or the value at $offset is null, then $default
  56       * will be returned.
  57       *
  58       * @param string $offset   Offset to pop
  59       * @param string $default  Default value
  60       *
  61       * @return mixed Value at $offset or $default
  62       */
  63      public function pop($offset, $default = null)
  64      {
  65          $value = $this->offsetGet($offset);
  66          $this->offsetUnset($offset);
  67          return isset($value) ? $value : $default;
  68      }
  69  
  70      /**
  71       * Update the array with the key/value pairs from $array
  72       *
  73       * @param array $array Key/value pairs to set/change in the array.
  74       */
  75      public function update($array)
  76      {
  77          if (!is_array($array) && !$array instanceof Traversable) {
  78              throw new InvalidArgumentException('expected array or traversable, got ' . gettype($array));
  79          }
  80  
  81          foreach ($array as $key => $val) {
  82              $this->offsetSet($key, $val);
  83          }
  84      }
  85  
  86      /**
  87       * Get the keys in the array
  88       *
  89       * @return array
  90       */
  91      public function getKeys()
  92      {
  93          return array_keys($this->_array);
  94      }
  95  
  96      /**
  97       * Get the values in the array
  98       *
  99       * @return array
 100       */
 101      public function getValues()
 102      {
 103          return array_values($this->_array);
 104      }
 105  
 106      /**
 107       * Clear out the array
 108       */
 109      public function clear()
 110      {
 111          $this->_array = array();
 112      }
 113  
 114      /**
 115       */
 116      public function __get($key)
 117      {
 118          return $this->get($key);
 119      }
 120  
 121      /**
 122       */
 123      public function __set($key, $value)
 124      {
 125          $this->_array[$key] = $value;
 126      }
 127  
 128      /**
 129       * Checks the existance of $key in this array
 130       */
 131      public function __isset($key)
 132      {
 133          return array_key_exists($key, $this->_array);
 134      }
 135  
 136      /**
 137       * Removes $key from this array
 138       */
 139      public function __unset($key)
 140      {
 141          unset($this->_array[$key]);
 142      }
 143  
 144      /**
 145       * Count the number of elements
 146       *
 147       * @return integer
 148       */
 149      #[ReturnTypeWillChange]
 150      public function count()
 151      {
 152          return count($this->_array);
 153      }
 154  
 155      /**
 156       */
 157      #[ReturnTypeWillChange]
 158      public function getIterator()
 159      {
 160          return new ArrayIterator($this->_array);
 161      }
 162  
 163      /**
 164       * Gets the value of $offset in this array
 165       *
 166       * @see __get()
 167       */
 168      #[ReturnTypeWillChange]
 169      public function offsetGet($offset)
 170      {
 171          return $this->__get($offset);
 172      }
 173  
 174      /**
 175       * Sets the value of $offset to $value
 176       *
 177       * @see __set()
 178       */
 179      #[ReturnTypeWillChange]
 180      public function offsetSet($offset, $value)
 181      {
 182          return $this->__set($offset, $value);
 183      }
 184  
 185      /**
 186       * Checks the existence of $offset in this array
 187       *
 188       * @see __isset()
 189       */
 190      #[ReturnTypeWillChange]
 191      public function offsetExists($offset)
 192      {
 193          return $this->__isset($offset);
 194      }
 195  
 196      /**
 197       * Removes $offset from this array
 198       *
 199       * @see __unset()
 200       */
 201      #[ReturnTypeWillChange]
 202      public function offsetUnset($offset)
 203      {
 204          return $this->__unset($offset);
 205      }
 206  
 207  }