PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/solarium/solarium/library/Solarium/Core/Client/Request.php

https://gitlab.com/Blueprint-Marketing/solr-power
PHP | 488 lines | 203 code | 54 blank | 231 comment | 16 complexity | 4217a41631a9afd303b370c659a35c86 MD5 | raw file
  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. /**
  36. * @namespace
  37. */
  38. namespace Solarium\Core\Client;
  39. use Solarium\Core\Configurable;
  40. use Solarium\Exception\RuntimeException;
  41. /**
  42. * Class for describing a request
  43. */
  44. class Request extends Configurable
  45. {
  46. /**
  47. * Request GET method
  48. */
  49. const METHOD_GET = 'GET';
  50. /**
  51. * Request POST method
  52. */
  53. const METHOD_POST = 'POST';
  54. /**
  55. * Request HEAD method
  56. */
  57. const METHOD_HEAD = 'HEAD';
  58. /**
  59. * Default options
  60. *
  61. * @var array
  62. */
  63. protected $options = array(
  64. 'method' => self::METHOD_GET,
  65. );
  66. /**
  67. * Request headers
  68. */
  69. protected $headers = array();
  70. /**
  71. * Request params
  72. *
  73. * Multivalue params are supported using a multidimensional array:
  74. * 'fq' => array('cat:1','published:1')
  75. *
  76. * @var array
  77. */
  78. protected $params = array();
  79. /**
  80. * Raw POST data
  81. *
  82. * @var string
  83. */
  84. protected $rawData;
  85. /**
  86. * Initialization hook
  87. */
  88. protected function init()
  89. {
  90. foreach ($this->options as $name => $value) {
  91. switch ($name) {
  92. case 'rawdata':
  93. $this->setRawData($value);
  94. break;
  95. case 'file':
  96. $this->setFileUpload($value);
  97. break;
  98. case 'param':
  99. $this->setParams($value);
  100. break;
  101. case 'header':
  102. $this->setHeaders($value);
  103. break;
  104. case 'authentication':
  105. if (isset($value['username']) && isset($value['password'])) {
  106. $this->setAuthentication($value['username'], $value['password']);
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * Set request handler
  113. *
  114. * @param string $handler
  115. * @return self Provides fluent interface
  116. */
  117. public function setHandler($handler)
  118. {
  119. $this->setOption('handler', $handler);
  120. return $this;
  121. }
  122. /**
  123. * Get request handler
  124. *
  125. * @return string
  126. */
  127. public function getHandler()
  128. {
  129. return $this->getOption('handler');
  130. }
  131. /**
  132. * Set request method
  133. *
  134. * Use one of the constants as value
  135. *
  136. * @param string $method
  137. * @return self Provides fluent interface
  138. */
  139. public function setMethod($method)
  140. {
  141. $this->setOption('method', $method);
  142. return $this;
  143. }
  144. /**
  145. * Get request method
  146. *
  147. * @return string
  148. */
  149. public function getMethod()
  150. {
  151. return $this->getOption('method');
  152. }
  153. /**
  154. * Get a param value
  155. *
  156. * @param string $key
  157. * @return string|array
  158. */
  159. public function getParam($key)
  160. {
  161. if (isset($this->params[$key])) {
  162. return $this->params[$key];
  163. } else {
  164. return null;
  165. }
  166. }
  167. /**
  168. * Get all params
  169. *
  170. * @return array
  171. */
  172. public function getParams()
  173. {
  174. return $this->params;
  175. }
  176. /**
  177. * Set request params
  178. *
  179. * @param array $params
  180. * @return self Provides fluent interface
  181. */
  182. public function setParams($params)
  183. {
  184. $this->clearParams();
  185. $this->addParams($params);
  186. return $this;
  187. }
  188. /**
  189. * Add a request param
  190. *
  191. * If you add a request param that already exists the param will be converted into a multivalue param,
  192. * unless you set the overwrite param to true.
  193. *
  194. * Empty params are not added to the request. If you want to empty a param disable it you should use
  195. * remove param instead.
  196. *
  197. * @param string $key
  198. * @param string|array $value
  199. * @param boolean $overwrite
  200. * @return self Provides fluent interface
  201. */
  202. public function addParam($key, $value, $overwrite = false)
  203. {
  204. if ($value !== null) {
  205. if (!$overwrite && isset($this->params[$key])) {
  206. if (!is_array($this->params[$key])) {
  207. $this->params[$key] = array($this->params[$key]);
  208. }
  209. $this->params[$key][] = $value;
  210. } else {
  211. // not all solr handlers support 0/1 as boolean values...
  212. if ($value === true) {
  213. $value = 'true';
  214. } elseif ($value === false) {
  215. $value = 'false';
  216. }
  217. $this->params[$key] = $value;
  218. }
  219. }
  220. return $this;
  221. }
  222. /**
  223. * Add multiple params to the request
  224. *
  225. * @param array $params
  226. * @param boolean $overwrite
  227. * @return self Provides fluent interface
  228. */
  229. public function addParams($params, $overwrite = false)
  230. {
  231. foreach ($params as $key => $value) {
  232. $this->addParam($key, $value, $overwrite);
  233. }
  234. return $this;
  235. }
  236. /**
  237. * Remove a param by key
  238. *
  239. * @param string $key
  240. * @return self Provides fluent interface
  241. */
  242. public function removeParam($key)
  243. {
  244. if (isset($this->params[$key])) {
  245. unset($this->params[$key]);
  246. }
  247. return $this;
  248. }
  249. /**
  250. * Clear all request params
  251. *
  252. * @return self Provides fluent interface
  253. */
  254. public function clearParams()
  255. {
  256. $this->params = array();
  257. return $this;
  258. }
  259. /**
  260. * Get raw POST data
  261. *
  262. * @return null
  263. */
  264. public function getRawData()
  265. {
  266. return $this->rawData;
  267. }
  268. /**
  269. * Set raw POST data
  270. *
  271. * This string must be safely encoded.
  272. *
  273. * @param string $data
  274. * @return self Provides fluent interface
  275. */
  276. public function setRawData($data)
  277. {
  278. $this->rawData = $data;
  279. return $this;
  280. }
  281. /**
  282. * Get the file to upload via "multipart/form-data" POST request
  283. *
  284. * @return string|null
  285. */
  286. public function getFileUpload()
  287. {
  288. return $this->getOption('file');
  289. }
  290. /**
  291. * Set the file to upload via "multipart/form-data" POST request
  292. *
  293. * @throws RuntimeException
  294. * @param string $filename Name of file to upload
  295. * @return self
  296. */
  297. public function setFileUpload($filename)
  298. {
  299. if (!is_file($filename) || !is_readable($filename)) {
  300. throw new RuntimeException("Unable to read file '{$filename}' for upload");
  301. }
  302. $this->setOption('file', $filename);
  303. return $this;
  304. }
  305. /**
  306. * Get all request headers
  307. *
  308. * @return array
  309. */
  310. public function getHeaders()
  311. {
  312. return $this->headers;
  313. }
  314. /**
  315. * Set request headers
  316. *
  317. * @param array $headers
  318. * @return self Provides fluent interface
  319. */
  320. public function setHeaders($headers)
  321. {
  322. $this->clearHeaders();
  323. $this->addHeaders($headers);
  324. return $this;
  325. }
  326. /**
  327. * Add a request header
  328. *
  329. * @param string|array $value
  330. * @return self Provides fluent interface
  331. */
  332. public function addHeader($value)
  333. {
  334. $this->headers[] = $value;
  335. return $this;
  336. }
  337. /**
  338. * Add multiple headers to the request
  339. *
  340. * @param array $headers
  341. * @return self Provides fluent interface
  342. */
  343. public function addHeaders($headers)
  344. {
  345. foreach ($headers as $header) {
  346. $this->addHeader($header);
  347. }
  348. return $this;
  349. }
  350. /**
  351. * Clear all request headers
  352. *
  353. * @return self Provides fluent interface
  354. */
  355. public function clearHeaders()
  356. {
  357. $this->headers = array();
  358. return $this;
  359. }
  360. /**
  361. * Get an URI for this request
  362. *
  363. * @return string
  364. */
  365. public function getUri()
  366. {
  367. return $this->getHandler() . '?' . $this->getQueryString();
  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. return $queryString;
  386. }
  387. /**
  388. * Magic method enables a object to be transformed to a string
  389. *
  390. * Get a summary showing significant variables in the object
  391. * note: uri resource is decoded for readability
  392. *
  393. * @return string
  394. */
  395. public function __toString()
  396. {
  397. $output = __CLASS__ . '::__toString' . "\n"
  398. . 'method: ' . $this->getMethod() . "\n"
  399. . 'header: ' . print_r($this->getHeaders(), 1) //don't add newline when using print_r
  400. . 'authentication: ' . print_r($this->getAuthentication(), 1)
  401. . 'resource: ' . $this->getUri() . "\n"
  402. . 'resource urldecoded: ' . urldecode($this->getUri()) . "\n"
  403. . 'raw data: ' . $this->getRawData() . "\n"
  404. . 'file upload: ' . $this->getFileUpload() . "\n";
  405. return $output;
  406. }
  407. /**
  408. * Set HTTP basic auth settings
  409. *
  410. * If one or both values are NULL authentication will be disabled
  411. *
  412. * @param string $username
  413. * @param string $password
  414. * @return self Provides fluent interface
  415. */
  416. public function setAuthentication($username, $password)
  417. {
  418. $this->setOption('username', $username);
  419. $this->setOption('password', $password);
  420. return $this;
  421. }
  422. /**
  423. * Get HTTP basic auth settings
  424. *
  425. * @return array
  426. */
  427. public function getAuthentication()
  428. {
  429. return array(
  430. 'username' => $this->getOption('username'),
  431. 'password' => $this->getOption('password'),
  432. );
  433. }
  434. }