1<?php
2
3namespace Elastica\Query;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * SpanNear query.
9 *
10 * @author Marek Hernik <marek.hernik@gmail.com>
11 *
12 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-near-query.html
13 */
14class SpanNear extends AbstractSpanQuery
15{
16    /**
17     * Constructs a SpanNear query object.
18     *
19     * @param AbstractSpanQuery[] $clauses
20     * @param int                 $slop    maximum proximity
21     * @param bool                $inOrder true if order of searched clauses is important
22     */
23    public function __construct(array $clauses = [], int $slop = 1, bool $inOrder = false)
24    {
25        if (!empty($clauses)) {
26            foreach ($clauses as $clause) {
27                if (!$clause instanceof AbstractSpanQuery) {
28                    throw new InvalidException(
29                        'Invalid parameter. Has to be array or instance of '.AbstractSpanQuery::class
30                    );
31                }
32            }
33        }
34        $this->setParams(['clauses' => $clauses]);
35        $this->setSlop($slop);
36        $this->setInOrder($inOrder);
37    }
38
39    /**
40     * @param int $slop
41     *
42     * @return $this
43     */
44    public function setSlop(int $slop): self
45    {
46        return $this->setParam('slop', $slop);
47    }
48
49    /**
50     * @param bool $inOrder
51     *
52     * @return $this
53     */
54    public function setInOrder(bool $inOrder): self
55    {
56        return $this->setParam('in_order', $inOrder);
57    }
58
59    /**
60     * Add clause part to query.
61     *
62     * @param AbstractSpanQuery $clause
63     *
64     * @return $this
65     */
66    public function addClause(AbstractSpanQuery $clause): self
67    {
68        return $this->addParam('clauses', $clause);
69    }
70}
71