Completed
Pull Request — master (#154)
by
unknown
03:47
created

NestedInnerHit::toArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
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\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 BuilderInterface
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->setQuery($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 BuilderInterface
75
     */
76
    public function getQuery()
77
    {
78
        return $this->query;
79
    }
80
81
    /**
82
     * @param Search $query
83
     *
84
     * @return $this
85
     */
86
    public function setQuery(Search $query = null)
87
    {
88
        $this->query = $query;
0 ignored issues
show
Documentation Bug introduced by
It seems like $query can also be of type object<ONGR\ElasticsearchDSL\Search>. However, the property $query is declared as type object<ONGR\ElasticsearchDSL\BuilderInterface>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
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->getQuery() ? $this->getQuery()->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