BoolQuery::toArray()   B
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5706
c 0
b 0
f 0
cc 7
nc 7
nop 0
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
     * @param array $container
40
     */
41
    public function __construct(array $container = [])
42
    {
43
        foreach ($container as $type => $queries) {
44
            $queries = is_array($queries) ? $queries : [$queries];
45
46
            array_walk($queries, function ($query) use ($type) {
47
                $this->add($query, $type);
48
            });
49
        }
50
    }
51
52
    /**
53
     * Returns the query instances (by bool type).
54
     *
55
     * @param  string|null $boolType
56
     *
57
     * @return array
58
     */
59
    public function getQueries($boolType = null)
60
    {
61
        if ($boolType === null) {
62
            $queries = [];
63
64
            foreach ($this->container as $item) {
65
                $queries = array_merge($queries, $item);
66
            }
67
68
            return $queries;
69
        }
70
71
        if (isset($this->container[$boolType])) {
72
            return $this->container[$boolType];
73
        }
74
75
        return [];
76
    }
77
78
    /**
79
     * Add BuilderInterface object to bool operator.
80
     *
81
     * @param BuilderInterface $query Query add to the bool.
82
     * @param string           $type  Bool type. Example: must, must_not, should.
83
     * @param string           $key   Key that indicates a builder id.
84
     *
85
     * @return string Key of added builder.
86
     *
87
     * @throws \UnexpectedValueException
88
     */
89
    public function add(BuilderInterface $query, $type = self::MUST, $key = null)
90
    {
91
        if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) {
92
            throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
93
        }
94
95
        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...
96
            $key = bin2hex(random_bytes(30));
97
        }
98
99
        $this->container[$type][$key] = $query;
100
101
        return $key;
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function toArray()
108
    {
109
        if (count($this->container) === 1 && isset($this->container[self::MUST])
110
                && count($this->container[self::MUST]) === 1) {
111
            $query = reset($this->container[self::MUST]);
112
113
            return $query->toArray();
114
        }
115
116
        $output = [];
117
118
        foreach ($this->container as $boolType => $builders) {
119
            /** @var BuilderInterface $builder */
120
            foreach ($builders as $builder) {
121
                $output[$boolType][] = $builder->toArray();
122
            }
123
        }
124
125
        $output = $this->processArray($output);
126
127
        if (empty($output)) {
128
            $output = new \stdClass();
129
        }
130
131
        return [$this->getType() => $output];
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getType()
138
    {
139
        return 'bool';
140
    }
141
}
142