1 | <?php |
||
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 | 92 | public function getSort() |
|
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 | 3 | public function setSort($sort) |
|
142 | |||
143 | } |
||
144 |