Completed
Push — master ( 6c9334...f3ba17 )
by Simonas
02:26
created

GeoBoundingBoxQuery::toArray()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 8.8571
cc 3
eloc 17
nc 3
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\Geo;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
17
/**
18
 * Represents Elasticsearch "geo_bounding_box" query.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html
21
 */
22
class GeoBoundingBoxQuery implements BuilderInterface
23
{
24
    use ParametersTrait;
25
26
    /**
27
     * @var array
28
     */
29
    private $values;
30
31
    /**
32
     * @var string
33
     */
34
    private $field;
35
36
    /**
37
     * @param string $field
38
     * @param array  $values
39
     * @param array  $parameters
40
     */
41
    public function __construct($field, $values, array $parameters = [])
42
    {
43
        $this->field = $field;
44
        $this->values = $values;
45
        $this->setParameters($parameters);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getType()
52
    {
53
        return 'geo_bounding_box';
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function toArray()
60
    {
61
        if (count($this->values) === 2) {
62
            $query = [
63
                $this->field => [
64
                    'top_left' => $this->values[0],
65
                    'bottom_right' => $this->values[1],
66
                ],
67
            ];
68
        } elseif (count($this->values) === 4) {
69
            $query = [
70
                $this->field => [
71
                    'top' => $this->values[0],
72
                    'left' => $this->values[1],
73
                    'bottom' => $this->values[2],
74
                    'right' => $this->values[3],
75
                ],
76
            ];
77
        } else {
78
            throw new \LogicException('Geo Bounding Box filter must have 2 or 4 geo points set.');
79
        }
80
81
        $output = $this->processArray($query);
82
83
        return [$this->getType() => $output];
84
    }
85
}
86