Completed
Pull Request — develop (#63)
by Sam
01:30
created

AbstractSort::assertMode()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 1
dl 8
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php namespace Nord\Lumen\Elasticsearch\Search\Sort;
2
3
use Nord\Lumen\Elasticsearch\Exceptions\InvalidArgument;
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
14
{
15
    const ORDER_ASC  = 'asc';
16
    const ORDER_DESC = 'desc';
17
18
    const MODE_MIN    = 'min';
19
    const MODE_MAX    = 'max';
20
    const MODE_SUM    = 'sum';
21
    const MODE_AVG    = 'avg';
22
    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 (!is_null($order)) {
44
            $options['order'] = $order;
45
        }
46
47
        $mode = $this->getMode();
48
        if (!is_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
69
     */
70
    public function getOrder()
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
89
     */
90
    public function getMode()
91
    {
92
        return $this->mode;
93
    }
94
95
96
    /**
97
     * @return mixed
98
     */
99
    abstract public function toArray();
100
}
101