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