1: <?php
2:
3: namespace Guzzle\Http\Message;
4:
5: use Guzzle\Common\Collection;
6: use Guzzle\Common\Exception\InvalidArgumentException;
7:
8: 9: 10:
11: abstract class AbstractMessage implements MessageInterface
12: {
13: 14: 15:
16: protected = array();
17:
18: 19: 20:
21: protected $params;
22:
23: 24: 25:
26: private $cacheControl = array();
27:
28: 29: 30:
31: protected $protocolVersion = '1.1';
32:
33: 34: 35:
36: public function getParams()
37: {
38: return $this->params;
39: }
40:
41: 42: 43:
44: public function ($header, $value)
45: {
46: $key = strtolower($header);
47: if (!isset($this->headers[$key])) {
48: $this->headers[$key] = new Header($header, $value);
49: } else {
50: $this->headers[$key]->add($value, $header);
51: }
52: $this->changedHeader($key);
53:
54: return $this;
55: }
56:
57: 58: 59:
60: public function (array $headers)
61: {
62: foreach ($headers as $key => $value) {
63: $this->addHeader($key, $value);
64: }
65:
66: return $this;
67: }
68:
69: 70: 71:
72: public function ($header, $string = false)
73: {
74: $key = strtolower($header);
75: if (!isset($this->headers[$key])) {
76: return null;
77: }
78:
79: return $string ? (string) $this->headers[$key] : $this->headers[$key];
80: }
81:
82: 83: 84:
85: public function ($asObjects = false)
86: {
87: if ($asObjects) {
88: $result = $this->headers;
89: } else {
90: $result = array();
91:
92: foreach ($this->headers as $header) {
93: foreach ($header->raw() as $key => $value) {
94: $result[$key] = $value;
95: }
96: }
97: }
98:
99: return new Collection($result);
100: }
101:
102: 103: 104:
105: public function ()
106: {
107: $headers = array();
108: foreach ($this->headers as $value) {
109: $glue = $value->getGlue();
110: foreach ($value->raw() as $key => $v) {
111: $headers[] = rtrim($key . ': ' . implode($glue, $v));
112: }
113: }
114:
115: return $headers;
116: }
117:
118: 119: 120:
121: public function ($header, $value)
122: {
123:
124: $key = strtolower($header);
125: unset($this->headers[$key]);
126:
127: if ($value instanceof Header) {
128: $this->headers[$key] = $value;
129: } else {
130:
131: if (!$value) {
132: $value = array($value);
133: }
134: $this->headers[$key] = new Header($header, $value);
135: }
136: $this->changedHeader($key);
137:
138: return $this;
139: }
140:
141: 142: 143:
144: public function (array $headers)
145: {
146:
147: $changed = array_keys($this->headers);
148:
149: $this->headers = array();
150:
151: foreach ($headers as $key => $value) {
152: $changed[] = $key;
153: $this->addHeader($key, $value);
154: }
155:
156:
157: foreach (array_unique($changed) as $header) {
158: $this->changedHeader(strtolower($header));
159: }
160:
161: return $this;
162: }
163:
164: 165: 166:
167: public function ($header)
168: {
169: return array_key_exists(strtolower($header), $this->headers);
170: }
171:
172: 173: 174:
175: public function ($header)
176: {
177: $header = strtolower($header);
178: unset($this->headers[$header]);
179: $this->changedHeader($header);
180:
181: return $this;
182: }
183:
184: 185: 186:
187: public function ($header, $token = ';')
188: {
189: if (!$this->hasHeader($header)) {
190: return null;
191: }
192:
193: $data = new Collection();
194:
195: foreach ($this->getHeader($header) as $singleValue) {
196: foreach (explode($token, $singleValue) as $kvp) {
197: $parts = explode('=', $kvp, 2);
198: if (!isset($parts[1])) {
199: $data[count($data)] = trim($parts[0]);
200: } else {
201: $data->add(trim($parts[0]), trim($parts[1]));
202: }
203: }
204: }
205:
206: foreach ($data as $key => $value) {
207: if (is_array($value)) {
208: $data->set($key, array_unique($value));
209: }
210: }
211:
212: return $data;
213: }
214:
215: 216: 217:
218: public function ($header, $data, $token = ';')
219: {
220: if (!($data instanceof Collection) && !is_array($data)) {
221: throw new InvalidArgumentException('Data must be a Collection or array');
222: }
223:
224: $values = array();
225: foreach ($data as $key => $value) {
226: foreach ((array) $value as $v) {
227: $values[] = is_int($key) ? $v : $key . '=' . $v;
228: }
229: }
230:
231: return $this->setHeader($header, implode($token, $values));
232: }
233:
234: 235: 236:
237: public function getCacheControlDirective($directive)
238: {
239: if (!isset($this->cacheControl[$directive])) {
240: return null;
241: }
242:
243: $directive = $this->cacheControl[$directive];
244:
245: if (is_array($directive) && !empty($directive)) {
246: return $directive[0];
247: }
248:
249: return $directive;
250: }
251:
252: 253: 254:
255: public function hasCacheControlDirective($directive)
256: {
257: return isset($this->cacheControl[$directive]);
258: }
259:
260: 261: 262:
263: public function addCacheControlDirective($directive, $value = true)
264: {
265: $this->cacheControl[$directive] = $value;
266: $this->rebuildCacheControlDirective();
267:
268: return $this;
269: }
270:
271: 272: 273:
274: public function removeCacheControlDirective($directive)
275: {
276: if (array_key_exists($directive, $this->cacheControl)) {
277: unset($this->cacheControl[$directive]);
278: $this->rebuildCacheControlDirective();
279: }
280:
281: return $this;
282: }
283:
284: 285: 286: 287: 288: 289:
290: protected function ($header)
291: {
292: if ($header == 'cache-control') {
293: $this->parseCacheControlDirective();
294: }
295: }
296:
297: 298: 299:
300: private function parseCacheControlDirective()
301: {
302: $this->cacheControl = array();
303: $tokenized = $this->getTokenizedHeader('Cache-Control', ',') ?: array();
304: foreach ($tokenized as $key => $value) {
305: if (is_numeric($key)) {
306: $this->cacheControl[$value] = true;
307: } else {
308: $this->cacheControl[$key] = $value;
309: }
310: }
311: }
312:
313: 314: 315:
316: private function rebuildCacheControlDirective()
317: {
318: $cacheControl = array();
319: foreach ($this->cacheControl as $key => $value) {
320: $cacheControl[] = ($value === true) ? $key : ($key . '=' . $value);
321: }
322: $this->headers['cache-control'] = new Header('Cache-Control', $cacheControl, ', ');
323: }
324: }
325: