offsetGet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 8
loc 8
rs 10
c 0
b 0
f 0
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|null Item value (null if offsetExists is false)
50
	 */
51 View Code Duplication
	public function offsetGet($key) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
}
104