Completed
Pull Request — master (#348)
by
unknown
09:42 queued 08:20
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
    private array $ranges = [];
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_ARRAY, expecting T_FUNCTION or T_CONST
Loading history...
29
30
    public function __construct(
31
        private string $name,
32
        private ?string $field = null,
33
        array $ranges = []
34
    ) {
35
        parent::__construct($name);
36
37
        $this->setField($field);
38
        foreach ($ranges as $range) {
39
            if (is_array($range)) {
40
                $from = isset($range['from']) ? $range['from'] : null;
41
                $to = isset($range['to']) ? $range['to'] : null;
42
                $this->addRange($from, $to);
43
            } else {
44
                $this->addMask($range);
45
            }
46
        }
47
    }
48
49
    /**
50
     * Add range to aggregation.
51
     *
52
     * @param string|null $from
53
     * @param string|null $to
54
     *
55
     * @return Ipv4RangeAggregation
56
     */
57
    public function addRange(?string $from = null, ?string $to = null): static
58
    {
59
        $range = array_filter(
60
            [
61
                'from' => $from,
62
                'to' => $to,
63
            ]
64
        );
65
66
        $this->ranges[] = $range;
67
68
        return $this;
69
    }
70
71
    public function addMask(string $mask): static
72
    {
73
        $this->ranges[] = ['mask' => $mask];
74
75
        return $this;
76
    }
77
78
    public function getType(): string
79
    {
80
        return 'ip_range';
81
    }
82
83
    public function getArray(): array
84
    {
85
        if ($this->getField() && !empty($this->ranges)) {
86
            return [
87
                'field' => $this->getField(),
88
                'ranges' => array_values($this->ranges),
89
            ];
90
        }
91
        throw new \LogicException('Ip range aggregation must have field set and range added.');
92
    }
93
}
94