Completed
Pull Request — master (#348)
by
unknown
01:15
created

Aggregation/Bucketing/GeoDistanceAggregation.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
     * @return $this
83
     */
84
    public function setOrigin($origin)
85
    {
86
        $this->origin = $origin;
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getDistanceType()
95
    {
96
        return $this->distanceType;
97
    }
98
99
    /**
100
     * @param string $distanceType
101
     *
102
     * @return $this
103
     */
104
    public function setDistanceType($distanceType)
105
    {
106
        $this->distanceType = $distanceType;
107
108
        return $this;
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getUnit()
115
    {
116
        return $this->unit;
117
    }
118
119
    /**
120
     * @param string $unit
121
     *
122
     * @return $this
123
     */
124
    public function setUnit($unit)
125
    {
126
        $this->unit = $unit;
127
128
        return $this;
129
    }
130
131
    /**
132
     * Add range to aggregation.
133
     *
134
     * @param int|float|null $from
135
     * @param int|float|null $to
136
     *
137
     * @throws \LogicException
138
     *
139
     * @return GeoDistanceAggregation
140
     */
141 View Code Duplication
    public function addRange($from = null, $to = null)
0 ignored issues
show
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...
142
    {
143
        $range = array_filter(
144
            [
145
                'from' => $from,
146
                'to' => $to,
147
            ],
148
            function ($v) {
149
                return !is_null($v);
150
            }
151
        );
152
153
        if (empty($range)) {
154
            throw new \LogicException('Either from or to must be set. Both cannot be null.');
155
        }
156
157
        $this->ranges[] = $range;
158
159
        return $this;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getArray()
166
    {
167
        $data = [];
168
169
        if ($this->getField()) {
170
            $data['field'] = $this->getField();
171
        } else {
172
            throw new \LogicException('Geo distance aggregation must have a field set.');
173
        }
174
175
        if ($this->getOrigin()) {
176
            $data['origin'] = $this->getOrigin();
177
        } else {
178
            throw new \LogicException('Geo distance aggregation must have an origin set.');
179
        }
180
181
        if ($this->getUnit()) {
182
            $data['unit'] = $this->getUnit();
183
        }
184
185
        if ($this->getDistanceType()) {
186
            $data['distance_type'] = $this->getDistanceType();
187
        }
188
189
        $data['ranges'] = $this->ranges;
190
191
        return $data;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197
    public function getType()
198
    {
199
        return 'geo_distance';
200
    }
201
}
202