Completed
Push — master ( 6c9334...f3ba17 )
by Simonas
02:26
created

BoolQuery::getQueries()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 18
rs 9.2
cc 4
eloc 9
nc 4
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Query\Compound;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
17
/**
18
 * Represents Elasticsearch "bool" query.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
21
 */
22
class BoolQuery implements BuilderInterface
23
{
24
    use ParametersTrait;
25
26
    const MUST = 'must';
27
    const MUST_NOT = 'must_not';
28
    const SHOULD = 'should';
29
    const FILTER = 'filter';
30
31
    /**
32
     * @var array
33
     */
34
    private $container = [];
35
36
    /**
37
     * Constructor to prepare container.
38
     */
39
    public function __construct()
40
    {
41
        $this->container = [];
42
    }
43
44
    /**
45
     * Returns the query instances (by bool type).
46
     *
47
     * @param  string|null $boolType
48
     *
49
     * @return array
50
     */
51
    public function getQueries($boolType = null)
52
    {
53
        if ($boolType === null) {
54
            $queries = [];
55
56
            foreach ($this->container as $item) {
57
                $queries = array_merge($queries, $item);
58
            }
59
60
            return $queries;
61
        }
62
63
        if (isset($this->container[$boolType])) {
64
            return $this->container[$boolType];
65
        }
66
67
        return [];
68
    }
69
70
    /**
71
     * Add BuilderInterface object to bool operator.
72
     *
73
     * @param BuilderInterface $query Query add to the bool.
74
     * @param string           $type  Bool type. Example: must, must_not, should.
75
     * @param string           $key   Key that indicates a builder id.
76
     *
77
     * @return string Key of added builder.
78
     *
79
     * @throws \UnexpectedValueException
80
     */
81
    public function add(BuilderInterface $query, $type = self::MUST, $key = null)
82
    {
83
        if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) {
84
            throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
85
        }
86
87
        if (!$key) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $key of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
88
            $key = bin2hex(random_bytes(30));
89
        }
90
91
        $this->container[$type][$key] = $query;
92
93
        return $key;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function toArray()
100
    {
101
        if (count($this->container) === 1 && isset($this->container[self::MUST])
102
                && count($this->container[self::MUST]) === 1) {
103
            $query = reset($this->container[self::MUST]);
104
105
            return $query->toArray();
106
        }
107
108
        $output = [];
109
110
        foreach ($this->container as $boolType => $builders) {
111
            /** @var BuilderInterface $builder */
112
            foreach ($builders as $builder) {
113
                $output[$boolType][] = $builder->toArray();
114
            }
115
        }
116
117
        $output = $this->processArray($output);
118
119
        return [$this->getType() => $output];
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getType()
126
    {
127
        return 'bool';
128
    }
129
}
130