|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan; |
|
15
|
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Criteria\ConditionDecorator; |
|
19
|
|
|
use Maslosoft\Mangan\Interfaces\SortInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Sort |
|
23
|
|
|
*/ |
|
24
|
|
|
class Sort implements SortInterface |
|
25
|
|
|
{ |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Sort Ascending |
|
29
|
|
|
* Alias left for BC |
|
30
|
|
|
* @deprecated since version 4.0.7 |
|
31
|
|
|
*/ |
|
32
|
|
|
const SORT_ASC = self::SortAsc; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Sort Descending |
|
36
|
|
|
* Alias left for BC |
|
37
|
|
|
* @deprecated since version 4.0.7 |
|
38
|
|
|
*/ |
|
39
|
|
|
const SORT_DESC = self::SortDesc; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @Ignored |
|
43
|
|
|
* @var AnnotatedInterface |
|
44
|
|
|
*/ |
|
45
|
|
|
public $model = null; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* |
|
49
|
|
|
* @var int[] |
|
50
|
|
|
*/ |
|
51
|
|
|
public $fields = []; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Condition decorator instance |
|
55
|
|
|
* @var ConditionDecorator |
|
56
|
|
|
*/ |
|
57
|
|
|
private $cd; |
|
58
|
|
|
|
|
59
|
11 |
|
public function __construct($sort = [], AnnotatedInterface $model = null) |
|
60
|
|
|
{ |
|
61
|
11 |
|
foreach ($sort as $field => $order) |
|
62
|
|
|
{ |
|
63
|
5 |
|
$this->replace($field, $order); |
|
64
|
|
|
} |
|
65
|
11 |
|
$this->setModel($model); |
|
66
|
11 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add sorting field order. If field is already declared it will be pushed to the end of sort list. |
|
70
|
|
|
* @param string $field |
|
71
|
|
|
* @param int $order |
|
72
|
|
|
*/ |
|
73
|
|
|
public function add($field, $order) |
|
74
|
|
|
{ |
|
75
|
|
|
$this->remove($field); |
|
76
|
|
|
$this->replace($field, $order); |
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Create or replace sorting field order. If field is already declared it will **not** be pushed to the end of sort list. |
|
82
|
|
|
* @param string $field |
|
83
|
|
|
* @param int $order |
|
84
|
|
|
*/ |
|
85
|
5 |
|
public function replace($field, $order) |
|
86
|
|
|
{ |
|
87
|
5 |
|
if (!in_array($order, [self::SortAsc, self::SortDesc])) |
|
88
|
|
|
{ |
|
89
|
|
|
throw new InvalidArgumentException(sprintf('Invalid order `%s` value for field `%s` of model %s', $order, $field, get_class($this->model))); |
|
90
|
|
|
} |
|
91
|
5 |
|
$this->fields[$field] = $order; |
|
92
|
5 |
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Remove sort field |
|
97
|
|
|
* @param string $field |
|
98
|
|
|
*/ |
|
99
|
|
|
public function remove($field) |
|
100
|
|
|
{ |
|
101
|
|
|
if (isset($this->fields[$field])) |
|
102
|
|
|
{ |
|
103
|
|
|
unset($this->fields[$field]); |
|
104
|
|
|
} |
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Returns true if sorting is applied |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
6 |
|
public function isSorted() |
|
113
|
|
|
{ |
|
114
|
6 |
|
return count($this->fields) > 0; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
5 |
|
public function getSort() |
|
118
|
|
|
{ |
|
119
|
5 |
|
$sort = []; |
|
120
|
5 |
|
if (null === $this->cd) |
|
121
|
|
|
{ |
|
122
|
5 |
|
$this->cd = new ConditionDecorator($this->model); |
|
123
|
|
|
} |
|
124
|
5 |
|
foreach ($this->fields as $fieldName => $order) |
|
125
|
|
|
{ |
|
126
|
5 |
|
$decorated = $this->cd->decorate($fieldName); |
|
127
|
5 |
|
$sort[key($decorated)] = $order; |
|
128
|
|
|
} |
|
129
|
5 |
|
return $sort; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
11 |
|
public function setModel(AnnotatedInterface $model = null) |
|
133
|
|
|
{ |
|
134
|
11 |
|
if (null !== $model) |
|
135
|
|
|
{ |
|
136
|
6 |
|
$this->model = $model; |
|
137
|
|
|
} |
|
138
|
11 |
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
|
142
|
|
|
|