Completed
Push — develop ( 876780...71f4b2 )
by Sam
10s
created

Sort   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 59
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addSort() 0 4 1
A setSorts() 0 4 1
A toArray() 0 8 2
A getSorts() 0 3 1
A __construct() 0 3 1
1
<?php namespace Nord\Lumen\Elasticsearch\Search;
2
3
use Illuminate\Contracts\Support\Arrayable;
4
use Nord\Lumen\Elasticsearch\Search\Sort\AbstractSort;
5
6
/**
7
 * Allows to add one or more sort on specific fields. Each sort can be reversed as well. The sort is defined on a per
8
 * field level, with special field name for _score to sort by score, and _doc to sort by index order.
9
 *
10
 * The order defaults to desc when sorting on the _score, and defaults to asc when sorting on anything else.
11
 *
12
 * @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html
13
 */
14
class Sort implements Arrayable
15
{
16
    /**
17
     * @var AbstractSort[]
18
     */
19
    private $sorts;
20
21
    /**
22
     * Sort constructor.
23
     *
24
     * @param AbstractSort[] $sorts
25
     */
26
    public function __construct(array $sorts = [])
27
    {
28
        $this->sorts = $sorts;
29
    }
30
31
    /**
32
     * @return array
33
     */
34
    public function toArray()
35
    {
36
        $result = [];
37
        foreach ($this->getSorts() as $sort) {
38
            $result[] = $sort->toArray();
39
        }
40
41
        return $result;
42
    }
43
44
45
    /**
46
     * @param AbstractSort $sort
47
     * @return $this
48
     */
49
    public function addSort(AbstractSort $sort)
50
    {
51
        $this->sorts[] = $sort;
52
        return $this;
53
    }
54
55
56
    /**
57
     * @param AbstractSort[] $sorts
58
     * @return Sort
59
     */
60
    public function setSorts($sorts)
61
    {
62
        $this->sorts = $sorts;
63
        return $this;
64
    }
65
66
67
    /**
68
     * @return AbstractSort[]
69
     */
70
    public function getSorts()
71
    {
72
        return $this->sorts;
73
    }
74
}
75