Completed
Push — master ( 9552d6...4b2b1b )
by Simonas
08:12
created

FieldSort::getField()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ONGR\ElasticsearchDSL\Sort;
13
14
use ONGR\ElasticsearchDSL\BuilderInterface;
15
use ONGR\ElasticsearchDSL\ParametersTrait;
16
17
/**
18
 * Holds all the values required for basic sorting.
19
 */
20
class FieldSort implements BuilderInterface
21
{
22
    use ParametersTrait;
23
24
    const ASC = 'asc';
25
    const DESC = 'desc';
26
27
    /**
28
     * @var string
29
     */
30
    private $field;
31
32
    /**
33
     * @var string
34
     */
35
    private $order;
36
37
    /**
38
     * @var BuilderInterface
39
     */
40
    private $nestedFilter;
41
42
    /**
43
     * @param string $field  Field name.
44
     * @param string $order  Order direction.
45
     * @param array  $params Params that can be set to field sort.
46
     */
47
    public function __construct($field, $order = null, $params = [])
48
    {
49
        $this->field = $field;
50
        $this->order = $order;
51
        $this->setParameters($params);
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    public function getField()
58
    {
59
        return $this->field;
60
    }
61
62
    /**
63
     * @param string $field
64
     * @return self
65
     */
66
    public function setField($field)
67
    {
68
        $this->field = $field;
69
        return $this;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getOrder()
76
    {
77
        return $this->order;
78
    }
79
80
    /**
81
     * @param string $order
82
     * @return self
83
     */
84
    public function setOrder($order)
85
    {
86
        $this->order = $order;
87
        return $this;
88
    }
89
90
    /**
91
     * @return BuilderInterface
92
     */
93
    public function getNestedFilter()
94
    {
95
        return $this->nestedFilter;
96
    }
97
98
    /**
99
     * @param BuilderInterface $nestedFilter
100
     */
101
    public function setNestedFilter(BuilderInterface $nestedFilter)
102
    {
103
        $this->nestedFilter = $nestedFilter;
104
    }
105
106
    /**
107
     * Returns element type.
108
     *
109
     * @return string
110
     */
111
    public function getType()
112
    {
113
        return 'sort';
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function toArray()
120
    {
121
        if ($this->order) {
122
            $this->addParameter('order', $this->order);
123
        }
124
125
        if ($this->nestedFilter) {
126
            $this->addParameter('nested_filter', $this->nestedFilter->toArray());
127
        }
128
129
        $output = [
130
            $this->field => !$this->getParameters() ? new \stdClass() : $this->getParameters(),
131
        ];
132
133
        return $output;
134
    }
135
}
136