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

GeoDistanceAggregation::getArray()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.1608
c 0
b 0
f 0
cc 5
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
declare(strict_types=1);
13
14
namespace ONGR\ElasticsearchDSL\Aggregation\Bucketing;
15
16
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
17
use ONGR\ElasticsearchDSL\Aggregation\Type\BucketingTrait;
18
19
/**
20
 * Class representing geo distance aggregation.
21
 *
22
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html
23
 */
24
class GeoDistanceAggregation extends AbstractAggregation
25
{
26
    use BucketingTrait;
27
28
    /**
29
     * Inner aggregations container init.
30
     */
31
    public function __construct(
32
        protected string $name,
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_PROTECTED, expecting T_VARIABLE
Loading history...
33
        protected ?string $field = null,
34
        protected ?string $origin = null,
35
        protected array $ranges = [],
36
        protected ?string $unit = null,
37
        protected ?string $distanceType = null
38
    ) {
39
        parent::__construct($name);
40
41
        $this->setField($field);
42
        $this->setOrigin($origin);
43
        foreach ($ranges as $range) {
44
            $from = isset($range['from']) ? $range['from'] : null;
45
            $to = isset($range['to']) ? $range['to'] : null;
46
            $this->addRange($from, $to);
47
        }
48
        $this->setUnit($unit);
49
        $this->setDistanceType($distanceType);
50
    }
51
52
    public function getOrigin(): ?string
53
    {
54
        return $this->origin;
55
    }
56
57
    public function setOrigin(?string $origin): static
58
    {
59
        $this->origin = $origin;
60
61
        return $this;
62
    }
63
64
    public function getDistanceType(): ?string
65
    {
66
        return $this->distanceType;
67
    }
68
69
    public function setDistanceType(?string $distanceType): static
70
    {
71
        $this->distanceType = $distanceType;
72
73
        return $this;
74
    }
75
76
    public function getUnit(): ?string
77
    {
78
        return $this->unit;
79
    }
80
81
    public function setUnit(?string $unit): static
82
    {
83
        $this->unit = $unit;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Add range to aggregation.
90
     */
91
    public function addRange(null|int|float|string $from = null, null|int|float|string $to = null): static
92
    {
93
        $range = array_filter(
94
            [
95
                'from' => $from,
96
                'to' => $to,
97
            ]
98
        );
99
100
        if (empty($range)) {
101
            throw new \LogicException('Either from or to must be set. Both cannot be null.');
102
        }
103
104
        $this->ranges[] = $range;
105
106
        return $this;
107
    }
108
109
    public function getArray(): array
110
    {
111
        $data = [];
112
113
        if ($this->getField()) {
114
            $data['field'] = $this->getField();
115
        } else {
116
            throw new \LogicException('Geo distance aggregation must have a field set.');
117
        }
118
119
        if ($this->getOrigin()) {
120
            $data['origin'] = $this->getOrigin();
121
        } else {
122
            throw new \LogicException('Geo distance aggregation must have an origin set.');
123
        }
124
125
        if ($this->getUnit()) {
126
            $data['unit'] = $this->getUnit();
127
        }
128
129
        if ($this->getDistanceType()) {
130
            $data['distance_type'] = $this->getDistanceType();
131
        }
132
133
        $data['ranges'] = $this->ranges;
134
135
        return $data;
136
    }
137
138
    public function getType(): string
139
    {
140
        return 'geo_distance';
141
    }
142
}
143