-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSort.php
More file actions
72 lines (57 loc) · 1.45 KB
/
Sort.php
File metadata and controls
72 lines (57 loc) · 1.45 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
<?php declare(strict_types = 1);
namespace Spameri\ElasticQuery\Options;
/**
* @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
*/
class Sort implements \Spameri\ElasticQuery\Entity\EntityInterface
{
public const ASC = 'ASC';
public const DESC = 'DESC';
public const MISSING_LAST = '_last';
public const MISSING_FIRST = '_first';
/**
* @var string
*/
private $field;
/**
* @var string
*/
private $type;
/**
* @var string
*/
private $missing;
public function __construct(
string $field
, string $type = self::DESC
, string $missing = self::MISSING_LAST
)
{
if ( ! \in_array($type, [self::ASC, self::DESC], TRUE)) {
throw new \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)) {
throw new \Spameri\ElasticQuery\Exception\InvalidArgumentException(
'Sorting by missing value on filed ' . $field . ' is out of allowed range. See \Spameri\ElasticQuery\Options\Sort for reference.'
);
}
$this->field = $field;
$this->type = $type;
$this->missing = $missing;
}
public function key(): string
{
return $this->field;
}
public function toArray(): array
{
return [
$this->field => [
'order' => $this->type,
'missing' => $this->missing,
],
];
}
}