Skip to content

Commit 01f8fe8

Browse files
tw99saimaz
authored andcommitted
GeoBoundingBoxQuery supports keyed values (#292)
1 parent 2bcaa12 commit 01f8fe8

File tree

2 files changed

+58
-17
lines changed

2 files changed

+58
-17
lines changed

src/Query/Geo/GeoBoundingBoxQuery.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,33 @@ public function getType()
5757
* {@inheritdoc}
5858
*/
5959
public function toArray()
60+
{
61+
return [
62+
$this->getType() => $this->processArray([$this->field => $this->points()])
63+
];
64+
}
65+
66+
/**
67+
* @return array
68+
*
69+
* @throws \LogicException
70+
*/
71+
private function points()
6072
{
6173
if (count($this->values) === 2) {
62-
$query = [
63-
$this->field => [
64-
'top_left' => $this->values[0],
65-
'bottom_right' => $this->values[1],
66-
],
74+
return [
75+
'top_left' => $this->values[0] ?? $this->values['top_left'],
76+
'bottom_right' => $this->values[1] ?? $this->values['bottom_right'],
6777
];
6878
} elseif (count($this->values) === 4) {
69-
$query = [
70-
$this->field => [
71-
'top' => $this->values[0],
72-
'left' => $this->values[1],
73-
'bottom' => $this->values[2],
74-
'right' => $this->values[3],
75-
],
79+
return [
80+
'top' => $this->values[0] ?? $this->values['top'],
81+
'left' => $this->values[1] ?? $this->values['left'],
82+
'bottom' => $this->values[2] ?? $this->values['bottom'],
83+
'right' => $this->values[3] ?? $this->values['right'],
7684
];
77-
} else {
78-
throw new \LogicException('Geo Bounding Box filter must have 2 or 4 geo points set.');
7985
}
8086

81-
$output = $this->processArray($query);
82-
83-
return [$this->getType() => $output];
87+
throw new \LogicException('Geo Bounding Box filter must have 2 or 4 geo points set.');
8488
}
8589
}

tests/Unit/Query/Geo/GeoBoundingBoxQueryTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ public function getArrayDataProvider()
5050
'parameter' => 'value',
5151
],
5252
],
53+
// Case #2 (2 values with keys).
54+
[
55+
'location',
56+
[
57+
'bottom_right' => ['lat' => 40.01, 'lon' => -71.12],
58+
'top_left' => ['lat' => 40.73, 'lon' => -74.1],
59+
],
60+
['parameter' => 'value'],
61+
[
62+
'location' => [
63+
'top_left' => ['lat' => 40.73, 'lon' => -74.1],
64+
'bottom_right' => ['lat' => 40.01, 'lon' => -71.12],
65+
],
66+
'parameter' => 'value',
67+
],
68+
],
5369
// Case #2 (4 values).
5470
[
5571
'location',
@@ -65,6 +81,27 @@ public function getArrayDataProvider()
6581
'parameter' => 'value',
6682
],
6783
],
84+
// Case #3 (4 values with keys).
85+
[
86+
'location',
87+
[
88+
// out of order
89+
'right' => -71.12,
90+
'bottom' => 40.01,
91+
'top' => 40.73,
92+
'left' => -74.1
93+
],
94+
['parameter' => 'value'],
95+
[
96+
'location' => [
97+
'top' => 40.73,
98+
'left' => -74.1,
99+
'bottom' => 40.01,
100+
'right' => -71.12,
101+
],
102+
'parameter' => 'value',
103+
],
104+
],
68105
];
69106
}
70107

0 commit comments

Comments
 (0)