Completed
Pull Request — master (#555)
by Mantas
02:53
created

ValueAggregation::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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 View Code Duplication
    public function getAggregation($name)
0 ignored issues
show
Duplication introduced by
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...
80
    {
81
        // TODO: remove this *** after DSL update
82
        $name = AbstractAggregation::PREFIX . $name;
83
84
        if (!isset($this->rawData[$name])) {
85
            return null;
86
        }
87
88
        return new ValueAggregation($this->rawData[$name]);
89
    }
90
91
    /**
92
     * Applies path method to aggregations.
93
     *
94
     * @param string $path
95
     *
96
     * @return ValueAggregation|null
97
     */
98
    public function find($path)
99
    {
100
        $name = explode('.', $path, 2);
101
        $aggregation = $this->getAggregation($name[0]);
102
103
        if ($aggregation === null || !isset($name[1])) {
104
            return $aggregation;
105
        }
106
107
        return $aggregation->find($name[1]);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function offsetExists($offset)
114
    {
115
        return array_key_exists($offset, $this->rawData);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function offsetGet($offset)
122
    {
123
        if (!isset($this->rawData[$offset])) {
124
            return null;
125
        }
126
127
        return $this->rawData[$offset];
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function offsetSet($offset, $value)
134
    {
135
        throw new \LogicException('Aggregation result can not be changed on runtime.');
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function offsetUnset($offset)
142
    {
143
        throw new \LogicException('Aggregation result can not be changed on runtime.');
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function getIterator()
150
    {
151
        $buckets = $this->getBuckets();
152
153
        if ($buckets === null) {
154
            throw new \LogicException('Can not iterate over aggregation without buckets!');
155
        }
156
157
        return new \ArrayIterator($this->getBuckets());
158
    }
159
}
160