Completed
Push — master ( 00021b...d3d348 )
by Nicolas
03:58 queued 41s
created

Terms::setTermsLookup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
1
<?php
2
namespace Elastica\Query;
3
4
use Elastica\Exception\InvalidException;
5
6
/**
7
 * Terms query.
8
 *
9
 * @author Nicolas Ruflin <[email protected]>
10
 * @author Roberto Nygaard <[email protected]>
11
 *
12
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
13
 */
14
class Terms extends AbstractQuery
15
{
16
    /**
17
     * Terms.
18
     *
19
     * @var array Terms
20
     */
21
    protected $_terms;
22
23
    /**
24
     * Terms key.
25
     *
26
     * @var string Terms key
27
     */
28
    protected $_key;
29
30
    /**
31
     * Construct terms query.
32
     *
33
     * @param string $key   OPTIONAL Terms key
34
     * @param array  $terms OPTIONAL Terms list
35
     */
36
    public function __construct($key = '', array $terms = [])
37
    {
38
        $this->setTerms($key, $terms);
39
    }
40
41
    /**
42
     * Sets key and terms for the query.
43
     *
44
     * @param string $key   Terms key
45
     * @param array  $terms Terms for the query.
46
     *
47
     * @return $this
48
     */
49
    public function setTerms($key, array $terms)
50
    {
51
        $this->_key = $key;
52
        $this->_terms = array_values($terms);
53
54
        return $this;
55
    }
56
57
    /**
58
     * Sets key and terms lookup for the query.
59
     *
60
     * @param string $key         Terms key
61
     * @param array  $termsLookup Terms lookup for the query.
62
     *
63
     * @return $this
64
     */
65
    public function setTermsLookup($key, array $termsLookup)
66
    {
67
        $this->_key = $key;
68
        $this->_terms = $termsLookup;
69
70
        return $this;
71
    }
72
73
    /**
74
     * Adds a single term to the list.
75
     *
76
     * @param string $term Term
77
     *
78
     * @return $this
79
     */
80
    public function addTerm($term)
81
    {
82
        $this->_terms[] = $term;
83
84
        return $this;
85
    }
86
87
    /**
88
     * Sets the minimum matching values.
89
     *
90
     * @param int|string $minimum Minimum value
91
     *
92
     * @return $this
93
     */
94
    public function setMinimumMatch($minimum)
95
    {
96
        return $this->setParam('minimum_match', $minimum);
97
    }
98
99
    /**
100
     * Converts the terms object to an array.
101
     *
102
     * @see \Elastica\Query\AbstractQuery::toArray()
103
     *
104
     * @throws \Elastica\Exception\InvalidException If term key is empty
105
     *
106
     * @return array Query array
107
     */
108
    public function toArray()
109
    {
110
        if (empty($this->_key)) {
111
            throw new InvalidException('Terms key has to be set');
112
        }
113
        $this->setParam($this->_key, $this->_terms);
114
115
        return parent::toArray();
116
    }
117
}
118