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\BuilderBag; |
15
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
16
|
|
|
use ONGR\ElasticsearchDSL\NameAwareTrait; |
17
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* AbstractAggregation class. |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractInnerHit implements BuilderInterface |
23
|
|
|
{ |
24
|
|
|
use ParametersTrait; |
25
|
|
|
use NameAwareTrait; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $path; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var BuilderInterface |
34
|
|
|
*/ |
35
|
|
|
private $query; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var BuilderBag |
39
|
|
|
*/ |
40
|
|
|
private $innerHits; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
abstract public function toArray(); |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
|
|
abstract public function getType(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Inner hits container init. |
54
|
|
|
* |
55
|
|
|
* @param string $name |
56
|
|
|
* @param string $path |
57
|
|
|
* @param BuilderInterface $query |
58
|
|
|
*/ |
59
|
|
|
public function __construct($name, $path, BuilderInterface $query) |
60
|
|
|
{ |
61
|
|
|
$this->setName($name); |
62
|
|
|
$this->setPath($path); |
63
|
|
|
$this->setQuery($query); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function getPath() |
70
|
|
|
{ |
71
|
|
|
return $this->path; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $path |
76
|
|
|
*/ |
77
|
|
|
public function setPath($path) |
78
|
|
|
{ |
79
|
|
|
$this->path = $path; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return BuilderInterface |
84
|
|
|
*/ |
85
|
|
|
public function getQuery() |
86
|
|
|
{ |
87
|
|
|
return $this->query; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param BuilderInterface $query |
92
|
|
|
*/ |
93
|
|
|
public function setQuery(BuilderInterface $query) |
94
|
|
|
{ |
95
|
|
|
$this->query = $query; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Adds a sub-innerHit. |
100
|
|
|
* |
101
|
|
|
* @param AbstractInnerHit $abstractInnerHit |
102
|
|
|
*/ |
103
|
|
|
public function addInnerHit(AbstractInnerHit $abstractInnerHit) |
104
|
|
|
{ |
105
|
|
|
if (!$this->innerHits) { |
106
|
|
|
$this->innerHits = new BuilderBag(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$this->innerHits->add($abstractInnerHit); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns all sub inner hits. |
114
|
|
|
* |
115
|
|
|
* @return BuilderInterface[] |
116
|
|
|
*/ |
117
|
|
|
public function getInnerHits() |
118
|
|
|
{ |
119
|
|
|
if ($this->innerHits) { |
120
|
|
|
return $this->innerHits->all(); |
121
|
|
|
} else { |
122
|
|
|
return []; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Returns sub inner hit. |
128
|
|
|
* @param string $name inner hit name to return. |
129
|
|
|
* |
130
|
|
|
* @return AbstractInnerHit|null |
131
|
|
|
*/ |
132
|
|
|
public function getInnerHit($name) |
133
|
|
|
{ |
134
|
|
|
if ($this->innerHits && $this->innerHits->has($name)) { |
135
|
|
|
return $this->innerHits->get($name); |
136
|
|
|
} else { |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Process all nested inner hits. |
143
|
|
|
* |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
public function collectNestedInnerHits() |
147
|
|
|
{ |
148
|
|
|
$result = []; |
149
|
|
|
/** @var AbstractInnerHit $innerHit */ |
150
|
|
|
foreach ($this->getInnerHits() as $innerHit) { |
151
|
|
|
$result[$innerHit->getName()] = $innerHit->toArray(); |
|
|
|
|
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $result; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: