Skip to content
Snippets Groups Projects
Commit 01f8fe81 authored by Timothy Winters's avatar Timothy Winters Committed by Simonas Šerlinskas
Browse files

GeoBoundingBoxQuery supports keyed values (#292)

parent 2bcaa125
No related branches found
No related tags found
No related merge requests found
......@@ -57,29 +57,33 @@ class GeoBoundingBoxQuery implements BuilderInterface
* {@inheritdoc}
*/
public function toArray()
{
return [
$this->getType() => $this->processArray([$this->field => $this->points()])
];
}
/**
* @return array
*
* @throws \LogicException
*/
private function points()
{
if (count($this->values) === 2) {
$query = [
$this->field => [
'top_left' => $this->values[0],
'bottom_right' => $this->values[1],
],
return [
'top_left' => $this->values[0] ?? $this->values['top_left'],
'bottom_right' => $this->values[1] ?? $this->values['bottom_right'],
];
} elseif (count($this->values) === 4) {
$query = [
$this->field => [
'top' => $this->values[0],
'left' => $this->values[1],
'bottom' => $this->values[2],
'right' => $this->values[3],
],
return [
'top' => $this->values[0] ?? $this->values['top'],
'left' => $this->values[1] ?? $this->values['left'],
'bottom' => $this->values[2] ?? $this->values['bottom'],
'right' => $this->values[3] ?? $this->values['right'],
];
} else {
throw new \LogicException('Geo Bounding Box filter must have 2 or 4 geo points set.');
}
$output = $this->processArray($query);
return [$this->getType() => $output];
throw new \LogicException('Geo Bounding Box filter must have 2 or 4 geo points set.');
}
}
......@@ -50,6 +50,22 @@ class GeoBoundingBoxQueryTest extends \PHPUnit\Framework\TestCase
'parameter' => 'value',
],
],
// Case #2 (2 values with keys).
[
'location',
[
'bottom_right' => ['lat' => 40.01, 'lon' => -71.12],
'top_left' => ['lat' => 40.73, 'lon' => -74.1],
],
['parameter' => 'value'],
[
'location' => [
'top_left' => ['lat' => 40.73, 'lon' => -74.1],
'bottom_right' => ['lat' => 40.01, 'lon' => -71.12],
],
'parameter' => 'value',
],
],
// Case #2 (4 values).
[
'location',
......@@ -65,6 +81,27 @@ class GeoBoundingBoxQueryTest extends \PHPUnit\Framework\TestCase
'parameter' => 'value',
],
],
// Case #3 (4 values with keys).
[
'location',
[
// out of order
'right' => -71.12,
'bottom' => 40.01,
'top' => 40.73,
'left' => -74.1
],
['parameter' => 'value'],
[
'location' => [
'top' => 40.73,
'left' => -74.1,
'bottom' => 40.01,
'right' => -71.12,
],
'parameter' => 'value',
],
],
];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment