-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
60686cc
commit 451b311
Showing
6 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Elastic\ScoutDriverPlus\Builders; | ||
|
||
use Elastic\ScoutDriverPlus\QueryParameters\ParameterCollection; | ||
use Elastic\ScoutDriverPlus\QueryParameters\Shared\FieldParameter; | ||
use Elastic\ScoutDriverPlus\QueryParameters\Shared\IgnoreUnmappedParameter; | ||
use Elastic\ScoutDriverPlus\QueryParameters\Shared\RelationParameter; | ||
use Elastic\ScoutDriverPlus\QueryParameters\Transformers\GroupedArrayTransformer; | ||
use Elastic\ScoutDriverPlus\QueryParameters\Validators\AllOfValidator; | ||
|
||
final class GeoShapeQueryBuilder extends AbstractParameterizedQueryBuilder | ||
{ | ||
use FieldParameter; | ||
use RelationParameter; | ||
use IgnoreUnmappedParameter; | ||
|
||
protected string $type = 'geo_shape'; | ||
|
||
public function __construct() | ||
{ | ||
$this->parameters = new ParameterCollection(); | ||
$this->parameterValidator = new AllOfValidator(['shape', 'relation']); | ||
$this->parameterTransformer = new GroupedArrayTransformer('field'); | ||
} | ||
|
||
public function shape(string $type, array $coordinates): self | ||
{ | ||
$this->parameters->put('shape', compact('type', 'coordinates')); | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Integration\Queries; | ||
|
||
use Elastic\ScoutDriverPlus\Support\Query; | ||
use Elastic\ScoutDriverPlus\Tests\App\Store; | ||
use Elastic\ScoutDriverPlus\Tests\Integration\TestCase; | ||
|
||
/** | ||
* @covers \Elastic\ScoutDriverPlus\Builders\AbstractParameterizedQueryBuilder | ||
* @covers \Elastic\ScoutDriverPlus\Builders\GeoShapeQuery | ||
* @covers \Elastic\ScoutDriverPlus\Engine | ||
* @covers \Elastic\ScoutDriverPlus\Factories\LazyModelFactory | ||
* @covers \Elastic\ScoutDriverPlus\Factories\ModelFactory | ||
* @covers \Elastic\ScoutDriverPlus\Support\Query | ||
* | ||
* @uses \Elastic\ScoutDriverPlus\Builders\DatabaseQueryBuilder | ||
* @uses \Elastic\ScoutDriverPlus\Builders\SearchParametersBuilder | ||
* @uses \Elastic\ScoutDriverPlus\Decorators\Hit | ||
* @uses \Elastic\ScoutDriverPlus\Decorators\SearchResult | ||
* @uses \Elastic\ScoutDriverPlus\Factories\DocumentFactory | ||
* @uses \Elastic\ScoutDriverPlus\Factories\ParameterFactory | ||
* @uses \Elastic\ScoutDriverPlus\Factories\RoutingFactory | ||
* @uses \Elastic\ScoutDriverPlus\QueryParameters\ParameterCollection | ||
* @uses \Elastic\ScoutDriverPlus\QueryParameters\Transformers\GroupedArrayTransformer | ||
* @uses \Elastic\ScoutDriverPlus\QueryParameters\Validators\AllOfValidator | ||
* @uses \Elastic\ScoutDriverPlus\Searchable | ||
*/ | ||
final class GeoShapeQueryTest extends TestCase | ||
{ | ||
public function test_models_can_be_found_using_field_and_shape_and_relation(): void | ||
{ | ||
// additional mixin | ||
factory(Store::class, rand(2, 10))->create([ | ||
'lat' => 20, | ||
'lon' => 20, | ||
]); | ||
|
||
$target = factory(Store::class)->create([ | ||
'lat' => 10, | ||
'lon' => 10, | ||
]); | ||
|
||
$query = Query::geoShape() | ||
->field('location') | ||
->shape('polygon', [[[0, 0], [15, 0], [15, 15], [0, 15]]]) | ||
->relation('within'); | ||
|
||
$found = Store::searchQuery($query)->execute(); | ||
|
||
$this->assertFoundModel($target, $found); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Elastic\ScoutDriverPlus\Tests\Unit\Builders; | ||
|
||
use Elastic\ScoutDriverPlus\Builders\GeoShapeQueryBuilder; | ||
use Elastic\ScoutDriverPlus\Exceptions\QueryBuilderValidationException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @covers \Elastic\ScoutDriverPlus\Builders\AbstractParameterizedQueryBuilder | ||
* @covers \Elastic\ScoutDriverPlus\Builders\GeoShapeQueryBuilder | ||
* | ||
* @uses \Elastic\ScoutDriverPlus\QueryParameters\ParameterCollection | ||
* @uses \Elastic\ScoutDriverPlus\QueryParameters\Transformers\GroupedArrayTransformer | ||
* @uses \Elastic\ScoutDriverPlus\QueryParameters\Validators\AllOfValidator | ||
*/ | ||
final class GeoShapeQueryBuilderTest extends TestCase | ||
{ | ||
private GeoShapeQueryBuilder $builder; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->builder = new GeoShapeQueryBuilder(); | ||
} | ||
|
||
public function test_exception_is_thrown_when_required_parameters_are_not_specified(): void | ||
{ | ||
$this->expectException(QueryBuilderValidationException::class); | ||
$this->builder->buildQuery(); | ||
} | ||
|
||
public function test_query_with_field_and_shape_and_relation_can_be_built(): void | ||
{ | ||
$expected = [ | ||
'geo_shape' => [ | ||
'location' => [ | ||
'shape' => [ | ||
'type' => 'envelope', | ||
'coordinates' => [[13.0, 53.0], [14.0, 52.0]], | ||
], | ||
'relation' => 'within', | ||
], | ||
], | ||
]; | ||
|
||
$actual = $this->builder | ||
->field('location') | ||
->shape('envelope', [[13.0, 53.0], [14.0, 52.0]]) | ||
->relation('within') | ||
->buildQuery(); | ||
|
||
$this->assertSame($expected, $actual); | ||
} | ||
|
||
public function test_query_with_field_and_shape_and_relation_and_ignore_unmapped_can_be_built(): void | ||
{ | ||
$expected = [ | ||
'geo_shape' => [ | ||
'location' => [ | ||
'shape' => [ | ||
'type' => 'envelope', | ||
'coordinates' => [[13.0, 53.0], [14.0, 52.0]], | ||
], | ||
'relation' => 'within', | ||
'ignore_unmapped' => true, | ||
], | ||
], | ||
]; | ||
|
||
$actual = $this->builder | ||
->field('location') | ||
->shape('envelope', [[13.0, 53.0], [14.0, 52.0]]) | ||
->relation('within') | ||
->ignoreUnmapped(true) | ||
->buildQuery(); | ||
|
||
$this->assertSame($expected, $actual); | ||
} | ||
} |