AbstractSort::applyOptions()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 13
rs 10
cc 3
nc 4
nop 1
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Sort;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
5
/**
6
 * Allows to add one or more sort on specific fields. Each sort can be reversed as well. The sort is defined on a per
7
 * field level, with special field name for _score to sort by score, and _doc to sort by index order.
8
 *
9
 * The order defaults to desc when sorting on the _score, and defaults to asc when sorting on anything else.
10
 *
11
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
12
 */
13
abstract class AbstractSort implements Arrayable
14
{
15
    public const ORDER_ASC  = 'asc';
16
    public const ORDER_DESC = 'desc';
17
18
    public const MODE_MIN    = 'min';
19
    public const MODE_MAX    = 'max';
20
    public const MODE_SUM    = 'sum';
21
    public const MODE_AVG    = 'avg';
22
    public const MODE_MEDIAN = 'median';
23
24
    /**
25
     * @var ?string Defaults to desc when sorting on the _score, and defaults to asc when sorting on anything else.
26
     */
27
    private $order;
28
29
    /**
30
     * @var ?string Elasticsearch supports sorting by array or multi-valued fields. The mode option controls what array
31
     * value is picked for sorting the document it belongs to.
32
     */
33
    private $mode;
34
35
36
    /**
37
     * @var array $options
38
     * @return array
39
     */
40
    protected function applyOptions(array $options)
41
    {
42
        $order = $this->getOrder();
43
        if (null !== $order) {
44
            $options['order'] = $order;
45
        }
46
47
        $mode = $this->getMode();
48
        if (null !== $mode) {
49
            $options['mode'] = $mode;
50
        }
51
52
        return $options;
53
    }
54
55
56
    /**
57
     * @param string $order
58
     * @return AbstractSort
59
     */
60
    public function setOrder($order)
61
    {
62
        $this->order = $order;
63
        return $this;
64
    }
65
66
67
    /**
68
     * @return string|null
69
     */
70
    public function getOrder(): ?string
71
    {
72
        return $this->order;
73
    }
74
75
76
    /**
77
     * @param string $mode
78
     * @return AbstractSort
79
     */
80
    public function setMode($mode)
81
    {
82
        $this->mode = $mode;
83
        return $this;
84
    }
85
86
87
    /**
88
     * @return string|null
89
     */
90
    public function getMode(): ?string
91
    {
92
        return $this->mode;
93
    }
94
}
95