Completed
Push — master ( 62f765...d71e72 )
by Simonas
15:54
created

src/Aggregation/Bucketing/Ipv4RangeAggregation.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 ip range aggregation.
19
 *
20
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-iprange-aggregation.html
21
 */
22
class Ipv4RangeAggregation extends AbstractAggregation
23
{
24
    use BucketingTrait;
25
26
    /**
27
     * @var array
28
     */
29
    private $ranges = [];
30
31
    /**
32
     * Inner aggregations container init.
33
     *
34
     * @param string $name
35
     * @param string $field
36
     * @param array  $ranges
37
     */
38 View Code Duplication
    public function __construct($name, $field = null, $ranges = [])
0 ignored issues
show
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...
39
    {
40
        parent::__construct($name);
41
42
        $this->setField($field);
43
        foreach ($ranges as $range) {
44
            if (is_array($range)) {
45
                $from = isset($range['from']) ? $range['from'] : null;
46
                $to = isset($range['to']) ? $range['to'] : null;
47
                $this->addRange($from, $to);
48
            } else {
49
                $this->addMask($range);
50
            }
51
        }
52
    }
53
54
    /**
55
     * Add range to aggregation.
56
     *
57
     * @param string|null $from
58
     * @param string|null $to
59
     *
60
     * @return Ipv4RangeAggregation
61
     */
62 View Code Duplication
    public function addRange($from = null, $to = null)
63
    {
64
        $range = array_filter(
65
            [
66
                'from' => $from,
67
                'to' => $to,
68
            ]
69
        );
70
71
        $this->ranges[] = $range;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Add ip mask to aggregation.
78
     *
79
     * @param string $mask
80
     *
81
     * @return Ipv4RangeAggregation
82
     */
83
    public function addMask($mask)
84
    {
85
        $this->ranges[] = ['mask' => $mask];
86
87
        return $this;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getType()
94
    {
95
        return 'ip_range';
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function getArray()
102
    {
103
        if ($this->getField() && !empty($this->ranges)) {
104
            return [
105
                'field' => $this->getField(),
106
                'ranges' => array_values($this->ranges),
107
            ];
108
        }
109
        throw new \LogicException('Ip range aggregation must have field set and range added.');
110
    }
111
}
112