]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/Requests/Utility/CaseInsensitiveDictionary.php
WordPress 4.6.1
[autoinstalls/wordpress.git] / wp-includes / Requests / Utility / CaseInsensitiveDictionary.php
1 <?php
2 /**
3  * Case-insensitive dictionary, suitable for HTTP headers
4  *
5  * @package Requests
6  * @subpackage Utilities
7  */
8
9 /**
10  * Case-insensitive dictionary, suitable for HTTP headers
11  *
12  * @package Requests
13  * @subpackage Utilities
14  */
15 class Requests_Utility_CaseInsensitiveDictionary implements ArrayAccess, IteratorAggregate {
16         /**
17          * Actual item data
18          *
19          * @var array
20          */
21         protected $data = array();
22
23         /**
24          * Creates a case insensitive dictionary.
25          *
26          * @param array $data Dictionary/map to convert to case-insensitive
27          */
28         public function __construct(array $data = array()) {
29                 foreach ($data as $key => $value) {
30                         $this->offsetSet($key, $value);
31                 }
32         }
33
34         /**
35          * Check if the given item exists
36          *
37          * @param string $key Item key
38          * @return boolean Does the item exist?
39          */
40         public function offsetExists($key) {
41                 $key = strtolower($key);
42                 return isset($this->data[$key]);
43         }
44
45         /**
46          * Get the value for the item
47          *
48          * @param string $key Item key
49          * @return string Item value
50          */
51         public function offsetGet($key) {
52                 $key = strtolower($key);
53                 if (!isset($this->data[$key])) {
54                         return null;
55                 }
56
57                 return $this->data[$key];
58         }
59
60         /**
61          * Set the given item
62          *
63          * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
64          *
65          * @param string $key Item name
66          * @param string $value Item value
67          */
68         public function offsetSet($key, $value) {
69                 if ($key === null) {
70                         throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
71                 }
72
73                 $key = strtolower($key);
74                 $this->data[$key] = $value;
75         }
76
77         /**
78          * Unset the given header
79          *
80          * @param string $key
81          */
82         public function offsetUnset($key) {
83                 unset($this->data[strtolower($key)]);
84         }
85
86         /**
87          * Get an iterator for the data
88          *
89          * @return ArrayIterator
90          */
91         public function getIterator() {
92                 return new ArrayIterator($this->data);
93         }
94
95         /**
96          * Get the headers as an array
97          *
98          * @return array Header data
99          */
100         public function getAll() {
101                 return $this->data;
102         }
103 }