Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSort.php
More file actions
Latest commit
85 lines (67 loc) · 1.96 KB
/
Copy pathSort.php
File metadata and controls
85 lines (67 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
declare(strict_types = 1);
namespaceSpameri\ElasticQuery\Options;
/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html
*/
class Sort implements \Spameri\ElasticQuery\Entity\EntityInterface
{
publicconstASC = 'ASC';
publicconstDESC = 'DESC';
publicconstMISSING_LAST = '_last';
publicconstMISSING_FIRST = '_first';
publicfunction__construct(
publicstring$field,
publicstring$type = self::DESC,
publicstring$missing = self::MISSING_LAST,
publicstring|null$mode = null,
public\Spameri\ElasticQuery\Options\NestedSort|null$nested = null,
publicstring|null$numericType = null,
publicstring|null$unmappedType = null,
publicstring|null$format = null,
)
{
if ( ! \in_array($type, [self::ASC, self::DESC], true)) {
thrownew \Spameri\ElasticQuery\Exception\InvalidArgumentException(
'Sorting type ' . $type . ' is out of allowed range. See \Spameri\ElasticQuery\Options\Sort for reference.',
);
}
if ( ! \in_array($missing, [self::MISSING_FIRST, self::MISSING_LAST], true)) {
thrownew \Spameri\ElasticQuery\Exception\InvalidArgumentException(
'Sorting by missing value on filed ' . $field . ' is out of allowed range. See \Spameri\ElasticQuery\Options\Sort for reference.',
);
}
}
publicfunctionkey(): string
{
return$this->field;
}
/**
* @return array<string, array<string, mixed>>
*/
publicfunctiontoArray(): array
{
$body = [
'order' => $this->type,
'missing' => $this->missing,
];
if ($this->mode !== null) {
$body['mode'] = $this->mode;
}
if ($this->nested !== null) {
$body['nested'] = $this->nested->toArray();
}
if ($this->numericType !== null) {
$body['numeric_type'] = $this->numericType;
}
if ($this->unmappedType !== null) {
$body['unmapped_type'] = $this->unmappedType;
}
if ($this->format !== null) {
$body['format'] = $this->format;
}
return [
$this->field => $body,
];
}
}