Completed
Pull Request — master (#106)
by
unknown
03:04
created

OrQuery::addQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16
17
/**
18
 * Represents Elasticsearch "or" query.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-or-query.html
21
 */
22
class OrQuery implements BuilderInterface
23
{
24
    /**
25
     * @var array
26
     */
27
    private $queries;
28
29
    /**
30
     * @param array $queries
31
     */
32
    public function __construct(array $queries = [])
33
    {
34
        $this->queries = $queries;
35
    }
36
37
    /**
38
     * adds a query to join with AND operator
39
     *
40
     * @param BuilderInterface $query
41
     */
42
    public function addQuery(BuilderInterface $query)
43
    {
44
        $this->queries[] = $query;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getType()
51
    {
52
        return 'or';
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function toArray()
59
    {
60
        $output = [];
61
        foreach ($this->queries as $query) {
62
            try {
63
                $output[] = $query->toArray();
64
            } catch (\Error $e) {
0 ignored issues
show
Bug introduced by
The class Error does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
65
                throw new InvalidArgumentException(
66
                    'Queries given to `OR` query must be instances of BuilderInterface'
67
                );
68
            }
69
        }
70
71
        return [$this->getType() => $output];
72
    }
73
}
74