1<?php
2
3namespace Elastica;
4
5/**
6 * Base class for things that can be sent to the update api (Document and
7 * Script).
8 *
9 * @author   Nik Everett <nik9000@gmail.com>
10 */
11class AbstractUpdateAction extends Param
12{
13    /**
14     * @var \Elastica\Document
15     */
16    protected $_upsert;
17
18    /**
19     * Sets the id of the document.
20     *
21     * @param string|int $id
22     *
23     * @return $this
24     */
25    public function setId($id)
26    {
27        return $this->setParam('_id', $id);
28    }
29
30    /**
31     * Returns document id.
32     *
33     * @return string|int Document id
34     */
35    public function getId()
36    {
37        return $this->hasParam('_id') ? $this->getParam('_id') : null;
38    }
39
40    /**
41     * @return bool
42     */
43    public function hasId()
44    {
45        return '' !== (string) $this->getId();
46    }
47
48    /**
49     * Sets the document type name.
50     *
51     * @param Type|string $type Type name
52     *
53     * @return $this
54     */
55    public function setType($type)
56    {
57        if ($type instanceof Type) {
58            $this->setIndex($type->getIndex());
59            $type = $type->getName();
60        }
61
62        return $this->setParam('_type', $type);
63    }
64
65    /**
66     * Return document type name.
67     *
68     * @throws \Elastica\Exception\InvalidException
69     *
70     * @return string Document type name
71     */
72    public function getType()
73    {
74        return $this->getParam('_type');
75    }
76
77    /**
78     * Sets the document index name.
79     *
80     * @param Index|string $index Index name
81     *
82     * @return $this
83     */
84    public function setIndex($index)
85    {
86        if ($index instanceof Index) {
87            $index = $index->getName();
88        }
89
90        return $this->setParam('_index', $index);
91    }
92
93    /**
94     * Get the document index name.
95     *
96     * @throws \Elastica\Exception\InvalidException
97     *
98     * @return string Index name
99     */
100    public function getIndex()
101    {
102        return $this->getParam('_index');
103    }
104
105    /**
106     * Sets the version of a document for use with optimistic concurrency control.
107     *
108     * @param int $version Document version
109     *
110     * @return $this
111     *
112     * @see https://www.elastic.co/blog/versioning
113     */
114    public function setVersion($version)
115    {
116        return $this->setParam('version', (int) $version);
117    }
118
119    /**
120     * Returns document version.
121     *
122     * @return string|int Document version
123     */
124    public function getVersion()
125    {
126        return $this->getParam('version');
127    }
128
129    /**
130     * @return bool
131     */
132    public function hasVersion()
133    {
134        return $this->hasParam('version');
135    }
136
137    /**
138     * Sets the version_type of a document
139     * Default in ES is internal, but you can set to external to use custom versioning.
140     *
141     * @param string $versionType Document version type
142     *
143     * @return $this
144     */
145    public function setVersionType($versionType)
146    {
147        return $this->setParam('version_type', $versionType);
148    }
149
150    /**
151     * Returns document version type.
152     *
153     * @return string|int Document version type
154     */
155    public function getVersionType()
156    {
157        return $this->getParam('version_type');
158    }
159
160    /**
161     * @return bool
162     */
163    public function hasVersionType()
164    {
165        return $this->hasParam('version_type');
166    }
167
168    /**
169     * Set operation type.
170     *
171     * @param string $opType Only accept create
172     *
173     * @return $this
174     */
175    public function setOpType($opType)
176    {
177        return $this->setParam('op_type', $opType);
178    }
179
180    /**
181     * Get operation type.
182     *
183     * @return string
184     */
185    public function getOpType()
186    {
187        return $this->getParam('op_type');
188    }
189
190    /**
191     * @return bool
192     */
193    public function hasOpType()
194    {
195        return $this->hasParam('op_type');
196    }
197
198    /**
199     * Set routing query param.
200     *
201     * @param string $value routing
202     *
203     * @return $this
204     */
205    public function setRouting($value)
206    {
207        return $this->setParam('routing', $value);
208    }
209
210    /**
211     * Get routing parameter.
212     *
213     * @return string
214     */
215    public function getRouting()
216    {
217        return $this->getParam('_routing');
218    }
219
220    /**
221     * @return bool
222     */
223    public function hasRouting()
224    {
225        return $this->hasParam('_routing');
226    }
227
228    /**
229     * @param array|string $fields
230     *
231     * @return $this
232     */
233    public function setFields($fields)
234    {
235        if (\is_array($fields)) {
236            $fields = \implode(',', $fields);
237        }
238
239        return $this->setParam('fields', (string) $fields);
240    }
241
242    /**
243     * @return $this
244     */
245    public function setFieldsSource()
246    {
247        return $this->setFields('_source');
248    }
249
250    /**
251     * @return string
252     */
253    public function getFields()
254    {
255        return $this->getParam('fields');
256    }
257
258    /**
259     * @return bool
260     */
261    public function hasFields()
262    {
263        return $this->hasParam('fields');
264    }
265
266    /**
267     * @param int $num
268     *
269     * @return $this
270     */
271    public function setRetryOnConflict($num)
272    {
273        return $this->setParam('retry_on_conflict', (int) $num);
274    }
275
276    /**
277     * @return int
278     */
279    public function getRetryOnConflict()
280    {
281        return $this->getParam('retry_on_conflict');
282    }
283
284    /**
285     * @return bool
286     */
287    public function hasRetryOnConflict()
288    {
289        return $this->hasParam('_retry_on_conflict');
290    }
291
292    /**
293     * @param bool $refresh
294     *
295     * @return $this
296     */
297    public function setRefresh($refresh = true)
298    {
299        return $this->setParam('refresh', (bool) $refresh ? 'true' : 'false');
300    }
301
302    /**
303     * @return bool
304     */
305    public function getRefresh()
306    {
307        return 'true' === $this->getParam('refresh');
308    }
309
310    /**
311     * @return bool
312     */
313    public function hasRefresh()
314    {
315        return $this->hasParam('refresh');
316    }
317
318    /**
319     * @param string $timeout
320     *
321     * @return $this
322     */
323    public function setTimeout($timeout)
324    {
325        return $this->setParam('timeout', $timeout);
326    }
327
328    /**
329     * @return bool
330     */
331    public function getTimeout()
332    {
333        return $this->getParam('timeout');
334    }
335
336    /**
337     * @return bool
338     */
339    public function hasTimeout()
340    {
341        return $this->hasParam('timeout');
342    }
343
344    /**
345     * @param string $timeout
346     *
347     * @return $this
348     */
349    public function setConsistency($timeout)
350    {
351        return $this->setParam('consistency', $timeout);
352    }
353
354    /**
355     * @return string
356     */
357    public function getConsistency()
358    {
359        return $this->getParam('consistency');
360    }
361
362    /**
363     * @return bool
364     */
365    public function hasConsistency()
366    {
367        return $this->hasParam('consistency');
368    }
369
370    /**
371     * @param string $timeout
372     *
373     * @return $this
374     */
375    public function setReplication($timeout)
376    {
377        return $this->setParam('replication', $timeout);
378    }
379
380    /**
381     * @return string
382     */
383    public function getReplication()
384    {
385        return $this->getParam('replication');
386    }
387
388    /**
389     * @return bool
390     */
391    public function hasReplication()
392    {
393        return $this->hasParam('replication');
394    }
395
396    /**
397     * @param \Elastica\Document|array $data
398     *
399     * @return $this
400     */
401    public function setUpsert($data)
402    {
403        $document = Document::create($data);
404        $this->_upsert = $document;
405
406        return $this;
407    }
408
409    /**
410     * @return \Elastica\Document
411     */
412    public function getUpsert()
413    {
414        return $this->_upsert;
415    }
416
417    /**
418     * @return bool
419     */
420    public function hasUpsert()
421    {
422        return null !== $this->_upsert;
423    }
424
425    /**
426     * @param array $fields if empty array all options will be returned
427     *
428     * @return array
429     */
430    public function getOptions(array $fields = [])
431    {
432        if (!empty($fields)) {
433            return \array_filter(\array_intersect_key($this->getParams(), \array_flip($fields)));
434        }
435
436        return \array_filter($this->getParams());
437    }
438}
439