1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ONGR package. |
5
|
|
|
* |
6
|
|
|
* (c) NFQ Technologies UAB <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ONGR\ElasticsearchDSL\Sort; |
13
|
|
|
|
14
|
|
|
use ONGR\ElasticsearchDSL\BuilderInterface; |
15
|
|
|
use ONGR\ElasticsearchDSL\ParametersTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Holds all the values required for basic sorting. |
19
|
|
|
*/ |
20
|
|
|
class FieldSort implements BuilderInterface |
21
|
|
|
{ |
22
|
|
|
use ParametersTrait; |
23
|
|
|
|
24
|
|
|
const ASC = 'asc'; |
25
|
|
|
const DESC = 'desc'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $field; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $order; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var BuilderInterface |
39
|
|
|
*/ |
40
|
|
|
private $nestedFilter; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $field Field name. |
44
|
|
|
* @param string $order Order direction. |
45
|
|
|
* @param array $params Params that can be set to field sort. |
46
|
|
|
*/ |
47
|
|
|
public function __construct($field, $order = null, $params = []) |
48
|
|
|
{ |
49
|
|
|
$this->field = $field; |
50
|
|
|
$this->order = $order; |
51
|
|
|
$this->setParameters($params); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return BuilderInterface |
56
|
|
|
*/ |
57
|
|
|
public function getNestedFilter() |
58
|
|
|
{ |
59
|
|
|
return $this->nestedFilter; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param BuilderInterface $nestedFilter |
64
|
|
|
*/ |
65
|
|
|
public function setNestedFilter(BuilderInterface $nestedFilter) |
66
|
|
|
{ |
67
|
|
|
$this->nestedFilter = $nestedFilter; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns element type. |
72
|
|
|
* |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function getType() |
76
|
|
|
{ |
77
|
|
|
return 'sort'; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function toArray() |
84
|
|
|
{ |
85
|
|
|
if ($this->order) { |
86
|
|
|
$this->addParameter('order', $this->order); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ($this->nestedFilter) { |
90
|
|
|
$this->addParameter('nested_filter', $this->nestedFilter->toArray()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$output = [ |
94
|
|
|
$this->field => !$this->getParameters() ? new \stdClass() : $this->getParameters(), |
95
|
|
|
]; |
96
|
|
|
|
97
|
|
|
return $output; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|