Request.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace Elastica;
  3. use Elastica\Exception\InvalidException;
  4. /**
  5. * Elastica Request object.
  6. *
  7. * @author Nicolas Ruflin <spam@ruflin.com>
  8. */
  9. class Request extends Param
  10. {
  11. const HEAD = 'HEAD';
  12. const POST = 'POST';
  13. const PUT = 'PUT';
  14. const GET = 'GET';
  15. const DELETE = 'DELETE';
  16. const DEFAULT_CONTENT_TYPE = 'application/json';
  17. const NDJSON_CONTENT_TYPE = 'application/x-ndjson';
  18. /**
  19. * @var \Elastica\Connection
  20. */
  21. protected $_connection;
  22. /**
  23. * Construct.
  24. *
  25. * @param string $path Request path
  26. * @param string $method OPTIONAL Request method (use const's) (default = self::GET)
  27. * @param array $data OPTIONAL Data array
  28. * @param array $query OPTIONAL Query params
  29. * @param Connection $connection
  30. * @param string $contentType Content-Type sent with this request
  31. *
  32. * @return \Elastica\Request OPTIONAL Connection object
  33. */
  34. public function __construct($path, $method = self::GET, $data = [], array $query = [], Connection $connection = null, $contentType = self::DEFAULT_CONTENT_TYPE)
  35. {
  36. $this->setPath($path);
  37. $this->setMethod($method);
  38. $this->setData($data);
  39. $this->setQuery($query);
  40. if ($connection) {
  41. $this->setConnection($connection);
  42. }
  43. $this->setContentType($contentType);
  44. }
  45. /**
  46. * Sets the request method. Use one of the for consts.
  47. *
  48. * @param string $method Request method
  49. *
  50. * @return $this
  51. */
  52. public function setMethod($method)
  53. {
  54. return $this->setParam('method', $method);
  55. }
  56. /**
  57. * Get request method.
  58. *
  59. * @return string Request method
  60. */
  61. public function getMethod()
  62. {
  63. return $this->getParam('method');
  64. }
  65. /**
  66. * Sets the request data.
  67. *
  68. * @param array $data Request data
  69. *
  70. * @return $this
  71. */
  72. public function setData($data)
  73. {
  74. return $this->setParam('data', $data);
  75. }
  76. /**
  77. * Return request data.
  78. *
  79. * @return array Request data
  80. */
  81. public function getData()
  82. {
  83. return $this->getParam('data');
  84. }
  85. /**
  86. * Sets the request path.
  87. *
  88. * @param string $path Request path
  89. *
  90. * @return $this
  91. */
  92. public function setPath($path)
  93. {
  94. return $this->setParam('path', $path);
  95. }
  96. /**
  97. * Return request path.
  98. *
  99. * @return string Request path
  100. */
  101. public function getPath()
  102. {
  103. return $this->getParam('path');
  104. }
  105. /**
  106. * Return query params.
  107. *
  108. * @return array Query params
  109. */
  110. public function getQuery()
  111. {
  112. return $this->getParam('query');
  113. }
  114. /**
  115. * @param array $query
  116. *
  117. * @return $this
  118. */
  119. public function setQuery(array $query = [])
  120. {
  121. return $this->setParam('query', $query);
  122. }
  123. /**
  124. * @param \Elastica\Connection $connection
  125. *
  126. * @return $this
  127. */
  128. public function setConnection(Connection $connection)
  129. {
  130. $this->_connection = $connection;
  131. return $this;
  132. }
  133. /**
  134. * Return Connection Object.
  135. *
  136. * @throws Exception\InvalidException If no valid connection was setted
  137. *
  138. * @return \Elastica\Connection
  139. */
  140. public function getConnection()
  141. {
  142. if (empty($this->_connection)) {
  143. throw new InvalidException('No valid connection object set');
  144. }
  145. return $this->_connection;
  146. }
  147. /**
  148. * Set the Content-Type of this request.
  149. *
  150. * @param string $contentType
  151. */
  152. public function setContentType($contentType)
  153. {
  154. return $this->setParam('contentType', $contentType);
  155. }
  156. /**
  157. * Get the Content-Type of this request.
  158. */
  159. public function getContentType()
  160. {
  161. return $this->getParam('contentType');
  162. }
  163. /**
  164. * Sends request to server.
  165. *
  166. * @return \Elastica\Response Response object
  167. */
  168. public function send()
  169. {
  170. $transport = $this->getConnection()->getTransportObject();
  171. // Refactor: Not full toArray needed in exec?
  172. return $transport->exec($this, $this->getConnection()->toArray());
  173. }
  174. /**
  175. * @return array
  176. */
  177. public function toArray()
  178. {
  179. $data = $this->getParams();
  180. if ($this->_connection) {
  181. $data['connection'] = $this->_connection->getParams();
  182. }
  183. return $data;
  184. }
  185. /**
  186. * Converts request to curl request format.
  187. *
  188. * @return string
  189. */
  190. public function toString()
  191. {
  192. return JSON::stringify($this->toArray());
  193. }
  194. /**
  195. * @return string
  196. */
  197. public function __toString()
  198. {
  199. return $this->toString();
  200. }
  201. }