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 string |
56
|
|
|
*/ |
57
|
|
|
public function getField() |
58
|
|
|
{ |
59
|
|
|
return $this->field; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $field |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setField($field) |
68
|
|
|
{ |
69
|
|
|
$this->field = $field; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getOrder() |
78
|
|
|
{ |
79
|
|
|
return $this->order; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $order |
84
|
|
|
* |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function setOrder($order) |
88
|
|
|
{ |
89
|
|
|
$this->order = $order; |
90
|
|
|
|
91
|
|
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return BuilderInterface |
96
|
|
|
*/ |
97
|
|
|
public function getNestedFilter() |
98
|
|
|
{ |
99
|
|
|
return $this->nestedFilter; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param BuilderInterface $nestedFilter |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function setNestedFilter(BuilderInterface $nestedFilter) |
108
|
|
|
{ |
109
|
|
|
$this->nestedFilter = $nestedFilter; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns element type. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
public function getType() |
120
|
|
|
{ |
121
|
|
|
return 'sort'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
|
|
public function toArray() |
128
|
|
|
{ |
129
|
|
|
if ($this->order) { |
130
|
|
|
$this->addParameter('order', $this->order); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($this->nestedFilter) { |
134
|
|
|
$this->addParameter('nested', $this->nestedFilter->toArray()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$output = [ |
138
|
|
|
$this->field => !$this->getParameters() ? new \stdClass() : $this->getParameters(), |
139
|
|
|
]; |
140
|
|
|
|
141
|
|
|
return $output; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|