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

RangeAggregation::removeRangeByKey()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4888
c 0
b 0
f 0
cc 5
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the ONGR package.
7
 *
8
 * (c) NFQ Technologies UAB <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
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 RangeAggregation.
21
 *
22
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-range-aggregation.html
23
 */
24
class RangeAggregation extends AbstractAggregation
25
{
26
    use BucketingTrait;
27
28
    public function __construct(
29
        private 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_PRIVATE, expecting T_VARIABLE
Loading history...
30
        private ?string $field = null,
31
        private array $ranges = [],
32
        private bool $keyed = false
33
    ) {
34
        parent::__construct($name);
35
36
        $this->setField($field);
37
        $this->setKeyed($keyed);
38
        foreach ($ranges as $range) {
39
            $from = isset($range['from']) ? $range['from'] : null;
40
            $to = isset($range['to']) ? $range['to'] : null;
41
            $key = isset($range['key']) ? $range['key'] : null;
42
            $this->addRange($from, $to, $key);
43
        }
44
    }
45
46
    public function setKeyed(bool $keyed): static
47
    {
48
        $this->keyed = $keyed;
49
50
        return $this;
51
    }
52
53
    public function addRange(mixed $from = null, mixed $to = null, string $key = ''): static
54
    {
55
        $range = array_filter(
56
            [
57
                'from' => $from,
58
                'to' => $to,
59
            ]
60
        );
61
62
        if (!empty($key)) {
63
            $range['key'] = $key;
64
        }
65
66
        $this->ranges[] = $range;
67
68
        return $this;
69
    }
70
71
    public function removeRange(int|float|null $from, int|float|null $to): bool
72
    {
73
        foreach ($this->ranges as $key => $range) {
74
            if (array_diff_assoc(array_filter(['from' => $from, 'to' => $to]), $range) === []) {
75
                unset($this->ranges[$key]);
76
77
                return true;
78
            }
79
        }
80
81
        return false;
82
    }
83
84
    public function removeRangeByKey(string $key): bool
85
    {
86
        if ($this->keyed) {
87
            foreach ($this->ranges as $rangeKey => $range) {
88
                if (array_key_exists('key', $range) && $range['key'] === $key) {
89
                    unset($this->ranges[$rangeKey]);
90
91
                    return true;
92
                }
93
            }
94
        }
95
96
        return false;
97
    }
98
99
    public function getArray(): array
100
    {
101
        $data = [
102
            'keyed' => $this->keyed,
103
            'ranges' => array_values($this->ranges),
104
        ];
105
106
        if ($this->getField()) {
107
            $data['field'] = $this->getField();
108
        }
109
110
        return $data;
111
    }
112
113
    public function getType(): string
114
    {
115
        return 'range';
116
    }
117
}
118