]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Http/Response.php
Added Phergie PHP IRC library
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Http / Response.php
1 <?php
2 /**
3  * Phergie 
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie 
15  * @package   Phergie_Plugin_Http
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie_Plugin_Http
20  */
21
22 /**
23  * Data structure for HTTP response information. 
24  *
25  * @category Phergie 
26  * @package  Phergie_Plugin_Http
27  * @author   Phergie Development Team <team@phergie.org>
28  * @license  http://phergie.org/license New BSD License
29  * @link     http://pear.phergie.org/package/Phergie_Plugin_Http
30  */
31 class Phergie_Plugin_Http_Response
32 {
33     /**
34      * HTTP response code or 0 if no HTTP response was received
35      *
36      * @var string 
37      */
38     protected $code;
39
40     /**
41      * Description of the HTTP response code or the error message if no HTTP 
42      * response was received
43      *
44      * @var string
45      */
46     protected $message;
47
48     /**
49      * Content of the response body, decoded for supported content types
50      *
51      * @var mixed
52      */
53     protected $content;
54
55     /**
56      * Associative array mapping response header names to their values
57      *
58      * @var array
59      */
60     protected $headers;
61
62     /**
63      * Associative array containing other metadata about the response
64      *
65      * @var array
66      */
67     protected $meta;
68
69     /**
70      * Sets the HTTP response code.
71      *
72      * @param string $code Response code
73      *
74      * @return Phergie_Plugin_Http_Response Provides a fluent interface
75      */
76     public function setCode($code)
77     {
78         $this->code = $code;
79         return $this;
80     }
81
82     /**
83      * Returns the HTTP response code.
84      *
85      * @return string Response code
86      */
87     public function getCode()
88     {
89         return $this->code;
90     }
91
92     /**
93      * Returns whether the response indicates a client- or server-side error.
94      *
95      * @return bool TRUE if the response indicates an error, FALSE otherwise
96      */
97     public function isError()
98     {
99         switch (substr($this->code, 0, 1)) {
100         case '0':
101         case '4':
102         case '5':
103             return true;
104         default:
105             return false;
106         }
107     }
108
109     /**
110      * Sets the HTTP response description.
111      *
112      * @param string $message Response description
113      *
114      * @return Phergie_Plugin_Http_Response Provides a fluent interface
115      */
116     public function setMessage($message)
117     {
118         $this->message = $message;
119         return $this;
120     }
121
122     /**
123      * Returns the HTTP response description.
124      *
125      * @return string
126      */
127     public function getMessage()
128     {
129         return $this->message;
130     }
131
132     /**
133      * Sets the content of the response body.
134      *
135      * @param mixed $content Response body content
136      *
137      * @return Phergie_Plugin_Http_Response Provides a fluent interface
138      */
139     public function setContent($content)
140     {
141         $this->content = $content;
142         return $this;
143     }
144
145     /**
146      * Returns the content of the response body.
147      *
148      * @return mixed Response body content, decoded for supported content 
149      *         types
150      */
151     public function getContent()
152     {
153         return $this->content;
154     }
155
156     /**
157      * Sets the response headers.
158      *
159      * @param array $headers Associative array of response headers indexed 
160      *        by header name
161      *
162      * @return Phergie_Plugin_Http_Response Provides a fluent interface
163      */
164     public function setHeaders(array $headers)
165     {
166         $names = array_map('strtolower', array_keys($headers));
167         $values = array_values($headers);
168         $this->headers = array_combine($names, $values);
169         return $this;
170     }
171
172     /**
173      * Returns all response headers or the value of a single specified 
174      * response header.
175      *
176      * @param string $name Optional name of a single header for which the 
177      *        associated value should be returned
178      *
179      * @return array|string Associative array of all header values, a string  
180      *         containing the value of the header indicated by $name if one 
181      *         is set, or null if one is not
182      */
183     public function getHeaders($name = null)
184     {
185         if ($name) {
186             $name = strtolower($name);
187             if (empty($this->headers[$name])) {
188                 return null;
189             }
190             return $this->headers[$name];
191         }
192         return $this->headers;
193     }
194
195     /**
196      * Sets the response metadata.
197      *
198      * @param array $meta Associative array of response metadata
199      *
200      * @return Phergie_Plugin_Http_Response Provides a fluent interface
201      */
202     public function setMeta(array $meta)
203     {
204         $this->meta = $meta;
205         return $this;
206     }
207
208     /**
209      * Returns all metadata or the value of a single specified metadatum.
210      *
211      * @param string $name Optional name of a single metadatum for which the 
212      *        associated value should be returned
213      * 
214      * @return array|string|null Associative array of all metadata values, a  
215      *         string containing the value of the metadatum indicated by 
216      *         $name if one is set, or null if one is not
217      */
218     public function getMeta($name = null)
219     {
220         if ($name) {
221             if (empty($this->meta[$name])) {
222                 return null;
223             }
224             return $this->meta[$name];
225         }
226         return $this->meta;
227     }
228 }