Completed
Pull Request — master (#148)
by Simonas
05:22
created

GeoDistanceAggregation::getArray()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 8.439
cc 5
eloc 16
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\Aggregation\Bucketing;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
16
17
/**
18
 * Class representing geo distance aggregation.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html
21
 */
22
class GeoDistanceAggregation extends AbstractAggregation
23
{
24
    use BucketingTrait;
25
26
    /**
27
     * @var mixed
28
     */
29
    private $origin;
30
31
    /**
32
     * @var string
33
     */
34
    private $distanceType;
35
36
    /**
37
     * @var string
38
     */
39
    private $unit;
40
41
    /**
42
     * @var array
43
     */
44
    private $ranges = [];
45
46
    /**
47
     * Inner aggregations container init.
48
     *
49
     * @param string $name
50
     * @param string $field
51
     * @param mixed  $origin
52
     * @param array  $ranges
53
     * @param string $unit
54
     * @param string $distanceType
55
     */
56
    public function __construct($name, $field = null, $origin = null, $ranges = [], $unit = null, $distanceType = null)
57
    {
58
        parent::__construct($name);
59
60
        $this->setField($field);
61
        $this->setOrigin($origin);
62
        foreach ($ranges as $range) {
63
            $from = isset($range['from']) ? $range['from'] : null;
64
            $to = isset($range['to']) ? $range['to'] : null;
65
            $this->addRange($from, $to);
66
        }
67
        $this->setUnit($unit);
68
        $this->setDistanceType($distanceType);
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getOrigin()
75
    {
76
        return $this->origin;
77
    }
78
79
    /**
80
     * @param mixed $origin
81
     */
82
    public function setOrigin($origin)
83
    {
84
        $this->origin = $origin;
85
    }
86
87
    /**
88
     * @return string
89
     */
90
    public function getDistanceType()
91
    {
92
        return $this->distanceType;
93
    }
94
95
    /**
96
     * @param string $distanceType
97
     */
98
    public function setDistanceType($distanceType)
99
    {
100
        $this->distanceType = $distanceType;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getUnit()
107
    {
108
        return $this->unit;
109
    }
110
111
    /**
112
     * @param string $unit
113
     */
114
    public function setUnit($unit)
115
    {
116
        $this->unit = $unit;
117
    }
118
119
    /**
120
     * Add range to aggregation.
121
     *
122
     * @param int|float|null $from
123
     * @param int|float|null $to
124
     *
125
     * @throws \LogicException
126
     *
127
     * @return GeoDistanceAggregation
128
     */
129 View Code Duplication
    public function addRange($from = null, $to = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
130
    {
131
        $range = array_filter(
132
            [
133
                'from' => $from,
134
                'to' => $to,
135
            ]
136
        );
137
138
        if (empty($range)) {
139
            throw new \LogicException('Either from or to must be set. Both cannot be null.');
140
        }
141
142
        $this->ranges[] = $range;
143
144
        return $this;
145
    }
146
147
    /**
148
     * {@inheritdoc}
149
     */
150
    public function getArray()
151
    {
152
        $data = [];
153
154
        if ($this->getField()) {
155
            $data['field'] = $this->getField();
156
        } else {
157
            throw new \LogicException('Geo distance aggregation must have a field set.');
158
        }
159
160
        if ($this->getOrigin()) {
161
            $data['origin'] = $this->getOrigin();
162
        } else {
163
            throw new \LogicException('Geo distance aggregation must have an origin set.');
164
        }
165
166
        if ($this->getUnit()) {
167
            $data['unit'] = $this->getUnit();
168
        }
169
170
        if ($this->getDistanceType()) {
171
            $data['distance_type'] = $this->getDistanceType();
172
        }
173
174
        $data['ranges'] = $this->ranges;
175
176
        return $data;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function getType()
183
    {
184
        return 'geo_distance';
185
    }
186
}
187