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