Skip to content
Snippets Groups Projects
Commit ee124db6 authored by Simonas Šerlinskas's avatar Simonas Šerlinskas
Browse files

introduce 2nd argument for FieldSort for ordering

parent b581a8d1
No related branches found
No related tags found
No related merge requests found
...@@ -22,10 +22,15 @@ class FieldSort implements BuilderInterface ...@@ -22,10 +22,15 @@ class FieldSort implements BuilderInterface
const DESC = 'desc'; const DESC = 'desc';
/** /**
* @var string. * @var string
*/ */
private $field; private $field;
/**
* @var string
*/
private $order;
/** /**
* @var array * @var array
*/ */
...@@ -38,11 +43,13 @@ class FieldSort implements BuilderInterface ...@@ -38,11 +43,13 @@ class FieldSort implements BuilderInterface
/** /**
* @param string $field Field name. * @param string $field Field name.
* @param string $order Order direction.
* @param array $params Params that can be set to field sort. * @param array $params Params that can be set to field sort.
*/ */
public function __construct($field, $params = []) public function __construct($field, $order = null, $params = [])
{ {
$this->field = $field; $this->field = $field;
$this->order = $order;
$this->params = $params; $this->params = $params;
} }
...@@ -77,6 +84,10 @@ class FieldSort implements BuilderInterface ...@@ -77,6 +84,10 @@ class FieldSort implements BuilderInterface
*/ */
public function toArray() public function toArray()
{ {
if ($this->order) {
$this->params['order'] = $this->order;
}
if ($this->nestedFilter) { if ($this->nestedFilter) {
$fieldValues = array_merge( $fieldValues = array_merge(
$this->params, $this->params,
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation; namespace ONGR\ElasticsearchDSL\Tests\Unit\DSL\Aggregation;
use ONGR\ElasticsearchDSL\Aggregation\AbstractAggregation;
use ONGR\ElasticsearchDSL\Aggregation\TopHitsAggregation; use ONGR\ElasticsearchDSL\Aggregation\TopHitsAggregation;
use ONGR\ElasticsearchDSL\Sort\FieldSort; use ONGR\ElasticsearchDSL\Sort\FieldSort;
...@@ -25,7 +24,7 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase ...@@ -25,7 +24,7 @@ class TopHitsAggregationTest extends \PHPUnit_Framework_TestCase
*/ */
public function testToArray() public function testToArray()
{ {
$sort = new FieldSort('acme', ['order' => 'asc']); $sort = new FieldSort('acme', FieldSort::ASC);
$aggregation = new TopHitsAggregation('acme', 1, 1, $sort); $aggregation = new TopHitsAggregation('acme', 1, 1, $sort);
$expected = [ $expected = [
......
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