AggregationCollection::toArray()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 2
nc 2
nop 0
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Aggregation;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
5
class AggregationCollection implements \Countable, \IteratorAggregate, Arrayable
6
{
7
    /**
8
     * @var Aggregation[]
9
     */
10
    private $aggregations = [];
11
12
13
    /**
14
     * @param array $aggregations
15
     */
16
    public function __construct(array $aggregations = [])
17
    {
18
        foreach ($aggregations as $aggregation) {
19
            $this->add($aggregation);
20
        }
21
    }
22
23
24
    /**
25
     * @return array
26
     */
27
    public function toArray()
28
    {
29
        $array = [];
30
31
        foreach ($this->aggregations as $aggregation) {
32
            $array[$aggregation->getName()] = $aggregation->toArray();
33
        }
34
35
        return $array;
36
    }
37
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function count()
43
    {
44
        return count($this->aggregations);
45
    }
46
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function getIterator()
52
    {
53
        return new \ArrayIterator($this->aggregations);
54
    }
55
56
57
    /**
58
     * @param Aggregation $aggregation
59
     * @return AggregationCollection
60
     */
61
    public function add(Aggregation $aggregation)
62
    {
63
        $this->aggregations[] = $aggregation;
64
        return $this;
65
    }
66
67
68
    /**
69
     * @param int $index
70
     * @return Aggregation|null
71
     */
72
    public function remove($index)
73
    {
74
        if (!isset($this->aggregations[$index])) {
75
            return null;
76
        }
77
78
        $removed = $this->aggregations[$index];
79
        unset($this->aggregations[$index]);
80
81
        return $removed;
82
    }
83
84
85
    /**
86
     * @param int $index
87
     * @return Aggregation|null
88
     */
89
    public function get($index)
90
    {
91
        return $this->aggregations[$index] ?? null;
92
    }
93
}
94