summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Filter/Terms.php
blob: 2f7c88e3a2920ba4026b2430b5df00d9f1efd11c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php

namespace Elastica\Filter;

use Elastica\Exception\InvalidException;

/**
 * Terms filter
 *
 * @category Xodoa
 * @package Elastica
 * @author Nicolas Ruflin <spam@ruflin.com>
 * @link http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter.html
 */
class Terms extends AbstractFilter
{
    /**
     * Terms
     *
     * @var array Terms
     */
    protected $_terms = array();

    /**
     * Params
     *
     * @var array Params
     */
    protected $_params = array();

    /**
     * Terms key
     *
     * @var string Terms key
     */
    protected $_key = '';

    /**
     * Creates terms filter
     *
     * @param string $key   Terms key
     * @param array  $terms Terms values
     */
    public function __construct($key = '', array $terms = array())
    {
        $this->setTerms($key, $terms);
    }

    /**
     * Sets key and terms for the filter
     *
     * @param  string                      $key   Terms key
     * @param  array                       $terms Terms for the query.
     * @return \Elastica\Filter\Terms
     */
    public function setTerms($key, array $terms)
    {
        $this->_key = $key;
        $this->_terms = array_values($terms);

        return $this;
    }

    /**
     * Set the lookup parameters for this filter
     * @param string $key terms key
     * @param string|\Elastica\Type $type document type from which to fetch the terms values
     * @param string $id id of the document from which to fetch the terms values
     * @param string $path the field from which to fetch the values for the filter
     * @param string|array|\Elastica\Index $options An array of options or the index from which to fetch the terms values. Defaults to the current index.
     * @return \Elastica\Filter\Terms Filter object
     */
    public function setLookup($key, $type, $id, $path, $options = array())
    {
        $this->_key = $key;
        if ($type instanceof \Elastica\Type) {
            $type = $type->getName();
        }
        $this->_terms = array(
            'type' => $type,
            'id' => $id,
            'path' => $path
        );
        
        $index = $options;
        if(is_array($options)) {
            if(isset($options['index'])) {
                $index = $options['index'];
                unset($options['index']);
            }
            $this->_terms = array_merge($options, $this->_terms);
        }
        
        if (!is_null($index)) {
            if ($index instanceof \Elastica\Index) {
                $index = $index->getName();
            }
            $this->_terms['index'] = $index;
        }
        return $this;
    }

    /**
     * Adds an additional term to the query
     *
     * @param  string                      $term Filter term
     * @return \Elastica\Filter\Terms Filter object
     */
    public function addTerm($term)
    {
        $this->_terms[] = $term;

        return $this;
    }

    /**
     * Converts object to an array
     *
     * @see \Elastica\Filter\AbstractFilter::toArray()
     * @throws \Elastica\Exception\InvalidException
     * @return array                               data array
     */
    public function toArray()
    {
        if (empty($this->_key)) {
            throw new InvalidException('Terms key has to be set');
        }
        $this->_params[$this->_key] = $this->_terms;

        return array('terms' => $this->_params);
    }
}