1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Zemit Framework. |
5
|
|
|
* |
6
|
|
|
* (c) Zemit Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Zemit\Mvc\Controller\Traits\Query; |
13
|
|
|
|
14
|
|
|
use Exception; |
15
|
|
|
use Phalcon\Support\Collection; |
16
|
|
|
use Phalcon\Filter\Filter; |
17
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractModel; |
18
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\AbstractParams; |
19
|
|
|
use Zemit\Mvc\Controller\Traits\Abstracts\Query\AbstractOrder; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The Order trait sets and retrieves the order parameter for the query. |
23
|
|
|
*/ |
24
|
|
|
trait Order |
25
|
|
|
{ |
26
|
|
|
use AbstractOrder; |
27
|
|
|
|
28
|
|
|
use AbstractModel; |
29
|
|
|
use AbstractParams; |
30
|
|
|
|
31
|
|
|
protected array|string|null $defaultOrder = null; |
32
|
|
|
protected ?Collection $order; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initializes the default order for the instance. |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public function initializeDefaultOrder(): void |
39
|
|
|
{ |
40
|
|
|
$this->setDefaultOrder(null); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Initializes the order parameter for the query. |
45
|
|
|
* This method processes and sets the order parameter based on the "order" input received. |
46
|
|
|
* |
47
|
|
|
* @return void |
48
|
|
|
* @throws Exception If the order parameter is invalid. |
49
|
|
|
*/ |
50
|
|
|
public function initializeOrder(): void |
51
|
|
|
{ |
52
|
|
|
$this->initializeDefaultOrder(); |
53
|
|
|
$order = $this->getParam('order', [Filter::FILTER_STRING, Filter::FILTER_TRIM], $this->getDefaultOrder()); |
54
|
|
|
|
55
|
|
|
if (!isset($order)) { |
56
|
|
|
$this->setOrder(null); |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (is_string($order)) { |
61
|
|
|
$order = explode(',', $order); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// type check order parameter |
65
|
|
|
if (!is_array($order)) { |
66
|
|
|
throw new Exception('Invalid order parameter received. Expected `null`, `string` or `array` but `' . gettype($order) . '` was provided.', 400); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$collection = new Collection([], false); |
70
|
|
|
foreach ($order as $key => $item) { |
71
|
|
|
if (is_int($key)) { |
72
|
|
|
if (is_string($item)) { |
73
|
|
|
$item = explode(' ', $item); |
74
|
|
|
} |
75
|
|
|
// type check order element |
76
|
|
|
if (!is_array($item)) { |
77
|
|
|
throw new Exception('Invalid order element received. Expected `string` or `array` but `' . gettype($item) . '` was provided.', 400); |
78
|
|
|
} |
79
|
|
|
$collection->set($item[0], $this->appendModelName($item[0]) . ' ' . $this->getSide($item[1] ?? 'asc')); |
80
|
|
|
} |
81
|
|
|
// string |
82
|
|
|
else { |
83
|
|
|
$collection->set($key, $this->appendModelName($key) . ' ' . $this->getSide($item ?? 'asc')); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$this->setOrder($collection); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Sets the order for the query. |
92
|
|
|
* The provided order will replace any existing order previously set. |
93
|
|
|
* |
94
|
|
|
* @param Collection|null $order The order to be set. It can be a Collection object or null. |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function setOrder(?Collection $order): void |
98
|
|
|
{ |
99
|
|
|
$this->order = $order; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Retrieves the order assigned to the query. |
104
|
|
|
* If no order has been assigned, it will return null. |
105
|
|
|
* |
106
|
|
|
* @return Collection|null The order collection to assign to the query, or null if no order has been set. |
107
|
|
|
*/ |
108
|
|
|
public function getOrder(): ?Collection |
109
|
|
|
{ |
110
|
|
|
return $this->order; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Sets the default order for the query. |
115
|
|
|
* The default order will be used if no "order" parameter was provided |
116
|
|
|
* |
117
|
|
|
* @param array|string|null $defaultOrder The default order to be set. It can be an array, a string, or null. |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function setDefaultOrder(array|string|null $defaultOrder): void |
121
|
|
|
{ |
122
|
|
|
$this->defaultOrder = $defaultOrder; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Retrieves the default order for the query. |
127
|
|
|
* |
128
|
|
|
* @return array|string|null The default order. It can be an array, a string, or null. |
129
|
|
|
*/ |
130
|
|
|
public function getDefaultOrder(): array|string|null |
131
|
|
|
{ |
132
|
|
|
return $this->defaultOrder; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns the side value based on the given input. |
137
|
|
|
* |
138
|
|
|
* @param string $side The side value to be checked. |
139
|
|
|
* |
140
|
|
|
* @return string The side value. Returns 'desc' if the input is 'desc', otherwise returns 'asc'. |
141
|
|
|
*/ |
142
|
|
|
protected function getSide(string $side): string |
143
|
|
|
{ |
144
|
|
|
if (strtolower(trim($side)) === 'desc') { |
145
|
|
|
return 'desc'; |
146
|
|
|
} |
147
|
|
|
return 'asc'; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|