1<?php
2
3namespace Sabre\HTTP;
4
5/**
6 * This trait contains a bunch of methods, shared by both the RequestDecorator
7 * and the ResponseDecorator.
8 *
9 * Didn't seem needed to create a full class for this, so we're just
10 * implementing it as a trait.
11 *
12 * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
13 * @author Evert Pot (http://evertpot.com/)
14 * @license http://sabre.io/license/ Modified BSD License
15 */
16trait MessageDecoratorTrait {
17
18    /**
19     * The inner request object.
20     *
21     * All method calls will be forwarded here.
22     *
23     * @var MessageInterface
24     */
25    protected $inner;
26
27    /**
28     * Returns the body as a readable stream resource.
29     *
30     * Note that the stream may not be rewindable, and therefore may only be
31     * read once.
32     *
33     * @return resource
34     */
35    function getBodyAsStream() {
36
37        return $this->inner->getBodyAsStream();
38
39    }
40
41    /**
42     * Returns the body as a string.
43     *
44     * Note that because the underlying data may be based on a stream, this
45     * method could only work correctly the first time.
46     *
47     * @return string
48     */
49    function getBodyAsString() {
50
51        return $this->inner->getBodyAsString();
52
53    }
54
55    /**
56     * Returns the message body, as it's internal representation.
57     *
58     * This could be either a string or a stream.
59     *
60     * @return resource|string
61     */
62    function getBody() {
63
64        return $this->inner->getBody();
65
66    }
67
68    /**
69     * Updates the body resource with a new stream.
70     *
71     * @param resource $body
72     * @return void
73     */
74    function setBody($body) {
75
76        $this->inner->setBody($body);
77
78    }
79
80    /**
81     * Returns all the HTTP headers as an array.
82     *
83     * Every header is returned as an array, with one or more values.
84     *
85     * @return array
86     */
87    function getHeaders() {
88
89        return $this->inner->getHeaders();
90
91    }
92
93    /**
94     * Will return true or false, depending on if a HTTP header exists.
95     *
96     * @param string $name
97     * @return bool
98     */
99    function hasHeader($name) {
100
101        return $this->inner->hasHeader($name);
102
103    }
104
105    /**
106     * Returns a specific HTTP header, based on it's name.
107     *
108     * The name must be treated as case-insensitive.
109     * If the header does not exist, this method must return null.
110     *
111     * If a header appeared more than once in a HTTP request, this method will
112     * concatenate all the values with a comma.
113     *
114     * Note that this not make sense for all headers. Some, such as
115     * `Set-Cookie` cannot be logically combined with a comma. In those cases
116     * you *should* use getHeaderAsArray().
117     *
118     * @param string $name
119     * @return string|null
120     */
121    function getHeader($name) {
122
123        return $this->inner->getHeader($name);
124
125    }
126
127    /**
128     * Returns a HTTP header as an array.
129     *
130     * For every time the HTTP header appeared in the request or response, an
131     * item will appear in the array.
132     *
133     * If the header did not exists, this method will return an empty array.
134     *
135     * @param string $name
136     * @return string[]
137     */
138    function getHeaderAsArray($name) {
139
140        return $this->inner->getHeaderAsArray($name);
141
142    }
143
144    /**
145     * Updates a HTTP header.
146     *
147     * The case-sensitivity of the name value must be retained as-is.
148     *
149     * If the header already existed, it will be overwritten.
150     *
151     * @param string $name
152     * @param string|string[] $value
153     * @return void
154     */
155    function setHeader($name, $value) {
156
157        $this->inner->setHeader($name, $value);
158
159    }
160
161    /**
162     * Sets a new set of HTTP headers.
163     *
164     * The headers array should contain headernames for keys, and their value
165     * should be specified as either a string or an array.
166     *
167     * Any header that already existed will be overwritten.
168     *
169     * @param array $headers
170     * @return void
171     */
172    function setHeaders(array $headers) {
173
174        $this->inner->setHeaders($headers);
175
176    }
177
178    /**
179     * Adds a HTTP header.
180     *
181     * This method will not overwrite any existing HTTP header, but instead add
182     * another value. Individual values can be retrieved with
183     * getHeadersAsArray.
184     *
185     * @param string $name
186     * @param string $value
187     * @return void
188     */
189    function addHeader($name, $value) {
190
191        $this->inner->addHeader($name, $value);
192
193    }
194
195    /**
196     * Adds a new set of HTTP headers.
197     *
198     * Any existing headers will not be overwritten.
199     *
200     * @param array $headers
201     * @return void
202     */
203    function addHeaders(array $headers) {
204
205        $this->inner->addHeaders($headers);
206
207    }
208
209
210    /**
211     * Removes a HTTP header.
212     *
213     * The specified header name must be treated as case-insensitive.
214     * This method should return true if the header was successfully deleted,
215     * and false if the header did not exist.
216     *
217     * @param string $name
218     * @return bool
219     */
220    function removeHeader($name) {
221
222        return $this->inner->removeHeader($name);
223
224    }
225
226    /**
227     * Sets the HTTP version.
228     *
229     * Should be 1.0 or 1.1.
230     *
231     * @param string $version
232     * @return void
233     */
234    function setHttpVersion($version) {
235
236        $this->inner->setHttpVersion($version);
237
238    }
239
240    /**
241     * Returns the HTTP version.
242     *
243     * @return string
244     */
245    function getHttpVersion() {
246
247        return $this->inner->getHttpVersion();
248
249    }
250
251}
252