Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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      public function count()
 150      {
 151          return count($this->_array);
 152      }
 153  
 154      /**
 155       */
 156      public function getIterator()
 157      {
 158          return new ArrayIterator($this->_array);
 159      }
 160  
 161      /**
 162       * Gets the value of $offset in this array
 163       *
 164       * @see __get()
 165       */
 166      public function offsetGet($offset)
 167      {
 168          return $this->__get($offset);
 169      }
 170  
 171      /**
 172       * Sets the value of $offset to $value
 173       *
 174       * @see __set()
 175       */
 176      public function offsetSet($offset, $value)
 177      {
 178          return $this->__set($offset, $value);
 179      }
 180  
 181      /**
 182       * Checks the existence of $offset in this array
 183       *
 184       * @see __isset()
 185       */
 186      public function offsetExists($offset)
 187      {
 188          return $this->__isset($offset);
 189      }
 190  
 191      /**
 192       * Removes $offset from this array
 193       *
 194       * @see __unset()
 195       */
 196      public function offsetUnset($offset)
 197      {
 198          return $this->__unset($offset);
 199      }
 200  
 201  }