Completed
Push — master ( 2c549d...b71b08 )
by Simonas
8s
created

NestedInnerHit::setSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
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\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 $search;
38
39
    /**
40
     * Inner hits container init.
41
     *
42
     * @param string $name
43
     * @param string $path
44
     * @param Search $search
45
     */
46
    public function __construct($name, $path, Search $search = null)
47
    {
48
        $this->setName($name);
49
        $this->setPath($path);
50
        if ($search) {
51
            $this->setSearch($search);
52
        }
53
    }
54
55
    /**
56
     * @return string
57
     */
58
    public function getPath()
59
    {
60
        return $this->path;
61
    }
62
63
    /**
64
     * @param string $path
65
     *
66
     * @return $this
67
     */
68
    public function setPath($path)
69
    {
70
        $this->path = $path;
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return Search
77
     */
78
    public function getSearch()
79
    {
80
        return $this->search;
81
    }
82
83
    /**
84
     * @param Search $search
85
     *
86
     * @return $this
87
     */
88
    public function setSearch(Search $search)
89
    {
90
        $this->search = $search;
91
92
        return $this;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function getType()
99
    {
100
        return 'nested';
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public function toArray()
107
    {
108
        $out = $this->getSearch() ? $this->getSearch()->toArray() : new \stdClass();
109
110
        $out = [
111
            $this->getPathType() => [
112
                $this->getPath() => $out ,
113
            ],
114
        ];
115
116
        return $out;
117
    }
118
119
    /**
120
     * Returns 'path' for nested and 'type' for parent inner hits
121
     *
122
     * @return null|string
123
     */
124
    private function getPathType()
125
    {
126
        switch ($this->getType()) {
127
            case 'nested':
128
                $type = 'path';
129
                break;
130
            case 'parent':
131
                $type = 'type';
132
                break;
133
            default:
134
                $type = null;
135
        }
136
        return $type;
137
    }
138
}
139