vendor/ruflin/elastica/src/Request.php line 178

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