Completed
Pull Request — master (#555)
by Mantas
30:23
created

ValueAggregation::getValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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\ElasticsearchBundle\Result\Aggregation;
13
14
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
15
16
/**
17
 * This is the class for plain aggregation result with nested aggregations support.
18
 */
19
class ValueAggregation implements \ArrayAccess, \IteratorAggregate
20
{
21
    /**
22
     * @var array
23
     */
24
    private $rawData;
25
26
    /**
27
     * Constructor.
28
     *
29
     * @param array $rawData
30
     */
31
    public function __construct($rawData)
32
    {
33
        $this->rawData = $rawData;
34
    }
35
36
    /**
37
     * Returns aggregation value by name.
38
     *
39
     * @param string $name
40
     *
41
     * @return array
42
     */
43
    public function getValue($name)
44
    {
45
        if (!isset($this->rawData[$name])) {
46
            return null;
47
        }
48
49
        return $this->rawData[$name];
50
    }
51
52
    /**
53
     * Returns array of bucket values.
54
     *
55
     * @return ValueAggregation[]|null
56
     */
57
    public function getBuckets()
58
    {
59
        if (!isset($this->rawData['buckets'])) {
60
            return null;
61
        }
62
63
        $buckets = [];
64
65
        foreach ($this->rawData['buckets'] as $bucket) {
66
            $buckets[] = new ValueAggregation($bucket);
67
        }
68
69
        return $buckets;
70
    }
71
72
    /**
73
     * Returns sub-aggregation.
74
     *
75
     * @param string $name
76
     *
77
     * @return ValueAggregation|null
78
     */
79
    public function getAggregation($name)
80
    {
81
        if (!isset($this->rawData[$name])) {
82
            return null;
83
        }
84
85
        return new ValueAggregation($this->rawData[$name]);
86
    }
87
88
    /**
89
     * Applies path method to aggregations.
90
     *
91
     * @param string $path
92
     *
93
     * @return ValueAggregation|null
94
     */
95
    public function find($path)
96
    {
97
        $name = explode('.', $path, 2);
98
        $aggregation = $this->getAggregation($name[0]);
99
100
        if ($aggregation === null || !isset($name[1])) {
101
            return $aggregation;
102
        }
103
104
        return $aggregation->find($name[1]);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function offsetExists($offset)
111
    {
112
        return array_key_exists($offset, $this->rawData);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
    public function offsetGet($offset)
119
    {
120
        if (!isset($this->rawData[$offset])) {
121
            return null;
122
        }
123
124
        return $this->rawData[$offset];
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function offsetSet($offset, $value)
131
    {
132
        throw new \LogicException('Aggregation result can not be changed on runtime.');
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function offsetUnset($offset)
139
    {
140
        throw new \LogicException('Aggregation result can not be changed on runtime.');
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function getIterator()
147
    {
148
        $buckets = $this->getBuckets();
149
150
        if ($buckets === null) {
151
            throw new \LogicException('Can not iterate over aggregation without buckets!');
152
        }
153
154
        return new \ArrayIterator($this->getBuckets());
155
    }
156
}
157