From 90c2309bf1078cfd833ac7ff28e1635bde142a95 Mon Sep 17 00:00:00 2001 From: Toni Rudolf <toni.rudolf@weekend4two.com> Date: Thu, 15 Mar 2018 11:36:03 +0100 Subject: [PATCH] implemented relation for geo_shape query (#240) * implemented relation for geo_shape query * switched position and coordinates parameters * Switched parameters for tests as well * Deprecated parameter position * Overwrite relation --- src/Query/Geo/GeoShapeQuery.php | 20 ++++++++++++++++++-- tests/Unit/Query/Geo/GeoShapeQueryTest.php | 3 ++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Query/Geo/GeoShapeQuery.php b/src/Query/Geo/GeoShapeQuery.php index 9e51537..584f335 100644 --- a/src/Query/Geo/GeoShapeQuery.php +++ b/src/Query/Geo/GeoShapeQuery.php @@ -23,6 +23,11 @@ class GeoShapeQuery implements BuilderInterface { use ParametersTrait; + const INTERSECTS = 'intersects'; + const DISJOINT = 'disjoint'; + const WITHIN = 'within'; + const CONTAINS = 'contains'; + /** * @var array */ @@ -50,10 +55,18 @@ class GeoShapeQuery implements BuilderInterface * @param string $field Field name. * @param string $type Shape type. * @param array $coordinates Shape coordinates. + * @param string $relation Spatial relation. * @param array $parameters Additional parameters. */ - public function addShape($field, $type, array $coordinates, array $parameters = []) + public function addShape($field, $type, array $coordinates, $relation = self::INTERSECTS, array $parameters = []) { + // TODO: remove this in the next major version + if (is_array($relation)) { + $parameters = $relation; + $relation = self::INTERSECTS; + trigger_error('$parameters as parameter 4 in addShape is deprecated', E_USER_DEPRECATED); + } + $filter = array_merge( $parameters, [ @@ -62,7 +75,10 @@ class GeoShapeQuery implements BuilderInterface ] ); - $this->fields[$field]['shape'] = $filter; + $this->fields[$field] = [ + 'shape' => $filter, + 'relation' => $relation, + ]; } /** diff --git a/tests/Unit/Query/Geo/GeoShapeQueryTest.php b/tests/Unit/Query/Geo/GeoShapeQueryTest.php index 9771144..054c6af 100644 --- a/tests/Unit/Query/Geo/GeoShapeQueryTest.php +++ b/tests/Unit/Query/Geo/GeoShapeQueryTest.php @@ -21,7 +21,7 @@ class GeoShapeQueryTest extends \PHPUnit_Framework_TestCase public function testToArray() { $filter = new GeoShapeQuery(['param1' => 'value1']); - $filter->addShape('location', 'envelope', [[13, 53], [14, 52]]); + $filter->addShape('location', 'envelope', [[13, 53], [14, 52]], GeoShapeQuery::INTERSECTS); $expected = [ 'geo_shape' => [ @@ -30,6 +30,7 @@ class GeoShapeQueryTest extends \PHPUnit_Framework_TestCase 'type' => 'envelope', 'coordinates' => [[13, 53], [14, 52]], ], + 'relation' => 'intersects' ], 'param1' => 'value1', ], -- GitLab