3 * Case-insensitive dictionary, suitable for HTTP headers
9 * Case-insensitive dictionary, suitable for HTTP headers
13 class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {
15 * Get the given header
17 * Unlike {@see self::getValues()}, this returns a string. If there are
18 * multiple values, it concatenates them with a comma as per RFC2616.
20 * Avoid using this where commas may be used unquoted in values, such as
24 * @return string Header value
26 public function offsetGet($key) {
27 $key = strtolower($key);
28 if (!isset($this->data[$key]))
31 return $this->flatten($this->data[$key]);
37 * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
39 * @param string $key Item name
40 * @param string $value Item value
42 public function offsetSet($key, $value) {
44 throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
47 $key = strtolower($key);
49 if (!isset($this->data[$key])) {
50 $this->data[$key] = array();
53 $this->data[$key][] = $value;
57 * Get all values for a given header
60 * @return array Header values
62 public function getValues($key) {
63 $key = strtolower($key);
64 if (!isset($this->data[$key]))
67 return $this->data[$key];
71 * Flattens a value into a string
73 * Converts an array into a string by imploding values with a comma, as per
74 * RFC2616's rules for folding headers.
76 * @param string|array $value Value to flatten
77 * @return string Flattened value
79 public function flatten($value) {
81 $value = implode(',', $value);
87 * Get an iterator for the data
89 * Converts the internal
90 * @return ArrayIterator
92 public function getIterator() {
93 return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));