[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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 Name of the header to retrieve. 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 Name of the header to retrieve. 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 if (is_string($offset)) { 83 $offset = strtolower($offset); 84 } 85 86 if (!isset($this->data[$offset])) { 87 return null; 88 } 89 90 return $this->data[$offset]; 91 } 92 93 /** 94 * Flattens a value into a string 95 * 96 * Converts an array into a string by imploding values with a comma, as per 97 * RFC2616's rules for folding headers. 98 * 99 * @param string|array $value Value to flatten 100 * @return string Flattened value 101 * 102 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or an array. 103 */ 104 public function flatten($value) { 105 if (is_string($value)) { 106 return $value; 107 } 108 109 if (is_array($value)) { 110 return implode(',', $value); 111 } 112 113 throw InvalidArgument::create(1, '$value', 'string|array', gettype($value)); 114 } 115 116 /** 117 * Get an iterator for the data 118 * 119 * Converts the internally stored values to a comma-separated string if there is more 120 * than one value for a key. 121 * 122 * @return \ArrayIterator 123 */ 124 public function getIterator() { 125 return new FilteredIterator($this->data, [$this, 'flatten']); 126 } 127 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Nov 23 08:20:01 2024 | Cross-referenced by PHPXref |