[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/Requests/src/Utility/ -> CaseInsensitiveDictionary.php (source)

   1  <?php
   2  /**
   3   * Case-insensitive dictionary, suitable for HTTP headers
   4   *
   5   * @package Requests\Utilities
   6   */
   7  
   8  namespace WpOrg\Requests\Utility;
   9  
  10  use ArrayAccess;
  11  use ArrayIterator;
  12  use IteratorAggregate;
  13  use ReturnTypeWillChange;
  14  use WpOrg\Requests\Exception;
  15  
  16  /**
  17   * Case-insensitive dictionary, suitable for HTTP headers
  18   *
  19   * @package Requests\Utilities
  20   */
  21  class CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate {
  22      /**
  23       * Actual item data
  24       *
  25       * @var array
  26       */
  27      protected $data = [];
  28  
  29      /**
  30       * Creates a case insensitive dictionary.
  31       *
  32       * @param array $data Dictionary/map to convert to case-insensitive
  33       */
  34  	public function __construct(array $data = []) {
  35          foreach ($data as $offset => $value) {
  36              $this->offsetSet($offset, $value);
  37          }
  38      }
  39  
  40      /**
  41       * Check if the given item exists
  42       *
  43       * @param string $offset Item key
  44       * @return boolean Does the item exist?
  45       */
  46      #[ReturnTypeWillChange]
  47  	public function offsetExists($offset) {
  48          if (is_string($offset)) {
  49              $offset = strtolower($offset);
  50          }
  51  
  52          return isset($this->data[$offset]);
  53      }
  54  
  55      /**
  56       * Get the value for the item
  57       *
  58       * @param string $offset Item key
  59       * @return string|null Item value (null if the item key doesn't exist)
  60       */
  61      #[ReturnTypeWillChange]
  62  	public function offsetGet($offset) {
  63          if (is_string($offset)) {
  64              $offset = strtolower($offset);
  65          }
  66  
  67          if (!isset($this->data[$offset])) {
  68              return null;
  69          }
  70  
  71          return $this->data[$offset];
  72      }
  73  
  74      /**
  75       * Set the given item
  76       *
  77       * @param string $offset Item name
  78       * @param string $value Item value
  79       *
  80       * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)
  81       */
  82      #[ReturnTypeWillChange]
  83  	public function offsetSet($offset, $value) {
  84          if ($offset === null) {
  85              throw new Exception('Object is a dictionary, not a list', 'invalidset');
  86          }
  87  
  88          if (is_string($offset)) {
  89              $offset = strtolower($offset);
  90          }
  91  
  92          $this->data[$offset] = $value;
  93      }
  94  
  95      /**
  96       * Unset the given header
  97       *
  98       * @param string $offset The key for the item to unset.
  99       */
 100      #[ReturnTypeWillChange]
 101  	public function offsetUnset($offset) {
 102          if (is_string($offset)) {
 103              $offset = strtolower($offset);
 104          }
 105  
 106          unset($this->data[$offset]);
 107      }
 108  
 109      /**
 110       * Get an iterator for the data
 111       *
 112       * @return \ArrayIterator
 113       */
 114      #[ReturnTypeWillChange]
 115  	public function getIterator() {
 116          return new ArrayIterator($this->data);
 117      }
 118  
 119      /**
 120       * Get the headers as an array
 121       *
 122       * @return array Header data
 123       */
 124  	public function getAll() {
 125          return $this->data;
 126      }
 127  }


Generated : Sat Apr 27 08:20:02 2024 Cross-referenced by PHPXref