-
Notifications
You must be signed in to change notification settings - Fork 20
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
Showing
2 changed files
with
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Erichard\ElasticQueryBuilder\Entities; | ||
|
||
class GpsPointEntity | ||
{ | ||
public function __construct( | ||
public float $lat, | ||
public float $lon | ||
) { | ||
} | ||
} |
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,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Erichard\ElasticQueryBuilder\Query; | ||
|
||
use Erichard\ElasticQueryBuilder\Contracts\QueryInterface; | ||
use Erichard\ElasticQueryBuilder\Entities\GpsPointEntity; | ||
use Erichard\ElasticQueryBuilder\Features\HasField; | ||
|
||
/** | ||
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html | ||
*/ | ||
class GeoBoundingBoxQuery implements QueryInterface | ||
{ | ||
use HasField; | ||
|
||
public function __construct( | ||
string $field, | ||
private GpsPointEntity $topLeft, | ||
private GpsPointEntity $bottomRight | ||
) { | ||
$this->field = $field; | ||
} | ||
|
||
public function build(): array | ||
{ | ||
return [ | ||
'geo_bounding_box' => [ | ||
$this->field => [ | ||
'top_left' => $this->pointToArray($this->topLeft), | ||
'bottom_right' => $this->pointToArray($this->bottomRight), | ||
], | ||
], | ||
]; | ||
} | ||
|
||
protected function pointToArray(GpsPointEntity $entity): array | ||
{ | ||
return [ | ||
'lat' => $entity->lat, | ||
'lon' => $entity->lon, | ||
]; | ||
} | ||
} |