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     * @param AbstractSpanQuery[] $clauses
18     * @param int                 $slop    maximum proximity
19     * @param bool                $inOrder true if order of searched clauses is important
20     */
21    public function __construct(array $clauses = [], int $slop = 1, bool $inOrder = false)
22    {
23        foreach ($clauses as $clause) {
24            if (!$clause instanceof AbstractSpanQuery) {
25                throw new InvalidException('Invalid parameter. Has to be array or instance of '.AbstractSpanQuery::class);
26            }
27        }
28
29        $this->setParams(['clauses' => $clauses]);
30        $this->setSlop($slop);
31        $this->setInOrder($inOrder);
32    }
33
34    /**
35     * @return $this
36     */
37    public function setSlop(int $slop): self
38    {
39        return $this->setParam('slop', $slop);
40    }
41
42    /**
43     * @return $this
44     */
45    public function setInOrder(bool $inOrder): self
46    {
47        return $this->setParam('in_order', $inOrder);
48    }
49
50    /**
51     * Add clause part to query.
52     *
53     * @return $this
54     */
55    public function addClause(AbstractSpanQuery $clause): self
56    {
57        return $this->addParam('clauses', $clause);
58    }
59}
60