1
|
|
|
<?php |
2
|
|
|
namespace ViewComponents\ViewComponents\Data\Operation; |
3
|
|
|
|
4
|
|
|
namespace Nayjest\Querying\Operation; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* DataProvider operation for sorting rows. |
8
|
|
|
*/ |
9
|
|
|
class SortOperation implements OperationInterface |
10
|
|
|
{ |
11
|
|
|
const ASC = 'asc'; |
12
|
|
|
const DESC = 'desc'; |
13
|
|
|
|
14
|
|
|
protected $order; |
15
|
|
|
|
16
|
|
|
protected $field; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Creates operation for sorting rows ascending based on values of target field. |
20
|
|
|
* |
21
|
|
|
* @param string $field name of data field to sort rows by its value |
22
|
|
|
* @return SortOperation |
23
|
|
|
*/ |
24
|
|
|
public static function asc($field) |
25
|
|
|
{ |
26
|
|
|
return new self($field, SortOperation::ASC); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates operation for sorting rows ascending based on values of target field. |
31
|
|
|
* |
32
|
|
|
* @param string $field name of data field to sort rows by its value |
33
|
|
|
* @return SortOperation |
34
|
|
|
*/ |
35
|
|
|
public static function desc($field) |
36
|
|
|
{ |
37
|
|
|
return new self($field, SortOperation::DESC); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* |
43
|
|
|
* @param string|null $field name of data field to sort rows by its value |
44
|
|
|
* @param string $order (optional, default value: 'asc'), |
45
|
|
|
* please use SortOperation::ASC and SortOperation::DESC constants. |
46
|
|
|
*/ |
47
|
3 |
|
public function __construct($field = null, $order = SortOperation::ASC) |
48
|
|
|
{ |
49
|
3 |
|
$this->setField($field); |
50
|
3 |
|
$this->setOrder($order); |
51
|
3 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns sorting order ('asc' or 'desc'). |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
3 |
|
public function getOrder() |
58
|
|
|
{ |
59
|
3 |
|
return $this->order; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets sorting order. |
64
|
|
|
* |
65
|
|
|
* @param string $order 'asc' (ascending) or 'desc' (descending), |
66
|
|
|
* please use SortOperation::ASC and SortOperation::DESC constants. |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
3 |
|
public function setOrder($order) |
70
|
|
|
{ |
71
|
3 |
|
$this->order = $order; |
72
|
3 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns name of data field to sort rows by its value. |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
3 |
|
public function getField() |
80
|
|
|
{ |
81
|
3 |
|
return $this->field; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets name of data field to sort rows by its value. |
86
|
|
|
* |
87
|
|
|
* @param string $field |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
3 |
|
public function setField($field) |
91
|
|
|
{ |
92
|
3 |
|
$this->field = $field; |
93
|
3 |
|
return $this; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|