[7588] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * Copyright 2011 Bas de Nooijer. All rights reserved. |
---|
| 4 | * |
---|
| 5 | * Redistribution and use in source and binary forms, with or without |
---|
| 6 | * modification, are permitted provided that the following conditions are met: |
---|
| 7 | * |
---|
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
---|
| 9 | * this list of conditions and the following disclaimer. |
---|
| 10 | * |
---|
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
---|
| 12 | * this listof conditions and the following disclaimer in the documentation |
---|
| 13 | * and/or other materials provided with the distribution. |
---|
| 14 | * |
---|
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" |
---|
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
---|
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
| 26 | * |
---|
| 27 | * The views and conclusions contained in the software and documentation are |
---|
| 28 | * those of the authors and should not be interpreted as representing official |
---|
| 29 | * policies, either expressed or implied, of the copyright holder. |
---|
| 30 | * |
---|
| 31 | * @copyright Copyright 2011 Bas de Nooijer <solarium@raspberry.nl> |
---|
| 32 | * @license http://github.com/basdenooijer/solarium/raw/master/COPYING |
---|
| 33 | * @link http://www.solarium-project.org/ |
---|
| 34 | * |
---|
| 35 | * @package Solarium |
---|
| 36 | * @subpackage Client |
---|
| 37 | */ |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * Class for describing a request |
---|
| 41 | * |
---|
| 42 | * @package Solarium |
---|
| 43 | * @subpackage Client |
---|
| 44 | */ |
---|
| 45 | class Solarium_Client_Request extends Solarium_Configurable |
---|
| 46 | { |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Request GET method |
---|
| 50 | */ |
---|
| 51 | const METHOD_GET = 'GET'; |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Request POST method |
---|
| 55 | */ |
---|
| 56 | const METHOD_POST = 'POST'; |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Request HEAD method |
---|
| 60 | */ |
---|
| 61 | const METHOD_HEAD = 'HEAD'; |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * Default options |
---|
| 65 | * |
---|
| 66 | * @var array |
---|
| 67 | */ |
---|
| 68 | protected $_options = array( |
---|
| 69 | 'method' => self::METHOD_GET, |
---|
| 70 | ); |
---|
| 71 | |
---|
| 72 | /** |
---|
| 73 | * Request headers |
---|
| 74 | */ |
---|
| 75 | protected $_headers = array(); |
---|
| 76 | |
---|
| 77 | /** |
---|
| 78 | * Request params |
---|
| 79 | * |
---|
| 80 | * Multivalue params are supported using a multidimensional array: |
---|
| 81 | * 'fq' => array('cat:1','published:1') |
---|
| 82 | * |
---|
| 83 | * @var array |
---|
| 84 | */ |
---|
| 85 | protected $_params = array(); |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Raw POST data |
---|
| 89 | * |
---|
| 90 | * @var string |
---|
| 91 | */ |
---|
| 92 | protected $_rawData; |
---|
| 93 | |
---|
| 94 | /** |
---|
| 95 | * Initialization hook |
---|
| 96 | */ |
---|
| 97 | protected function _init() |
---|
| 98 | { |
---|
| 99 | foreach ($this->_options AS $name => $value) { |
---|
| 100 | switch ($name) { |
---|
| 101 | case 'rawdata': |
---|
| 102 | $this->setRawData($value); |
---|
| 103 | break; |
---|
| 104 | case 'param': |
---|
| 105 | $this->setParams($value); |
---|
| 106 | break; |
---|
| 107 | case 'header': |
---|
| 108 | $this->setHeaders($value); |
---|
| 109 | break; |
---|
| 110 | } |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /** |
---|
| 115 | * Set request handler |
---|
| 116 | * |
---|
| 117 | * @param string $handler |
---|
| 118 | * @return Solarium_Client_Request |
---|
| 119 | */ |
---|
| 120 | public function setHandler($handler) |
---|
| 121 | { |
---|
| 122 | $this->_setOption('handler', $handler); |
---|
| 123 | return $this; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * Get request handler |
---|
| 128 | * |
---|
| 129 | * @return string |
---|
| 130 | */ |
---|
| 131 | public function getHandler() |
---|
| 132 | { |
---|
| 133 | return $this->getOption('handler'); |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * Set request method |
---|
| 138 | * |
---|
| 139 | * Use one of the constants as value |
---|
| 140 | * |
---|
| 141 | * @param string $method |
---|
| 142 | * @return Solarium_Client_Request |
---|
| 143 | */ |
---|
| 144 | public function setMethod($method) |
---|
| 145 | { |
---|
| 146 | $this->_setOption('method', $method); |
---|
| 147 | return $this; |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /** |
---|
| 151 | * Get request method |
---|
| 152 | * |
---|
| 153 | * @return string |
---|
| 154 | */ |
---|
| 155 | public function getMethod() |
---|
| 156 | { |
---|
| 157 | return $this->getOption('method'); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * Get a param value |
---|
| 162 | * |
---|
| 163 | * @param string $key |
---|
| 164 | * @return string|array |
---|
| 165 | */ |
---|
| 166 | public function getParam($key) |
---|
| 167 | { |
---|
| 168 | if (isset($this->_params[$key])) { |
---|
| 169 | return $this->_params[$key]; |
---|
| 170 | } else { |
---|
| 171 | return null; |
---|
| 172 | } |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * Get all params |
---|
| 177 | * |
---|
| 178 | * @return array |
---|
| 179 | */ |
---|
| 180 | public function getParams() |
---|
| 181 | { |
---|
| 182 | return $this->_params; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | /** |
---|
| 186 | * Set request params |
---|
| 187 | * |
---|
| 188 | * @param array $params |
---|
| 189 | * @return Solarium_Client_Request |
---|
| 190 | */ |
---|
| 191 | public function setParams($params) |
---|
| 192 | { |
---|
| 193 | $this->clearParams(); |
---|
| 194 | $this->addParams($params); |
---|
| 195 | return $this; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | /** |
---|
| 199 | * Add a request param |
---|
| 200 | * |
---|
| 201 | * If you add a request param that already exists the param will be converted into a multivalue param, |
---|
| 202 | * unless you set the overwrite param to true. |
---|
| 203 | * |
---|
| 204 | * Empty params are not added to the request. If you want to empty a param disable it you should use |
---|
| 205 | * remove param instead. |
---|
| 206 | * |
---|
| 207 | * @param string $key |
---|
| 208 | * @param string|array $value |
---|
| 209 | * @param boolean $overwrite |
---|
| 210 | * @return Solarium_Client_Request |
---|
| 211 | */ |
---|
| 212 | public function addParam($key, $value, $overwrite = false) |
---|
| 213 | { |
---|
| 214 | if ($value !== null) { |
---|
| 215 | if (!$overwrite && isset($this->_params[$key])) { |
---|
| 216 | if (!is_array($this->_params[$key])) { |
---|
| 217 | $this->_params[$key] = array($this->_params[$key]); |
---|
| 218 | } |
---|
| 219 | $this->_params[$key][] = $value; |
---|
| 220 | } else { |
---|
| 221 | // not all solr handlers support 0/1 as boolean values... |
---|
| 222 | if($value === true) $value = 'true'; |
---|
| 223 | if($value === false) $value = 'false'; |
---|
| 224 | |
---|
| 225 | $this->_params[$key] = $value; |
---|
| 226 | } |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | return $this; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | /** |
---|
| 233 | * Add multiple params to the request |
---|
| 234 | * |
---|
| 235 | * @param array $params |
---|
| 236 | * @param boolean $overwrite |
---|
| 237 | * @return Solarium_Client_Request |
---|
| 238 | */ |
---|
| 239 | public function addParams($params, $overwrite = false) |
---|
| 240 | { |
---|
| 241 | foreach ($params as $key => $value) { |
---|
| 242 | $this->addParam($key, $value, $overwrite); |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | return $this; |
---|
| 246 | } |
---|
| 247 | |
---|
| 248 | /** |
---|
| 249 | * Remove a param by key |
---|
| 250 | * |
---|
| 251 | * @param string $key |
---|
| 252 | * @return Solarium_Client_Request |
---|
| 253 | */ |
---|
| 254 | public function removeParam($key) |
---|
| 255 | { |
---|
| 256 | if (isset($this->_params[$key])) { |
---|
| 257 | unset($this->_params[$key]); |
---|
| 258 | } |
---|
| 259 | return $this; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * Clear all request params |
---|
| 264 | * |
---|
| 265 | * @return Solarium_Client_Request |
---|
| 266 | */ |
---|
| 267 | public function clearParams() |
---|
| 268 | { |
---|
| 269 | $this->_params = array(); |
---|
| 270 | return $this; |
---|
| 271 | } |
---|
| 272 | |
---|
| 273 | /** |
---|
| 274 | * Get raw POST data |
---|
| 275 | * |
---|
| 276 | * @return null |
---|
| 277 | */ |
---|
| 278 | public function getRawData() |
---|
| 279 | { |
---|
| 280 | return $this->_rawData; |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | /** |
---|
| 284 | * Set raw POST data |
---|
| 285 | * |
---|
| 286 | * This string must be safely encoded. |
---|
| 287 | * |
---|
| 288 | * @param string $data |
---|
| 289 | * @return Solarium_Client_Request |
---|
| 290 | */ |
---|
| 291 | public function setRawData($data) |
---|
| 292 | { |
---|
| 293 | $this->_rawData = $data; |
---|
| 294 | return $this; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /** |
---|
| 298 | * Get all request headers |
---|
| 299 | * |
---|
| 300 | * @return array |
---|
| 301 | */ |
---|
| 302 | public function getHeaders() |
---|
| 303 | { |
---|
| 304 | return $this->_headers; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | * Set request headers |
---|
| 309 | * |
---|
| 310 | * @param array $headers |
---|
| 311 | * @return Solarium_Client_Request |
---|
| 312 | */ |
---|
| 313 | public function setHeaders($headers) |
---|
| 314 | { |
---|
| 315 | $this->clearHeaders(); |
---|
| 316 | $this->addHeaders($headers); |
---|
| 317 | return $this; |
---|
| 318 | } |
---|
| 319 | |
---|
| 320 | /** |
---|
| 321 | * Add a request header |
---|
| 322 | * |
---|
| 323 | * @param string|array $value |
---|
| 324 | * @return Solarium_Client_Request |
---|
| 325 | */ |
---|
| 326 | public function addHeader($value) |
---|
| 327 | { |
---|
| 328 | $this->_headers[] = $value; |
---|
| 329 | |
---|
| 330 | return $this; |
---|
| 331 | } |
---|
| 332 | |
---|
| 333 | /** |
---|
| 334 | * Add multiple headers to the request |
---|
| 335 | * |
---|
| 336 | * @param array $headers |
---|
| 337 | * @return Solarium_Client_Request |
---|
| 338 | */ |
---|
| 339 | public function addHeaders($headers) |
---|
| 340 | { |
---|
| 341 | foreach ($headers as $header) { |
---|
| 342 | $this->addHeader($header); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | return $this; |
---|
| 346 | } |
---|
| 347 | |
---|
| 348 | /** |
---|
| 349 | * Clear all request headers |
---|
| 350 | * |
---|
| 351 | * @return Solarium_Client_Request |
---|
| 352 | */ |
---|
| 353 | public function clearHeaders() |
---|
| 354 | { |
---|
| 355 | $this->_headers = array(); |
---|
| 356 | return $this; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | /** |
---|
| 360 | * Get an URI for this request |
---|
| 361 | * |
---|
| 362 | * @return string |
---|
| 363 | */ |
---|
| 364 | public function getUri() |
---|
| 365 | { |
---|
| 366 | return $this->getHandler() . '?' . $this->getQueryString(); |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | /** |
---|
| 370 | * Get the query string for this request |
---|
| 371 | * |
---|
| 372 | * @return string |
---|
| 373 | */ |
---|
| 374 | public function getQueryString() |
---|
| 375 | { |
---|
| 376 | $queryString = ''; |
---|
| 377 | if (count($this->_params) > 0) { |
---|
| 378 | $queryString = http_build_query($this->_params, null, '&'); |
---|
| 379 | $queryString = preg_replace( |
---|
| 380 | '/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', |
---|
| 381 | '=', |
---|
| 382 | $queryString |
---|
| 383 | ); |
---|
| 384 | } |
---|
| 385 | |
---|
| 386 | return $queryString; |
---|
| 387 | } |
---|
| 388 | |
---|
| 389 | /** |
---|
| 390 | * Magic method enables a object to be transformed to a string |
---|
| 391 | * |
---|
| 392 | * Get a summary showing significant variables in the object |
---|
| 393 | * note: uri resource is decoded for readability |
---|
| 394 | * |
---|
| 395 | * @return string |
---|
| 396 | */ |
---|
| 397 | public function __toString() |
---|
| 398 | { |
---|
| 399 | $output = __CLASS__ . '::toString' . "\n" |
---|
| 400 | . 'method: ' . $this->getMethod() . "\n" |
---|
| 401 | . 'header: ' . print_r($this->getHeaders(), 1) //don't add newline when using print_r |
---|
| 402 | . 'resource: ' . $this->getUri() . "\n" |
---|
| 403 | . 'resource urldecoded: ' . urldecode($this->getUri()) . "\n" |
---|
| 404 | . 'raw data: ' . $this->getRawData() . "\n"; |
---|
| 405 | |
---|
| 406 | return $output; |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | } |
---|