WordPress / Requests
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 2
	public function __construct(array $data = array()) {
29 2
		foreach ($data as $key => $value) {
30 2
			$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 2
	public function offsetExists($key) {
41 2
		$key = strtolower($key);
42 2
		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|null Item value (null if offsetExists is false)
50
	 */
51 2
	public function offsetGet($key) {
52 2
		$key = strtolower($key);
53 2
		if (!isset($this->data[$key])) {
54 2
			return null;
55
		}
56

57 2
		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 2
	public function offsetSet($key, $value) {
69 2
		if ($key === null) {
70 0
			throw new Requests_Exception('Object is a dictionary, not a list', 'invalidset');
71
		}
72

73 2
		$key              = strtolower($key);
74 2
		$this->data[$key] = $value;
75
	}
76

77
	/**
78
	 * Unset the given header
79
	 *
80
	 * @param string $key
81
	 */
82 2
	public function offsetUnset($key) {
83 2
		unset($this->data[strtolower($key)]);
84
	}
85

86
	/**
87
	 * Get an iterator for the data
88
	 *
89
	 * @return ArrayIterator
90
	 */
91 2
	public function getIterator() {
92 2
		return new ArrayIterator($this->data);
93
	}
94

95
	/**
96
	 * Get the headers as an array
97
	 *
98
	 * @return array Header data
99
	 */
100 0
	public function getAll() {
101 0
		return $this->data;
102
	}
103
}

Read our documentation on viewing source code .

Loading