Completed
Pull Request — master (#104)
by
unknown
11:31 queued 08:03
created

FilteredQuery::toArray()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
rs 8.8571
cc 5
eloc 7
nc 6
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;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
16
/**
17
 * Represents Elasticsearch "filtered" query.
18
 *
19
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-filtered-query.html
20
 */
21
class FilteredQuery implements BuilderInterface
22
{
23
    /**
24
     * @var BuilderInterface
25
     */
26
    private $query;
27
28
    /**
29
     * @var BuilderInterface
30
     */
31
    private $filter;
32
33
    /**
34
     * @var array
35
     */
36
    private $boolFilter;
37
38
    /**
39
     * @param BuilderInterface $query
40
     * @param BuilderInterface $filter
41
     */
42
    public function __construct(BuilderInterface $query = null, BuilderInterface $filter = null)
43
    {
44
        $this->query = $query;
45
        $this->filter = $filter;
46
    }
47
48
    /**
49
     * adds a query to join with AND operator
50
     *
51
     * @param BuilderInterface $must
52
     * @param BuilderInterface $should
53
     * @param BuilderInterface $mustNot
54
     */
55
    public function addBoolFilter(
56
        BuilderInterface $must = null,
57
        BuilderInterface $should = null,
58
        BuilderInterface $mustNot = null
59
    ) {
60
        if (!$must && !$should && !$mustNot) {
61
            return;
62
        }
63
        $filter = [];
64
        if ($must) {
65
            $filter['bool']['must'] = $must->toArray();
66
        }
67
        if ($should) {
68
            $filter['bool']['should'] = $should->toArray();
69
        }
70
        if ($mustNot) {
71
            $filter['bool']['should'] = $should->toArray();
0 ignored issues
show
Bug introduced by
It seems like $should is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
72
        }
73
74
        $this->boolFilter = $filter;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getType()
81
    {
82
        return 'filtered';
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function toArray()
89
    {
90
        $output = [];
91
92
        if (isset($this->query)) {
93
            $output['query'] = $this->query->toArray();
94
        }
95
        if (isset($this->filter) || isset($this->boolFilter)) {
96
            $output['filter'] = isset($this->filter) ? $this->filter->toArray() : $this->boolFilter;
97
        }
98
99
        return [$this->getType() => $output];
100
    }
101
}
102