Completed
Pull Request — master (#348)
by
unknown
09:42 queued 08:20
created

BoolQuery::toArray()   B

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
declare(strict_types=1);
13
14
namespace ONGR\ElasticsearchDSL\Query\Compound;
15
16
use ONGR\ElasticsearchDSL\BuilderInterface;
17
use ONGR\ElasticsearchDSL\ParametersTrait;
18
19
/**
20
 * Represents Elasticsearch "bool" query.
21
 *
22
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html
23
 */
24
class BoolQuery implements BuilderInterface
25
{
26
    use ParametersTrait;
27
28
    public const MUST = 'must';
29
30
    public const MUST_NOT = 'must_not';
31
32
    public const SHOULD = 'should';
33
34
    public const FILTER = 'filter';
35
36
    private array $container = [];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
37
38
    public function __construct(array $container = [])
39
    {
40
        foreach ($container as $type => $queries) {
41
            $queries = is_array($queries) ? $queries : [$queries];
42
43
            array_walk(
44
                $queries,
45
                function (BuilderInterface $query) use ($type): void {
46
                    $this->add($query, $type);
47
                }
48
            );
49
        }
50
    }
51
52
    public function getQueries(?string $boolType = null): array
53
    {
54
        if ($boolType === null) {
55
            $queries = [];
56
57
            foreach ($this->container as $item) {
58
                $queries = array_merge($queries, $item);
59
            }
60
61
            return $queries;
62
        }
63
64
        if (isset($this->container[$boolType])) {
65
            return $this->container[$boolType];
66
        }
67
68
        return [];
69
    }
70
71
    public function add(BuilderInterface $query, string $type = self::MUST, ?string $key = null): string
72
    {
73
        if (!in_array($type, [self::MUST, self::MUST_NOT, self::SHOULD, self::FILTER])) {
74
            throw new \UnexpectedValueException(sprintf('The bool operator %s is not supported', $type));
75
        }
76
77
        if (!$key) {
78
            $key = bin2hex(random_bytes(30));
79
        }
80
81
        $this->container[$type][$key] = $query;
82
83
        return $key;
84
    }
85
86
    public function toArray(): array
87
    {
88
        if (count($this->container) === 1 && isset($this->container[self::MUST])
89
            && count($this->container[self::MUST]) === 1) {
90
            $query = reset($this->container[self::MUST]);
91
92
            return $query->toArray();
93
        }
94
95
        $output = [];
96
97
        foreach ($this->container as $boolType => $builders) {
98
            /** @var BuilderInterface $builder */
99
            foreach ($builders as $builder) {
100
                $output[$boolType][] = $builder->toArray();
101
            }
102
        }
103
104
        $output = $this->processArray($output);
105
106
        if (empty($output)) {
107
            $output = new \stdClass();
108
        }
109
110
        return [$this->getType() => $output];
111
    }
112
113
    public function getType(): string
114
    {
115
        return 'bool';
116
    }
117
}
118