Completed
Push — master ( 197eed...6f0e33 )
by Simonas
10:57 queued 11s
created

GeoBoundingBoxQuery::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
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
        return [
62
            $this->getType() => $this->processArray([$this->field => $this->points()])
63
        ];
64
    }
65
66
    /**
67
     * @return array
68
     *
69
     * @throws \LogicException
70
     */
71
    private function points()
72
    {
73
        if (count($this->values) === 2) {
74
            return [
75
                'top_left' => $this->values[0] ?? $this->values['top_left'],
76
                'bottom_right' => $this->values[1] ?? $this->values['bottom_right'],
77
            ];
78
        } elseif (count($this->values) === 4) {
79
            return [
80
                'top' => $this->values[0] ?? $this->values['top'],
81
                'left' => $this->values[1] ?? $this->values['left'],
82
                'bottom' => $this->values[2] ?? $this->values['bottom'],
83
                'right' => $this->values[3] ?? $this->values['right'],
84
            ];
85
        }
86
87
        throw new \LogicException('Geo Bounding Box filter must have 2 or 4 geo points set.');
88
    }
89
}
90