Completed
Pull Request — master (#154)
by
unknown
04:14 queued 57s
created

NestedInnerHit   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 5
Bugs 1 Features 1
Metric Value
wmc 11
c 5
b 1
f 1
lcom 1
cbo 3
dl 0
loc 113
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getPath() 0 4 1
A setPath() 0 6 1
A getPathType() 0 14 3
A getSearch() 0 4 1
A setSearch() 0 6 1
A getType() 0 4 1
A toArray() 0 12 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\ElasticsearchDSL\InnerHit;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\NameAwareTrait;
16
use ONGR\ElasticsearchDSL\ParametersTrait;
17
use ONGR\ElasticsearchDSL\Search;
18
19
/**
20
 * Represents Elasticsearch top level nested inner hits.
21
 *
22
 * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html
23
 */
24
class NestedInnerHit implements BuilderInterface
25
{
26
    use ParametersTrait;
27
    use NameAwareTrait;
28
29
    /**
30
     * @var string
31
     */
32
    private $path;
33
34
    /**
35
     * @var Search
36
     */
37
    private $query;
38
39
    /**
40
     * Inner hits container init.
41
     *
42
     * @param string $name
43
     * @param string $path
44
     * @param Search $query
45
     */
46
    public function __construct($name, $path, Search $query = null)
47
    {
48
        $this->setName($name);
49
        $this->setPath($path);
50
        $this->setSearch($query);
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getPath()
57
    {
58
        return $this->path;
59
    }
60
61
    /**
62
     * @param string $path
63
     *
64
     * @return $this
65
     */
66
    public function setPath($path)
67
    {
68
        $this->path = $path;
69
70
        return $this;
71
    }
72
73
    /**
74
     * @return Search
75
     */
76
    public function getSearch()
77
    {
78
        return $this->query;
79
    }
80
81
    /**
82
     * @param Search $query
83
     *
84
     * @return $this
85
     */
86
    public function setSearch(Search $query = null)
87
    {
88
        $this->query = $query;
89
90
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function getType()
97
    {
98
        return 'nested';
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function toArray()
105
    {
106
        $out = $this->getSearch() ? $this->getSearch()->toArray() : new \stdClass();
107
108
        $out = [
109
            $this->getPathType() => [
110
                $this->getPath() => $out ,
111
            ],
112
        ];
113
114
        return $out;
115
    }
116
117
    /**
118
     * Returns 'path' for nested and 'type' for parent inner hits
119
     *
120
     * @return null|string
121
     */
122
    private function getPathType()
123
    {
124
        switch ($this->getType()) {
125
            case 'nested':
126
                $type = 'path';
127
                break;
128
            case 'parent':
129
                $type = 'type';
130
                break;
131
            default:
132
                $type = null;
133
        }
134
        return $type;
135
    }
136
}
137