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 2013-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (BSD). If you
   6   * did not receive this file, see http://www.horde.org/licenses/bsd.
   7   *
   8   * @category  Horde
   9   * @copyright 2013-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/bsd BSD
  11   * @package   Support
  12   */
  13  
  14  /**
  15   * An array implemented as an object that contains case-insensitive keys.
  16   *
  17   * @author    Michael Slusarz <slusarz@horde.org>
  18   * @category  Horde
  19   * @copyright 2013-2017 Horde LLC
  20   * @license   http://www.horde.org/licenses/bsd BSD
  21   * @package   Support
  22   */
  23  class Horde_Support_CaseInsensitiveArray extends ArrayIterator
  24  {
  25      /**
  26       */
  27      public function offsetGet($offset)
  28      {
  29          return (is_null($offset = $this->_getRealOffset($offset)))
  30              ? null
  31              : parent::offsetGet($offset);
  32      }
  33  
  34      /**
  35       */
  36      public function offsetSet($offset, $value)
  37      {
  38          if (is_null($roffset = $this->_getRealOffset($offset))) {
  39              parent::offsetSet($offset, $value);
  40          } else {
  41              parent::offsetSet($roffset, $value);
  42          }
  43      }
  44  
  45      /**
  46       */
  47      public function offsetExists($offset)
  48      {
  49          return !is_null($offset = $this->_getRealOffset($offset));
  50      }
  51  
  52      /**
  53       */
  54      public function offsetUnset($offset)
  55      {
  56          if (!is_null($offset = $this->_getRealOffset($offset))) {
  57              parent::offsetUnset($offset);
  58          }
  59      }
  60  
  61      /**
  62       * Determines the actual array offset given the input offset.
  63       *
  64       * @param string $offset  Input offset.
  65       *
  66       * @return string  Real offset or null.
  67       */
  68      protected function _getRealOffset($offset)
  69      {
  70          /* Optimize: check for base $offset in array first. */
  71          if (parent::offsetExists($offset)) {
  72              return $offset;
  73          }
  74  
  75          foreach (array_keys($this->getArrayCopy()) as $key) {
  76              if (strcasecmp($key, $offset) === 0) {
  77                  return $key;
  78              }
  79          }
  80  
  81          return null;
  82      }
  83  
  84  }