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

InnerHitValue::getInnerHits()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
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;
13
14
use ONGR\ElasticsearchBundle\Service\Manager;
15
16
/**
17
 * This is the class for inner hit result with nested support.
18
 */
19
class InnerHitValue
20
{
21
    /**
22
     * @var array
23
     */
24
    private $rawData;
25
26
    /**
27
     * @var Manager
28
     */
29
    private $manager;
30
31
    /**
32
     * @param array   $rawData
33
     * @param Manager $manager
34
     */
35
    public function __construct($rawData, Manager $manager)
36
    {
37
        $this->rawData = $rawData;
38
        $this->manager = $manager;
39
    }
40
41
    /**
42
     * Returns array of specific inner hit objects
43
     *
44
     * @param string $name
45
     *
46
     * @return object[]
47
     */
48
    public function getValue($name)
49
    {
50
        if (!isset($this->rawData[$name])) {
51
            return null;
52
        }
53
54
        $hits = [];
55
56
        foreach ($this->rawData[$name]['hits']['hits'] as $hit) {
57
            if (isset($hit['_parent'])) {
58
                $hits[] = $this->manager->getConverter()->convertToDocument($hit, $this->manager);
59
            } else {
60
                $fields = $hit;
61
                $metadata = $this->manager
62
                    ->getMetadataCollector()
63
                    ->getMappings(
64
                        $this->manager->getConfig()['mappings']
65
                    )[$hit['_type']];
66
67
                while (isset($fields['_nested'])) {
68
                    $fields = $fields['_nested'];
69
                    $metadata = $metadata['aliases'][$fields['field']];
70
                }
71
72
                $hits[] = $this->manager->getConverter()->assignArrayToObject(
73
                    $hit['_source'],
74
                    new $metadata['namespace'],
75
                    $metadata['aliases']
76
                );
77
            }
78
        };
79
80
        return $hits;
81
    }
82
83
    /**
84
     * Returns the count of inner hits for a specific hit
85
     *
86
     * @param string $name
87
     *
88
     * @return integer
89
     */
90 View Code Duplication
    public function getCount($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...
91
    {
92
        if (!isset($this->rawData[$name])) {
93
            return null;
94
        }
95
96
        return $this->rawData[$name]['hits']['total'];
97
    }
98
99
    /**
100
     * Returns inner hits for a specified inner hit
101
     *
102
     * @param string $name
103
     *
104
     * @return InnerHitValue[]|null
105
     */
106
    public function getInnerHits($name)
107
    {
108
        if (!isset($this->rawData[$name]['hits']['hits'][0]['inner_hits'])) {
109
            return null;
110
        }
111
112
        $hits = [];
113
114
        foreach ($this->rawData[$name]['hits']['hits'] as $hit) {
115
            $hits[] = new self($hit['inner_hits'], $this->manager);
116
        }
117
118
        return $hits;
119
    }
120
}
121