1<?php
2
3namespace Elastica\Query;
4
5use Elastica\Exception\InvalidException;
6use Elastica\Script\AbstractScript;
7
8/**
9 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-set-query.html
10 */
11class TermsSet extends AbstractQuery
12{
13    /**
14     * @var string
15     */
16    private $field;
17
18    /**
19     * @param array<bool|float|int|string> $terms
20     * @param AbstractScript|string        $minimumShouldMatch
21     */
22    public function __construct(string $field, array $terms, $minimumShouldMatch)
23    {
24        if ('' === $field) {
25            throw new InvalidException('TermsSet field name has to be set');
26        }
27
28        $this->field = $field;
29        $this->setTerms($terms);
30
31        if (\is_string($minimumShouldMatch)) {
32            $this->setMinimumShouldMatchField($minimumShouldMatch);
33        } elseif ($minimumShouldMatch instanceof AbstractScript) {
34            $this->setMinimumShouldMatchScript($minimumShouldMatch);
35        } else {
36            throw new \TypeError(\sprintf('Argument 3 passed to "%s()" must be of type %s|string, %s given.', __METHOD__, AbstractScript::class, \is_object($minimumShouldMatch) ? \get_class($minimumShouldMatch) : \gettype($minimumShouldMatch)));
37        }
38    }
39
40    /**
41     * @param array<bool|float|int|string> $terms
42     */
43    public function setTerms(array $terms): self
44    {
45        return $this->addParam($this->field, $terms, 'terms');
46    }
47
48    public function setMinimumShouldMatchField(string $minimumShouldMatchField): self
49    {
50        return $this->addParam($this->field, $minimumShouldMatchField, 'minimum_should_match_field');
51    }
52
53    public function setMinimumShouldMatchScript(AbstractScript $script): self
54    {
55        return $this->addParam($this->field, $script->toArray()['script'], 'minimum_should_match_script');
56    }
57}
58