]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - includes/Requests/Requests/Utility/CaseInsensitiveDictionary.php
Add Requests 1.6
[Github/YOURLS.git] / includes / Requests / 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          * Check if the given item exists
25          *
26          * @param string $key Item key
27          * @return boolean Does the item exist?
28          */
29         public function offsetExists($key) {
30                 $key = strtolower($key);
31                 return isset($this->data[$key]);
32         }
33
34         /**
35          * Get the value for the item
36          *
37          * @param string $key Item key
38          * @return string Item value
39          */
40         public function offsetGet($key) {
41                 $key = strtolower($key);
42                 if (!isset($this->data[$key]))
43                         return null;
44
45                 return $this->data[$key];
46         }
47
48         /**
49          * Set the given item
50          *
51          * @throws Requests_Exception On attempting to use dictionary as list (`invalidset`)
52          *
53          * @param string $key Item name
54          * @param string $value Item value
55          */
56         public function offsetSet($key, $value) {
57                 if ($key === null) {
58                         throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
59                 }
60
61                 $key = strtolower($key);
62                 $this->data[$key] = $value;
63         }
64
65         /**
66          * Unset the given header
67          *
68          * @param string $key
69          */
70         public function offsetUnset($key) {
71                 unset($this->data[strtolower($key)]);
72         }
73
74         /**
75          * Get an iterator for the data
76          *
77          * @return ArrayIterator
78          */
79         public function getIterator() {
80                 return new ArrayIterator($this->data);
81         }
82
83         /**
84          * Get the headers as an array
85          *
86          * @return array Header data
87          */
88         public function getAll() {
89                 return $this->data;
90         }
91 }