Skip to content

Commit

Permalink
brought in PR ongr-io#322
Browse files Browse the repository at this point in the history
  • Loading branch information
Lindsay Snider committed Aug 27, 2021
1 parent 5804945 commit 11417d6
Showing 1 changed file with 200 additions and 0 deletions.
200 changes: 200 additions & 0 deletions src/Sort/GeoDistanceSort.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
<?php

/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ONGR\ElasticsearchDSL\Sort;

use ONGR\ElasticsearchDSL\BuilderInterface;
use ONGR\ElasticsearchDSL\ParametersTrait;

/**
* Holds all the values required for basic sorting.
*/
class GeoDistanceSort implements BuilderInterface
{
use ParametersTrait;

const ASC = 'asc';
const DESC = 'desc';

const ARC = 'arc';
const PLANE = 'plane';

/**
* @var string
*/
private $field;

/**
* @var string
*/
private $order;

/**
* @var string
*/
private $distanceType;

/**
* @var string
*/
private $location;

/**
* @var BuilderInterface
*/
private $nestedFilter;

/**
* @param string $field Field name.
* @param string|array $location The location to measure the distance from. The possible representations can be found here: https://www.elastic.co/guide/en/elasticsearch/reference/current/geo-point.html
* @param string $order Order direction.
* @param array $params Params that can be set to field sort.
*/
public function __construct($field, $location, $order = null, $params = [])
{
$this->field = $field;
$this->order = $order;
$this->location = $location;
$this->setParameters($params);
}

/**
* @return string
*/
public function getField()
{
return $this->field;
}

/**
* @param string $field
*
* @return $this
*/
public function setField($field)
{
$this->field = $field;

return $this;
}

/**
* @return string
*/
public function getOrder()
{
return $this->order;
}

/**
* @param string $order
*
* @return $this
*/
public function setOrder($order)
{
$this->order = $order;

return $this;
}

/**
* @return BuilderInterface
*/
public function getNestedFilter()
{
return $this->nestedFilter;
}

/**
* @param BuilderInterface $nestedFilter
*
* @return $this
*/
public function setNestedFilter(BuilderInterface $nestedFilter)
{
$this->nestedFilter = $nestedFilter;

return $this;
}

/**
* @return string
*/
public function getDistanceType()
{
return $this->distanceType;
}

/**
* @param string $distanceType
* @return GeoSort
*/
public function setDistanceType($distanceType)
{
$this->distanceType = $distanceType;

return $this;
}

/**
* @return string
*/
public function getLocation()
{
return $this->location;
}

/**
* @param string $location
* @return GeoSort
*/
public function setLocation($location)
{
$this->location = $location;

return $this;
}

/**
* Returns element type.
*
* @return string
*/
public function getType()
{
return 'sort';
}

/**
* {@inheritdoc}
*/
public function toArray()
{
if ($this->field) {
$this->addParameter($this->field, $this->location);
}

if ($this->order) {
$this->addParameter('order', $this->order);
}

if ($this->nestedFilter) {
$this->addParameter('nested', $this->nestedFilter->toArray());
}

$output = [
'_geo_distance' => !$this->getParameters() ? new \stdClass() : $this->getParameters(),
];

return $output;
}
}

0 comments on commit 11417d6

Please sign in to comment.