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     * @param AbstractQuery|array $match
18     */
19    public function __construct($match = null, ?int $end = null)
20    {
21        if (null !== $match) {
22            $this->setMatch($match);
23        }
24
25        if (null !== $match) {
26            $this->setEnd($end);
27        }
28    }
29
30    /**
31     * Set the query to be wrapped into the span multi query.
32     *
33     * @param AbstractSpanQuery|array $args Matching query
34     *
35     * @throws InvalidException If not valid query
36     *
37     * @return $this
38     */
39    public function setMatch($args): self
40    {
41        return $this->_setQuery('match', $args);
42    }
43
44    /**
45     * Set the maximum end position for the match query.
46     *
47     * @return $this
48     */
49    public function setEnd(int $end): self
50    {
51        $this->setParam('end', $end);
52
53        return $this;
54    }
55
56    /**
57     * Sets a query to the current object.
58     *
59     * @param string              $type Query type
60     * @param AbstractQuery|array $args Query
61     *
62     * @throws InvalidException If not valid query
63     *
64     * @return $this
65     */
66    protected function _setQuery(string $type, $args): self
67    {
68        if (!\is_array($args) && !($args instanceof AbstractSpanQuery)) {
69            throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractSpanQuery');
70        }
71
72        return $this->setParam($type, $args);
73    }
74}
75