Completed
Push — master ( 896392...7af673 )
by Simonas
02:55
created

AggregationValue::getCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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