Changeset 52244 for trunk/src/wp-includes/Requests/Response/Headers.php
- Timestamp:
- 11/25/2021 01:10:30 AM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/Requests/Response/Headers.php
r50842 r52244 6 6 */ 7 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 8 15 /** 9 16 * Case-insensitive dictionary, suitable for HTTP headers … … 11 18 * @package Requests 12 19 */ 13 class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {20 class Headers extends CaseInsensitiveDictionary { 14 21 /** 15 22 * Get the given header 16 23 * 17 * Unlike {@see self::getValues()}, this returns a string. If there are24 * Unlike {@see \WpOrg\Requests\Response\Headers::getValues()}, this returns a string. If there are 18 25 * multiple values, it concatenates them with a comma as per RFC2616. 19 26 * … … 21 28 * Set-Cookie headers. 22 29 * 23 * @param string $ key30 * @param string $offset 24 31 * @return string|null Header value 25 32 */ 26 public function offsetGet($key) { 27 $key = strtolower($key); 28 if (!isset($this->data[$key])) { 33 public function offsetGet($offset) { 34 if (is_string($offset)) { 35 $offset = strtolower($offset); 36 } 37 38 if (!isset($this->data[$offset])) { 29 39 return null; 30 40 } 31 41 32 return $this->flatten($this->data[$ key]);42 return $this->flatten($this->data[$offset]); 33 43 } 34 44 … … 36 46 * Set the given item 37 47 * 38 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`) 48 * @param string $offset Item name 49 * @param string $value Item value 39 50 * 40 * @param string $key Item name 41 * @param string $value Item value 51 * @throws \WpOrg\Requests\Exception On attempting to use dictionary as list (`invalidset`) 42 52 */ 43 public function offsetSet($ key, $value) {44 if ($ key=== null) {45 throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');53 public function offsetSet($offset, $value) { 54 if ($offset === null) { 55 throw new Exception('Object is a dictionary, not a list', 'invalidset'); 46 56 } 47 57 48 $key = strtolower($key); 49 50 if (!isset($this->data[$key])) { 51 $this->data[$key] = array(); 58 if (is_string($offset)) { 59 $offset = strtolower($offset); 52 60 } 53 61 54 $this->data[$key][] = $value; 62 if (!isset($this->data[$offset])) { 63 $this->data[$offset] = []; 64 } 65 66 $this->data[$offset][] = $value; 55 67 } 56 68 … … 58 70 * Get all values for a given header 59 71 * 60 * @param string $ key72 * @param string $offset 61 73 * @return array|null Header values 74 * 75 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not valid as an array key. 62 76 */ 63 public function getValues($key) { 64 $key = strtolower($key); 65 if (!isset($this->data[$key])) { 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])) { 66 84 return null; 67 85 } 68 86 69 return $this->data[$ key];87 return $this->data[$offset]; 70 88 } 71 89 … … 78 96 * @param string|array $value Value to flatten 79 97 * @return string Flattened value 98 * 99 * @throws \WpOrg\Requests\Exception\InvalidArgument When the passed argument is not a string or an array. 80 100 */ 81 101 public function flatten($value) { 82 if (is_ array($value)) {83 $value = implode(',', $value);102 if (is_string($value)) { 103 return $value; 84 104 } 85 105 86 return $value; 106 if (is_array($value)) { 107 return implode(',', $value); 108 } 109 110 throw InvalidArgument::create(1, '$value', 'string|array', gettype($value)); 87 111 } 88 112 … … 90 114 * Get an iterator for the data 91 115 * 92 * Converts the internal 93 * @return ArrayIterator 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 94 120 */ 95 121 public function getIterator() { 96 return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));122 return new FilteredIterator($this->data, [$this, 'flatten']); 97 123 } 98 124 }
Note: See TracChangeset
for help on using the changeset viewer.