[ Index ]

PHP Cross Reference of YOURLS

title

Body

[close]

/includes/vendor/rmccue/requests/src/Response/ -> Headers.php (source)

   1  <?php
   2  /**
   3   * Case-insensitive dictionary, suitable for HTTP headers
   4   *
   5   * @package Requests
   6   */
   7  
   8  namespace WpOrg\Requests\Response;
   9  
  10  use WpOrg\Requests\Exception;
  11  use WpOrg\Requests\Exception\InvalidArgument;
  12  use WpOrg\Requests\Utility\CaseInsensitiveDictionary;
  13  use WpOrg\Requests\Utility\FilteredIterator;
  14  
  15  /**
  16   * Case-insensitive dictionary, suitable for HTTP headers
  17   *
  18   * @package Requests
  19   */
  20  class Headers extends CaseInsensitiveDictionary {
  21      /**
  22       * Get the given header
  23       *
  24       * Unlike {@see \WpOrg\Requests\Response\Headers::getValues()}, this returns a string. If there are
  25       * multiple values, it concatenates them with a comma as per RFC2616.
  26       *
  27       * Avoid using this where commas may be used unquoted in values, such as
  28       * Set-Cookie headers.
  29       *
  30       * @param string $offset
  31       * @return string|null Header value
  32       */
  33  	public function offsetGet($offset) {
  34          if (is_string($offset)) {
  35              $offset = strtolower($offset);
  36          }
  37  
  38          if (!isset($this->data[$offset])) {
  39              return null;
  40          }
  41  
  42          return $this->flatten($this->data[$offset]);
  43      }
  44  
  45      /**
  46       * Set the given item
  47       *
  48       * @param string $offset Item name
  49       * @param string $value Item value
  50       *
  51       * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`)
  52       */
  53  	public function offsetSet($offset, $value) {
  54          if ($offset === null) {
  55              throw new Exception('Object is a dictionary, not a list', 'invalidset');
  56          }
  57  
  58          if (is_string($offset)) {
  59              $offset = strtolower($offset);
  60          }
  61  
  62          if (!isset($this->data[$offset])) {
  63              $this->data[$offset] = [];
  64          }
  65  
  66          $this->data[$offset][] = $value;
  67      }
  68  
  69      /**
  70       * Get all values for a given header
  71       *
  72       * @param string $offset
  73       * @return array|null Header values
  74       *
  75       * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not valid as an array key.
  76       */
  77  	public function getValues($offset) {
  78          if (!is_string($offset) && !is_int($offset)) {
  79              throw InvalidArgument::create(1, '$offset', 'string|int', gettype($offset));
  80          }
  81  
  82          $offset = strtolower($offset);
  83          if (!isset($this->data[$offset])) {
  84              return null;
  85          }
  86  
  87          return $this->data[$offset];
  88      }
  89  
  90      /**
  91       * Flattens a value into a string
  92       *
  93       * Converts an array into a string by imploding values with a comma, as per
  94       * RFC2616's rules for folding headers.
  95       *
  96       * @param string|array $value Value to flatten
  97       * @return string Flattened value
  98       *
  99       * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or an array.
 100       */
 101  	public function flatten($value) {
 102          if (is_string($value)) {
 103              return $value;
 104          }
 105  
 106          if (is_array($value)) {
 107              return implode(',', $value);
 108          }
 109  
 110          throw InvalidArgument::create(1, '$value', 'string|array', gettype($value));
 111      }
 112  
 113      /**
 114       * Get an iterator for the data
 115       *
 116       * Converts the internally stored values to a comma-separated string if there is more
 117       * than one value for a key.
 118       *
 119       * @return \ArrayIterator
 120       */
 121  	public function getIterator() {
 122          return new FilteredIterator($this->data, [$this, 'flatten']);
 123      }
 124  }


Generated: Tue Jan 21 05:10:11 2025 Cross-referenced by PHPXref 0.7.1