1<?php
2
3namespace Elastica\Query;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * SpanFirst query.
9 *
10 * @author Alessandro Chitolina <alekitto@gmail.com>
11 *
12 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-span-first-query.html
13 */
14class SpanFirst extends AbstractSpanQuery
15{
16    /**
17     * Constructs a SpanFirst query object.
18     *
19     * @param AbstractQuery|array $match
20     * @param int                 $end
21     */
22    public function __construct($match = null, int $end = null)
23    {
24        if (null !== $match) {
25            $this->setMatch($match);
26        }
27
28        if (null !== $match) {
29            $this->setEnd($end);
30        }
31    }
32
33    /**
34     * Set the query to be wrapped into the span multi query.
35     *
36     * @param AbstractSpanQuery|array $args Matching query
37     *
38     * @throws InvalidException If not valid query
39     *
40     * @return $this
41     */
42    public function setMatch($args): self
43    {
44        return $this->_setQuery('match', $args);
45    }
46
47    /**
48     * Set the maximum end position for the match query.
49     *
50     * @param int $end
51     *
52     * @return $this
53     */
54    public function setEnd(int $end): self
55    {
56        $this->setParam('end', $end);
57
58        return $this;
59    }
60
61    /**
62     * Sets a query to the current object.
63     *
64     * @param string              $type Query type
65     * @param AbstractQuery|array $args Query
66     *
67     * @throws InvalidException If not valid query
68     *
69     * @return $this
70     */
71    protected function _setQuery(string $type, $args): self
72    {
73        if (!\is_array($args) && !($args instanceof AbstractSpanQuery)) {
74            throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractSpanQuery');
75        }
76
77        return $this->setParam($type, $args);
78    }
79}
80