Completed
Pull Request — master (#663)
by
unknown
02:37
created

DocumentIterator::getInnerHits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
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;
13
14
use ONGR\ElasticsearchBundle\Result\Aggregation\AggregationValue;
15
use ONGR\ElasticsearchBundle\Service\Manager;
16
17
/**
18
 * Class DocumentIterator.
19
 */
20
class DocumentIterator extends AbstractResultsIterator
21
{
22
    /**
23
     * @var array
24
     */
25
    private $aggregations;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
26
27
    /**
28
     * @var array
29
     */
30
    private $inner_hits;
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function __construct(array $rawData, Manager $manager, array $scroll = [])
36
    {
37
        if (isset($rawData['aggregations'])) {
38
            $this->aggregations = $rawData['aggregations'];
39
            unset($rawData['aggregations']);
40
        }
41
42
        if (isset($rawData['hits']['hits'][0]['inner_hits'])) {
43
            foreach ($rawData['hits']['hits'] as $item) {
44
                $this->inner_hits[$item['_id']] = $item['inner_hits'];
45
            }
46
        }
47
48
        parent::__construct($rawData, $manager, $scroll);
49
    }
50
51
    /**
52
     * Returns aggregations.
53
     *
54
     * @return array
55
     */
56
    public function getAggregations()
57
    {
58
        $aggregations = array();
59
60
        foreach ($this->aggregations as $key => $aggregation) {
61
            $aggregations[$key] = $this->getAggregation($key);
62
        }
63
64
        return $aggregations;
65
    }
66
67
    /**
68
     * Get a specific aggregation by name. It fetches from the top level only.
69
     *
70
     * @param string $name
71
     *
72
     * @return AggregationValue|null
73
     */
74
    public function getAggregation($name)
75
    {
76
        if (!isset($this->aggregations[$name])) {
77
            return null;
78
        }
79
80
        return new AggregationValue($this->aggregations[$name]);
81
    }
82
83
    /**
84
     * Returns inner hits for all objects
85
     *
86
     * @return InnerHitValue[]
87
     */
88
    public function getInnerHits()
89
    {
90
        $hits = [];
91
92
        foreach ($this->inner_hits as $id => $hit) {
93
            $hits[$id] = new InnerHitValue($hit, $this->getManager());
94
        }
95
        return $hits;
96
    }
97
98
    /**
99
     * Returns inner hits for all objects
100
     *
101
     * @param string $id
102
     *
103
     * @return InnerHitValue
104
     */
105
    public function getInnerHit($id)
106
    {
107
        if (!isset($this->inner_hits[$id])) {
108
            return null;
109
        }
110
111
        return new InnerHitValue($this->inner_hits[$id], $this->getManager());
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    protected function convertDocument(array $document)
118
    {
119
        return $this->getConverter()->convertToDocument($document, $this->getManager());
120
    }
121
}
122