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

Ipv4RangeAggregation::__construct()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4555
c 0
b 0
f 0
cc 5
nc 6
nop 3
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 ip range aggregation.
21
 *
22
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-iprange-aggregation.html
23
 */
24
class Ipv4RangeAggregation 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
    ) {
33
        parent::__construct($name);
34
35
        $this->setField($field);
36
        foreach ($ranges as $range) {
37
            if (is_array($range)) {
38
                $from = isset($range['from']) ? $range['from'] : null;
39
                $to = isset($range['to']) ? $range['to'] : null;
40
                $this->addRange($from, $to);
41
            } else {
42
                $this->addMask($range);
43
            }
44
        }
45
    }
46
47
    /**
48
     * Add range to aggregation.
49
     *
50
     * @param string|null $from
51
     * @param string|null $to
52
     *
53
     * @return Ipv4RangeAggregation
54
     */
55
    public function addRange(?string $from = null, ?string $to = null): static
56
    {
57
        $range = array_filter(
58
            [
59
                'from' => $from,
60
                'to' => $to,
61
            ]
62
        );
63
64
        $this->ranges[] = $range;
65
66
        return $this;
67
    }
68
69
    public function addMask(string $mask): static
70
    {
71
        $this->ranges[] = ['mask' => $mask];
72
73
        return $this;
74
    }
75
76
    public function getType(): string
77
    {
78
        return 'ip_range';
79
    }
80
81
    public function getArray(): array
82
    {
83
        if ($this->getField() && !empty($this->ranges)) {
84
            return [
85
                'field' => $this->getField(),
86
                'ranges' => array_values($this->ranges),
87
            ];
88
        }
89
        throw new \LogicException('Ip range aggregation must have field set and range added.');
90
    }
91
}
92