Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
namespace Guzzle\Http\Message;
4
 
5
use Guzzle\Common\Version;
6
use Guzzle\Common\Collection;
7
use Guzzle\Http\Message\Header\HeaderCollection;
8
use Guzzle\Http\Message\Header\HeaderFactory;
9
use Guzzle\Http\Message\Header\HeaderFactoryInterface;
10
use Guzzle\Http\Message\Header\HeaderInterface;
11
 
12
/**
13
 * Abstract HTTP request/response message
14
 */
15
abstract class AbstractMessage implements MessageInterface
16
{
17
    /** @var array HTTP header collection */
18
    protected $headers;
19
 
20
    /** @var HeaderFactoryInterface $headerFactory */
21
    protected $headerFactory;
22
 
23
    /** @var Collection Custom message parameters that are extendable by plugins */
24
    protected $params;
25
 
26
    /** @var string Message protocol */
27
    protected $protocol = 'HTTP';
28
 
29
    /** @var string HTTP protocol version of the message */
30
    protected $protocolVersion = '1.1';
31
 
32
    public function __construct()
33
    {
34
        $this->params = new Collection();
35
        $this->headerFactory = new HeaderFactory();
36
        $this->headers = new HeaderCollection();
37
    }
38
 
39
    /**
40
     * Set the header factory to use to create headers
41
     *
42
     * @param HeaderFactoryInterface $factory
43
     *
44
     * @return self
45
     */
46
    public function setHeaderFactory(HeaderFactoryInterface $factory)
47
    {
48
        $this->headerFactory = $factory;
49
 
50
        return $this;
51
    }
52
 
53
    public function getParams()
54
    {
55
        return $this->params;
56
    }
57
 
58
    public function addHeader($header, $value)
59
    {
60
        if (isset($this->headers[$header])) {
61
            $this->headers[$header]->add($value);
62
        } elseif ($value instanceof HeaderInterface) {
63
            $this->headers[$header] = $value;
64
        } else {
65
            $this->headers[$header] = $this->headerFactory->createHeader($header, $value);
66
        }
67
 
68
        return $this;
69
    }
70
 
71
    public function addHeaders(array $headers)
72
    {
73
        foreach ($headers as $key => $value) {
74
            $this->addHeader($key, $value);
75
        }
76
 
77
        return $this;
78
    }
79
 
80
    public function getHeader($header)
81
    {
82
        return $this->headers[$header];
83
    }
84
 
85
    public function getHeaders()
86
    {
87
        return $this->headers;
88
    }
89
 
90
    public function getHeaderLines()
91
    {
92
        $headers = array();
93
        foreach ($this->headers as $value) {
94
            $headers[] = $value->getName() . ': ' . $value;
95
        }
96
 
97
        return $headers;
98
    }
99
 
100
    public function setHeader($header, $value)
101
    {
102
        unset($this->headers[$header]);
103
        $this->addHeader($header, $value);
104
 
105
        return $this;
106
    }
107
 
108
    public function setHeaders(array $headers)
109
    {
110
        $this->headers->clear();
111
        foreach ($headers as $key => $value) {
112
            $this->addHeader($key, $value);
113
        }
114
 
115
        return $this;
116
    }
117
 
118
    public function hasHeader($header)
119
    {
120
        return isset($this->headers[$header]);
121
    }
122
 
123
    public function removeHeader($header)
124
    {
125
        unset($this->headers[$header]);
126
 
127
        return $this;
128
    }
129
 
130
    /**
131
     * @deprecated Use $message->getHeader()->parseParams()
132
     * @codeCoverageIgnore
133
     */
134
    public function getTokenizedHeader($header, $token = ';')
135
    {
136
        Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader()->parseParams()');
137
        if ($this->hasHeader($header)) {
138
            $data = new Collection();
139
            foreach ($this->getHeader($header)->parseParams() as $values) {
140
                foreach ($values as $key => $value) {
141
                    if ($value === '') {
142
                        $data->set($data->count(), $key);
143
                    } else {
144
                        $data->add($key, $value);
145
                    }
146
                }
147
            }
148
            return $data;
149
        }
150
    }
151
 
152
    /**
153
     * @deprecated
154
     * @codeCoverageIgnore
155
     */
156
    public function setTokenizedHeader($header, $data, $token = ';')
157
    {
158
        Version::warn(__METHOD__ . ' is deprecated.');
159
        return $this;
160
    }
161
 
162
    /**
163
     * @deprecated
164
     * @codeCoverageIgnore
165
     */
166
    public function getCacheControlDirective($directive)
167
    {
168
        Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader(\'Cache-Control\')->getDirective()');
169
        if (!($header = $this->getHeader('Cache-Control'))) {
170
            return null;
171
        }
172
 
173
        return $header->getDirective($directive);
174
    }
175
 
176
    /**
177
     * @deprecated
178
     * @codeCoverageIgnore
179
     */
180
    public function hasCacheControlDirective($directive)
181
    {
182
        Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader(\'Cache-Control\')->hasDirective()');
183
        if ($header = $this->getHeader('Cache-Control')) {
184
            return $header->hasDirective($directive);
185
        } else {
186
            return false;
187
        }
188
    }
189
 
190
    /**
191
     * @deprecated
192
     * @codeCoverageIgnore
193
     */
194
    public function addCacheControlDirective($directive, $value = true)
195
    {
196
        Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader(\'Cache-Control\')->addDirective()');
197
        if (!($header = $this->getHeader('Cache-Control'))) {
198
            $this->addHeader('Cache-Control', '');
199
            $header = $this->getHeader('Cache-Control');
200
        }
201
 
202
        $header->addDirective($directive, $value);
203
 
204
        return $this;
205
    }
206
 
207
    /**
208
     * @deprecated
209
     * @codeCoverageIgnore
210
     */
211
    public function removeCacheControlDirective($directive)
212
    {
213
        Version::warn(__METHOD__ . ' is deprecated. Use $message->getHeader(\'Cache-Control\')->removeDirective()');
214
        if ($header = $this->getHeader('Cache-Control')) {
215
            $header->removeDirective($directive);
216
        }
217
 
218
        return $this;
219
    }
220
}