1: <?php
2:
3: namespace PHPixie\HTTP\Data;
4:
5: /**
6: * Header storage
7: */
8: class Headers
9: {
10: /**
11: * @var array
12: */
13: protected $headers = array();
14:
15: /**
16: * @var array
17: */
18: protected $names;
19:
20: /**
21: * Constructor
22: * @param array $headers Headers map
23: */
24: public function __construct($headers = array())
25: {
26: foreach($headers as $key => $value) {
27: $this->headers[$key] = $this->normalizeValue($value);
28: }
29: }
30:
31: /**
32: * Get joined header values, or if missing return a default value
33: * @param string $name
34: * @param string $default
35: * @return string
36: */
37: public function get($name, $default = '')
38: {
39: $lines = $this->getLines($name);
40: if(count($lines) === 0) {
41: return $default;
42: }
43:
44: return $this->implode($lines);
45: }
46:
47: /**
48: * Get joined header values, or if missing throw an exception
49: * @param string $name
50: * @return string
51: * @throw \PHPixie\HTTP\Exception
52: */
53: public function getRequired($name)
54: {
55: $lines = $this->getRequiredLines($name);
56: return $this->implode($lines);
57: }
58:
59: /**
60: * Get header lines, or if missing return a default array
61: * @param string $name
62: * @param array $default
63: * @return array
64: */
65: public function getLines($name, $default = array())
66: {
67: $name = $this->normalizeName($name);
68: if(!$this->existsNormalized($name)) {
69: return $default;
70: }
71:
72: return $this->headers[$name];
73: }
74:
75: /**
76: * Get header lines, or if missing throw an exception
77: * @param string $name
78: * @return array
79: * @throws \PHPixie\HTTP\Exception
80: */
81: public function getRequiredLines($name)
82: {
83: $name = $this->normalizeName($name);
84:
85: if(!$this->existsNormalized($name)) {
86: throw new \PHPixie\HTTP\Exception("Header '$name' is not set");
87: }
88:
89: return $this->headers[$name];
90: }
91:
92: /**
93: * Get headers array
94: * @return array
95: */
96: public function asArray()
97: {
98: return $this->headers;
99: }
100:
101: /**
102: * @param string $normlizedName
103: * @return bool
104: */
105: protected function existsNormalized($normlizedName)
106: {
107: return array_key_exists($normlizedName, $this->headers);
108: }
109:
110: /**
111: * @param array $lines
112: * @return string
113: */
114: protected function implode($lines)
115: {
116: return implode(',', $lines);
117: }
118:
119: /**
120: * @param string $name
121: * @return string
122: */
123: protected function normalizeName($name)
124: {
125: $this->requireNames();
126:
127: $lower = strtolower($name);
128:
129: if(array_key_exists($lower, $this->names)) {
130: return $this->names[$lower];
131: }
132:
133: return $name;
134: }
135:
136: /**
137: * @param string $value
138: * @return array
139: */
140: protected function normalizeValue($value)
141: {
142: if(!is_array($value)) {
143: return array($value);
144: }
145:
146: return $value;
147: }
148:
149: /**
150: * @return void
151: */
152: protected function requireNames()
153: {
154: if($this->names === null) {
155: $this->names = array();
156: foreach($this->headers as $name => $value) {
157: $this->names[strtolower($name)] = $name;
158: }
159: }
160: }
161: }