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\Sort; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
15
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Holds all the values required for basic sorting. |
19
|
|
|
*/ |
20
|
|
|
class GeoDistanceSort implements BuilderInterface |
21
|
|
|
{ |
22
|
|
|
use ParametersTrait; |
23
|
|
|
|
24
|
|
|
const ASC = 'asc'; |
25
|
|
|
const DESC = 'desc'; |
26
|
|
|
|
27
|
|
|
const ARC = 'arc'; |
28
|
|
|
const PLANE = 'plane'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $field; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $order; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $distanceType; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $location; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var BuilderInterface |
52
|
|
|
*/ |
53
|
|
|
private $nestedFilter; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $field Field name. |
57
|
|
|
* @param string|array $location The location to measure the distance from. The possible representations can be found here: https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html |
58
|
|
|
* @param string $order Order direction. |
59
|
|
|
* @param array $params Params that can be set to field sort. |
60
|
|
|
*/ |
61
|
|
|
public function __construct($field, $location, $order = null, $params = []) |
62
|
|
|
{ |
63
|
|
|
$this->field = $field; |
64
|
|
|
$this->order = $order; |
65
|
|
|
$this->location = $location; |
|
|
|
|
66
|
|
|
$this->setParameters($params); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function getField() |
73
|
|
|
{ |
74
|
|
|
return $this->field; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $field |
79
|
|
|
* |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function setField($field) |
83
|
|
|
{ |
84
|
|
|
$this->field = $field; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getOrder() |
93
|
|
|
{ |
94
|
|
|
return $this->order; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $order |
99
|
|
|
* |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function setOrder($order) |
103
|
|
|
{ |
104
|
|
|
$this->order = $order; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return BuilderInterface |
111
|
|
|
*/ |
112
|
|
|
public function getNestedFilter() |
113
|
|
|
{ |
114
|
|
|
return $this->nestedFilter; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param BuilderInterface $nestedFilter |
119
|
|
|
* |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function setNestedFilter(BuilderInterface $nestedFilter) |
123
|
|
|
{ |
124
|
|
|
$this->nestedFilter = $nestedFilter; |
125
|
|
|
|
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
|
|
public function getDistanceType() |
133
|
|
|
{ |
134
|
|
|
return $this->distanceType; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $distanceType |
139
|
|
|
* @return GeoSort |
140
|
|
|
*/ |
141
|
|
|
public function setDistanceType($distanceType) |
142
|
|
|
{ |
143
|
|
|
$this->distanceType = $distanceType; |
144
|
|
|
|
145
|
|
|
return $this; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function getLocation() |
152
|
|
|
{ |
153
|
|
|
return $this->location; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param string $location |
158
|
|
|
* @return GeoSort |
159
|
|
|
*/ |
160
|
|
|
public function setLocation($location) |
161
|
|
|
{ |
162
|
|
|
$this->location = $location; |
163
|
|
|
|
164
|
|
|
return $this; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Returns element type. |
169
|
|
|
* |
170
|
|
|
* @return string |
171
|
|
|
*/ |
172
|
|
|
public function getType() |
173
|
|
|
{ |
174
|
|
|
return 'sort'; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritdoc} |
179
|
|
|
*/ |
180
|
|
|
public function toArray() |
181
|
|
|
{ |
182
|
|
|
if ($this->field) { |
183
|
|
|
$this->addParameter($this->field, $this->location); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
if ($this->order) { |
187
|
|
|
$this->addParameter('order', $this->order); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
if ($this->nestedFilter) { |
191
|
|
|
$this->addParameter('nested', $this->nestedFilter->toArray()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$output = [ |
195
|
|
|
'_geo_distance' => !$this->getParameters() ? new \stdClass() : $this->getParameters(), |
196
|
|
|
]; |
197
|
|
|
|
198
|
|
|
return $output; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
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 theid
property of an instance of theAccount
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.