|
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\Traits\Criteria; |
|
15
|
|
|
|
|
16
|
|
|
use Exception; |
|
17
|
|
|
use Maslosoft\Mangan\Interfaces\Criteria\DecoratableInterface; |
|
18
|
|
|
use Maslosoft\Mangan\Interfaces\Criteria\SortableInterface; |
|
19
|
|
|
use Maslosoft\Mangan\Interfaces\CriteriaInterface; |
|
20
|
|
|
use Maslosoft\Mangan\Interfaces\SortInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* SortableTrait |
|
24
|
|
|
* @see SortableInterface |
|
25
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
26
|
|
|
*/ |
|
27
|
|
|
trait SortableTrait |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
private $_sort = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Add sorting, available orders are: Criteria::SortAsc and Criteria::SortDesc |
|
34
|
|
|
* Each call will be grouped with previous calls |
|
35
|
|
|
* @param string $fieldName |
|
36
|
|
|
* @param integer $order |
|
37
|
|
|
* @return CriteriaInterface |
|
38
|
|
|
* @since v1.0 |
|
39
|
|
|
*/ |
|
40
|
1 |
|
public function sort($fieldName, $order = SortInterface::SortAsc) |
|
41
|
|
|
{ |
|
42
|
1 |
|
if ($this instanceof DecoratableInterface) |
|
43
|
|
|
{ |
|
44
|
1 |
|
$decorated = $this->getCd()->decorate($fieldName); |
|
45
|
1 |
|
$this->_sort[key($decorated)] = intval($order); |
|
46
|
|
|
// NOTE: Ignore further bogus scrunitize report: |
|
47
|
|
|
// Accessing _sort on the interface Maslosoft\Mangan\Interfa...ia\DecoratableInterface |
|
48
|
|
|
// suggest that you code against a concrete implementation. How about adding an instanceof check? |
|
49
|
|
|
} |
|
50
|
|
|
else |
|
51
|
|
|
{ |
|
52
|
|
|
$this->_sort[$fieldName] = intval($order); |
|
53
|
|
|
} |
|
54
|
1 |
|
return $this; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @since v1.0 |
|
59
|
|
|
*/ |
|
60
|
135 |
|
public function getSort() |
|
61
|
|
|
{ |
|
62
|
135 |
|
return $this->_sort; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Set sorting of results. Use model field names as keys and Criteria's sort consntants. |
|
67
|
|
|
* |
|
68
|
|
|
* All fields will be automatically decorated according to model. |
|
69
|
|
|
* For instance, when sorting on i18n field simply use field name, without language prefix. |
|
70
|
|
|
* |
|
71
|
|
|
* Sort by title example: |
|
72
|
|
|
* ```php |
|
73
|
|
|
* $criteria = new Criteria(); |
|
74
|
|
|
* $sort = [ |
|
75
|
|
|
* 'title' => Criteria::SortAsc |
|
76
|
|
|
* ]; |
|
77
|
|
|
* $criteria->setSort($sort); |
|
78
|
|
|
* ``` |
|
79
|
|
|
* If title is declared as i18n and language is set to `en`, it will sort by `title.en` ascending in this case. |
|
80
|
|
|
* |
|
81
|
|
|
* Subsequent calls to setSort will override existing sort field and add new ones. |
|
82
|
|
|
* |
|
83
|
|
|
* Sort by title and then reverse order and add another field example: |
|
84
|
|
|
* ```php |
|
85
|
|
|
* $criteria = new Criteria(); |
|
86
|
|
|
* $sort = [ |
|
87
|
|
|
* 'title' => Criteria::SortAsc |
|
88
|
|
|
* ]; |
|
89
|
|
|
* $criteria->setSort($sort); |
|
90
|
|
|
* // Override order and add second sort field |
|
91
|
|
|
* $sort = [ |
|
92
|
|
|
* 'title' => Criteria::SortDesc, |
|
93
|
|
|
* 'active' => Critera::SortAsc |
|
94
|
|
|
* ]; |
|
95
|
|
|
* $criteria->setSort($sort); |
|
96
|
|
|
* ``` |
|
97
|
|
|
* Will sort by title descending, then active ascending |
|
98
|
|
|
* |
|
99
|
|
|
* When using `Sort` object as param, it will replace entire sorting |
|
100
|
|
|
* information with that provided by `Sort` instance. |
|
101
|
|
|
* |
|
102
|
|
|
* Sort by title and then replace with `Sort` instance example: |
|
103
|
|
|
* ```php |
|
104
|
|
|
* $criteria = new Criteria(); |
|
105
|
|
|
* $sort = [ |
|
106
|
|
|
* 'title' => Criteria::SortAsc |
|
107
|
|
|
* 'active' => Critera::SortAsc |
|
108
|
|
|
* ]; |
|
109
|
|
|
* $criteria->setSort($sort); |
|
110
|
|
|
* |
|
111
|
|
|
* // Override order completely with new Sort instance |
|
112
|
|
|
* $sort = new Sort([ |
|
113
|
|
|
* 'title' => Criteria::SortDesc, |
|
114
|
|
|
* ]; |
|
115
|
|
|
* $criteria->setSort($sort); |
|
116
|
|
|
* ``` |
|
117
|
|
|
* Will sort by title descending |
|
118
|
|
|
* |
|
119
|
|
|
* |
|
120
|
|
|
* @param mixed[]|SortInterface |
|
121
|
|
|
* @return CriteriaInterface |
|
122
|
|
|
*/ |
|
123
|
5 |
|
public function setSort($sort) |
|
124
|
|
|
{ |
|
125
|
5 |
|
if ($sort instanceof SortInterface) |
|
126
|
|
|
{ |
|
127
|
5 |
|
$this->_sort = $sort->getSort(); |
|
128
|
|
|
} |
|
129
|
|
|
else |
|
130
|
|
|
{ |
|
131
|
|
|
if (!is_array($sort)) |
|
132
|
|
|
{ |
|
133
|
|
|
throw new Exception(sprintf('Sort must be instance of `%s` or array, `%s` given', SortInterface::class, gettype($sort))); |
|
134
|
|
|
} |
|
135
|
|
|
foreach ($sort as $fieldName => $order) |
|
136
|
|
|
{ |
|
137
|
|
|
$this->sort($fieldName, $order); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
5 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
} |
|
144
|
|
|
|