Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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      #[ReturnTypeWillChange]
  28      public function offsetGet($offset)
  29      {
  30          return (is_null($offset = $this->_getRealOffset($offset)))
  31              ? null
  32              : parent::offsetGet($offset);
  33      }
  34  
  35      /**
  36       */
  37      #[ReturnTypeWillChange]
  38      public function offsetSet($offset, $value)
  39      {
  40          if (is_null($roffset = $this->_getRealOffset($offset))) {
  41              parent::offsetSet($offset, $value);
  42          } else {
  43              parent::offsetSet($roffset, $value);
  44          }
  45      }
  46  
  47      /**
  48       */
  49      #[ReturnTypeWillChange]
  50      public function offsetExists($offset)
  51      {
  52          return !is_null($offset = $this->_getRealOffset($offset));
  53      }
  54  
  55      /**
  56       */
  57      #[ReturnTypeWillChange]
  58      public function offsetUnset($offset)
  59      {
  60          if (!is_null($offset = $this->_getRealOffset($offset))) {
  61              parent::offsetUnset($offset);
  62          }
  63      }
  64  
  65      /**
  66       * Determines the actual array offset given the input offset.
  67       *
  68       * @param string $offset  Input offset.
  69       *
  70       * @return string  Real offset or null.
  71       */
  72      protected function _getRealOffset($offset)
  73      {
  74          /* Optimize: check for base $offset in array first. */
  75          if (parent::offsetExists($offset)) {
  76              return $offset;
  77          }
  78  
  79          foreach (array_keys($this->getArrayCopy()) as $key) {
  80              if (strcasecmp($key, $offset) === 0) {
  81                  return $key;
  82              }
  83          }
  84  
  85          return null;
  86      }
  87  
  88  }