1<?php
2
3namespace Elastica\Query;
4
5use Elastica\Exception\InvalidException;
6
7/**
8 * Bool query.
9 *
10 * @author Nicolas Ruflin <spam@ruflin.com>
11 *
12 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
13 */
14class BoolQuery extends AbstractQuery
15{
16    /**
17     * Add should part to query.
18     *
19     * @param AbstractQuery|array $args Should query
20     *
21     * @return $this
22     */
23    public function addShould($args): self
24    {
25        return $this->_addQuery('should', $args);
26    }
27
28    /**
29     * Add must part to query.
30     *
31     * @param AbstractQuery|array $args Must query
32     *
33     * @return $this
34     */
35    public function addMust($args): self
36    {
37        return $this->_addQuery('must', $args);
38    }
39
40    /**
41     * Add must not part to query.
42     *
43     * @param AbstractQuery|array $args Must not query
44     *
45     * @return $this
46     */
47    public function addMustNot($args): self
48    {
49        return $this->_addQuery('must_not', $args);
50    }
51
52    /**
53     * Sets the filter.
54     *
55     * @param AbstractQuery $filter Filter object
56     *
57     * @return $this
58     */
59    public function addFilter(AbstractQuery $filter): self
60    {
61        return $this->addParam('filter', $filter);
62    }
63
64    /**
65     * Adds a query to the current object.
66     *
67     * @param string              $type Query type
68     * @param AbstractQuery|array $args Query
69     *
70     * @throws InvalidException If not valid query
71     *
72     * @return $this
73     */
74    protected function _addQuery(string $type, $args): self
75    {
76        if (!\is_array($args) && !($args instanceof AbstractQuery)) {
77            throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Query\AbstractQuery');
78        }
79
80        return $this->addParam($type, $args);
81    }
82
83    /**
84     * Sets boost value of this query.
85     *
86     * @param float $boost Boost value
87     *
88     * @return $this
89     */
90    public function setBoost(float $boost): self
91    {
92        return $this->setParam('boost', $boost);
93    }
94
95    /**
96     * Sets the minimum number of should clauses to match.
97     *
98     * @param int|string $minimum Minimum value
99     *
100     * @return $this
101     */
102    public function setMinimumShouldMatch($minimum): self
103    {
104        return $this->setParam('minimum_should_match', $minimum);
105    }
106
107    /**
108     * {@inheritdoc}
109     */
110    public function toArray(): array
111    {
112        if (empty($this->_params)) {
113            $this->_params = new \stdClass();
114        }
115
116        return parent::toArray();
117    }
118}
119