summaryrefslogtreecommitdiff
path: root/vendor/ruflin/elastica/lib/Elastica/Filter/Bool.php
blob: aff708f8be2761503009d05ecbe9ca6be6ff34f8 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php

namespace Elastica\Filter;

use Elastica\Exception\InvalidException;

/**
 * Bool Filter
 *
 * @category Xodoa
 * @package Elastica
 * @author Nicolas Ruflin <spam@ruflin.com>
 * @link http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html
 */
class Bool extends AbstractFilter
{
    /**
     * @var float
     */
    protected $_boost = 1.0;

    /**
     * Must
     *
     * @var array
     */
    protected $_must = array();

    /**
     * Should
     *
     * @var array
     */
    protected $_should = array();

    /**
     * Must not
     *
     * @var array
     */
    protected $_mustNot = array();

    /**
     * Adds should filter
     *
     * @param  array|\Elastica\Filter\AbstractFilter $args Filter data
     * @return \Elastica\Filter\Bool           Current object
     */
    public function addShould($args)
    {
        return $this->_addFilter('should', $args);
    }

    /**
     * Adds must filter
     *
     * @param  array|\Elastica\Filter\AbstractFilter $args Filter data
     * @return \Elastica\Filter\Bool           Current object
     */
    public function addMust($args)
    {
        return $this->_addFilter('must', $args);
    }

    /**
     * Adds mustNot filter
     *
     * @param  array|\Elastica\Filter\AbstractFilter $args Filter data
     * @return \Elastica\Filter\Bool           Current object
     */
    public function addMustNot($args)
    {
        return $this->_addFilter('mustNot', $args);
    }

    /**
     * Adds general filter based on type
     *
     * @param  string                               $type Filter type
     * @param  array|\Elastica\Filter\AbstractFilter $args Filter data
     * @throws \Elastica\Exception\InvalidException
     * @return \Elastica\Filter\Bool           Current object
     */
    protected function _addFilter($type, $args)
    {
        if ($args instanceof AbstractFilter) {
            $args = $args->toArray();
        }
        else if (!is_array($args)) {
            throw new InvalidException('Invalid parameter. Has to be array or instance of Elastica\Filter');
        }
        else{
            $parsedArgs = array();
            foreach($args as $filter){
                if($filter instanceof AbstractFilter){
                    $parsedArgs[] = $filter->toArray();
                }
            }
            $args = $parsedArgs;
        }

        $varName = '_' . $type;
        $this->{$varName}[] = $args;

        return $this;
    }

    /**
     * Converts bool filter to array
     *
     * @see \Elastica\Filter\AbstractFilter::toArray()
     * @return array Filter array
     */
    public function toArray()
    {
        $args = array();

        if (!empty($this->_must)) {
            $args['bool']['must'] = $this->_must;
        }

        if (!empty($this->_should)) {
            $args['bool']['should'] = $this->_should;
        }

        if (!empty($this->_mustNot)) {
            $args['bool']['must_not'] = $this->_mustNot;
        }

        return $args;
    }

    /**
     * Sets the boost value for this filter
     *
     * @param  float                      $boost Boost
     * @return \Elastica\Filter\Bool Current object
     */
    public function setBoost($boost)
    {
        $this->_boost = $boost;

        return $this;
    }

}