AggregationValue   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 0
dl 0
loc 114
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getValue() 0 8 2
A getCount() 0 4 1
A getBuckets() 0 14 3
A getAggregation() 0 8 2
A find() 0 11 3
A offsetExists() 0 4 1
A offsetGet() 0 8 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
A getIterator() 0 10 2
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 class represent`s aggregation buckets in the objective way.
16
 */
17
class AggregationValue implements \ArrayAccess, \IteratorAggregate
18
{
19
    const BUCKETS_KEY = 'buckets';
20
    const DOC_COUNT_KEY = 'doc_count';
21
22
    /**
23
     * @var array
24
     */
25
    private $rawData;
26
27
    public function __construct(array $rawData)
28
    {
29
        $this->rawData = $rawData;
30
    }
31
32
    /**
33
     * Returns aggregation value by the aggregation name.
34
     */
35
    public function getValue(string $name)
36
    {
37
        if (!isset($this->rawData[$name])) {
38
            return null;
39
        }
40
41
        return $this->rawData[$name];
42
    }
43
44
    public function getCount(): int
45
    {
46
        return (int) $this->getValue(self::DOC_COUNT_KEY);
47
    }
48
49
    /**
50
     * Returns array of bucket values.
51
     *
52
     * @return AggregationValue[]
53
     */
54
    public function getBuckets(): array
55
    {
56
        if (!isset($this->rawData[self::BUCKETS_KEY])) {
57
            return [];
58
        }
59
60
        $buckets = [];
61
62
        foreach ($this->rawData[self::BUCKETS_KEY] as $bucket) {
63
            $buckets[] = new self($bucket);
64
        }
65
66
        return $buckets;
67
    }
68
69
    /**
70
     * Returns sub-aggregation.
71
     */
72
    public function getAggregation(string $name): ?self
73
    {
74
        if (!isset($this->rawData[$name])) {
75
            return null;
76
        }
77
78
        return new self($this->rawData[$name]);
79
    }
80
81
    /**
82
     * Search'es the aggregation by defined path.
83
     */
84
    public function find(string $path): ?self
85
    {
86
        $name = explode('.', $path, 2);
87
        $aggregation = $this->getAggregation($name[0]);
88
89
        if ($aggregation === null || !isset($name[1])) {
90
            return $aggregation;
91
        }
92
93
        return $aggregation->find($name[1]);
94
    }
95
96
    public function offsetExists($offset)
97
    {
98
        return array_key_exists($offset, $this->rawData);
99
    }
100
101
    public function offsetGet($offset)
102
    {
103
        if (!isset($this->rawData[$offset])) {
104
            return null;
105
        }
106
107
        return $this->rawData[$offset];
108
    }
109
110
    public function offsetSet($offset, $value)
111
    {
112
        throw new \LogicException('Aggregation result can not be changed on runtime.');
113
    }
114
115
    public function offsetUnset($offset)
116
    {
117
        throw new \LogicException('Aggregation result can not be changed on runtime.');
118
    }
119
120
    public function getIterator(): \ArrayIterator
121
    {
122
        $buckets = $this->getBuckets();
123
124
        if ($buckets === null) {
125
            throw new \LogicException('Can not iterate over aggregation without buckets!');
126
        }
127
128
        return new \ArrayIterator($this->getBuckets());
129
    }
130
}
131