]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/Requests/Requests/Response/Headers.php
Add Requests 1.6
[Github/YOURLS.git] / includes / Requests / Requests / Response / Headers.php
1 <?php
2 /**
3  * Case-insensitive dictionary, suitable for HTTP headers
4  *
5  * @package Requests
6  */
7
8 /**
9  * Case-insensitive dictionary, suitable for HTTP headers
10  *
11  * @package Requests
12  */
13 class Requests_Response_Headers extends Requests_Utility_CaseInsensitiveDictionary {
14         /**
15          * Get the given header
16          *
17          * Unlike {@see self::getValues()}, this returns a string. If there are
18          * multiple values, it concatenates them with a comma as per RFC2616.
19          *
20          * Avoid using this where commas may be used unquoted in values, such as
21          * Set-Cookie headers.
22          *
23          * @param string $key
24          * @return string Header value
25          */
26         public function offsetGet($key) {
27                 $key = strtolower($key);
28                 if (!isset($this->data[$key]))
29                         return null;
30
31                 return $this->flatten($this->data[$key]);
32         }
33
34         /**
35          * Set the given item
36          *
37          * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
38          *
39          * @param string $key Item name
40          * @param string $value Item value
41          */
42         public function offsetSet($key, $value) {
43                 if ($key === null) {
44                         throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
45                 }
46
47                 $key = strtolower($key);
48
49                 if (!isset($this->data[$key])) {
50                         $this->data[$key] = array();
51                 }
52
53                 $this->data[$key][] = $value;
54         }
55
56         /**
57          * Get all values for a given header
58          *
59          * @param string $key
60          * @return array Header values
61          */
62         public function getValues($key) {
63                 $key = strtolower($key);
64                 if (!isset($this->data[$key]))
65                         return null;
66
67                 return $this->data[$key];
68         }
69
70         /**
71          * Flattens a value into a string
72          *
73          * Converts an array into a string by imploding values with a comma, as per
74          * RFC2616's rules for folding headers.
75          *
76          * @param string|array $value Value to flatten
77          * @return string Flattened value
78          */
79         public function flatten($value) {
80                 if (is_array($value))
81                         $value = implode(',', $value);
82
83                 return $value;
84         }
85
86         /**
87          * Get an iterator for the data
88          *
89          * Converts the internal
90          * @return ArrayIterator
91          */
92         public function getIterator() {
93                 return new Requests_Utility_FilteredIterator($this->data, array($this, 'flatten'));
94         }
95 }