RangeAggregation::removeRange()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 3
nc 3
nop 2
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 RangeAggregation.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html
21
 */
22
class RangeAggregation extends AbstractAggregation
23
{
24
    use BucketingTrait;
25
26
    /**
27
     * @var array
28
     */
29
    private $ranges = [];
30
31
    /**
32
     * @var bool
33
     */
34
    private $keyed = false;
35
36
    /**
37
     * Inner aggregations container init.
38
     *
39
     * @param string $name
40
     * @param string $field
41
     * @param array  $ranges
42
     * @param bool   $keyed
43
     */
44 View Code Duplication
    public function __construct($name, $field = null, $ranges = [], $keyed = false)
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...
45
    {
46
        parent::__construct($name);
47
48
        $this->setField($field);
49
        $this->setKeyed($keyed);
50
        foreach ($ranges as $range) {
51
            $from = isset($range['from']) ? $range['from'] : null;
52
            $to = isset($range['to']) ? $range['to'] : null;
53
            $key = isset($range['key']) ? $range['key'] : null;
54
            $this->addRange($from, $to, $key);
55
        }
56
    }
57
58
    /**
59
     * Sets if result buckets should be keyed.
60
     *
61
     * @param bool $keyed
62
     *
63
     * @return $this
64
     */
65
    public function setKeyed($keyed)
66
    {
67
        $this->keyed = $keyed;
68
69
        return $this;
70
    }
71
72
    /**
73
     * Add range to aggregation.
74
     *
75
     * @param int|float|null $from
76
     * @param int|float|null $to
77
     * @param string         $key
78
     *
79
     * @return RangeAggregation
80
     */
81
    public function addRange($from = null, $to = null, $key = '')
82
    {
83
        $range = array_filter(
84
            [
85
                'from' => $from,
86
                'to' => $to,
87
            ],
88
            function ($v) {
89
                return !is_null($v);
90
            }
91
        );
92
93
        if (!empty($key)) {
94
            $range['key'] = $key;
95
        }
96
97
        $this->ranges[] = $range;
98
99
        return $this;
100
    }
101
102
    /**
103
     * Remove range from aggregation. Returns true on success.
104
     *
105
     * @param int|float|null $from
106
     * @param int|float|null $to
107
     *
108
     * @return bool
109
     */
110
    public function removeRange($from, $to)
111
    {
112
        foreach ($this->ranges as $key => $range) {
113
            if (array_diff_assoc(array_filter(['from' => $from, 'to' => $to]), $range) === []) {
114
                unset($this->ranges[$key]);
115
116
                return true;
117
            }
118
        }
119
120
        return false;
121
    }
122
123
    /**
124
     * Removes range by key.
125
     *
126
     * @param string $key Range key.
127
     *
128
     * @return bool
129
     */
130
    public function removeRangeByKey($key)
131
    {
132
        if ($this->keyed) {
133
            foreach ($this->ranges as $rangeKey => $range) {
134
                if (array_key_exists('key', $range) && $range['key'] === $key) {
135
                    unset($this->ranges[$rangeKey]);
136
137
                    return true;
138
                }
139
            }
140
        }
141
142
        return false;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function getArray()
149
    {
150
        $data = [
151
            'keyed' => $this->keyed,
152
            'ranges' => array_values($this->ranges),
153
        ];
154
155
        if ($this->getField()) {
156
            $data['field'] = $this->getField();
157
        }
158
159
        return $data;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getType()
166
    {
167
        return 'range';
168
    }
169
}
170