1 | <?php |
||
21 | class SortableHeader implements HeaderFormatter { |
||
22 | |||
23 | const SORTING_SEPARATOR = '~'; |
||
24 | const COLUMN_SEPARATOR = '.'; |
||
25 | |||
26 | /** @var string */ |
||
27 | private $_defaultDirection = 'asc'; |
||
28 | |||
29 | /** @var array */ |
||
30 | private $_sortableHeaders; |
||
31 | |||
32 | /** @var array */ |
||
33 | private $_dontSort; |
||
34 | |||
35 | /** @var array */ |
||
36 | private $_sortingSymbols = ['asc' => '▲', 'desc' => '▼', 'none' => '⇵']; |
||
37 | |||
38 | /** |
||
39 | * SortableHeader constructor. |
||
40 | * |
||
41 | * @param array $sortableHeaders |
||
42 | * @param array $dontSort |
||
43 | */ |
||
44 | 7 | public function __construct(array $sortableHeaders = [], array $dontSort = []) |
|
50 | |||
51 | /** |
||
52 | * @param array $sortingSymbols |
||
53 | * @return SortableHeader |
||
54 | */ |
||
55 | 1 | public function sortingSymbols(array $sortingSymbols): SortableHeader |
|
61 | |||
62 | /** |
||
63 | * Add a field to the sortable fields. |
||
64 | * |
||
65 | * @param string $header |
||
66 | * @return SortableHeader |
||
67 | */ |
||
68 | 1 | public function makeSortable(string $header): SortableHeader |
|
75 | |||
76 | /** |
||
77 | * @param array $array |
||
78 | * @param string $key |
||
79 | */ |
||
80 | 2 | private function _removeIndex(array $array, string $key) |
|
88 | |||
89 | /** |
||
90 | * Remove the ability to sort by this column/header. |
||
91 | * |
||
92 | * @param string $header |
||
93 | * @return SortableHeader |
||
94 | */ |
||
95 | 1 | public function dontSort(string $header): SortableHeader |
|
102 | |||
103 | /** |
||
104 | * Adds a link to sort by this header/column. |
||
105 | * Also indicates how the columns are sorted (when sorted). |
||
106 | * |
||
107 | * @param Header $header |
||
108 | * @param Request $request |
||
109 | * @throws \RuntimeException |
||
110 | */ |
||
111 | 7 | public function format(Header $header, Request $request) |
|
124 | |||
125 | /** |
||
126 | * @param Request $request |
||
127 | * @return array |
||
128 | */ |
||
129 | 7 | private function _extractSortFields(Request $request): array |
|
136 | |||
137 | /** |
||
138 | * Get the sorted fields from the request. |
||
139 | * |
||
140 | * @param Request $request |
||
141 | * @return array |
||
142 | */ |
||
143 | 7 | private function _getSortFields(Request $request): array |
|
160 | |||
161 | /** |
||
162 | * Get the name of the field and the sorting direction (default: "asc"). |
||
163 | * |
||
164 | * @param string $field |
||
165 | * @return array |
||
166 | */ |
||
167 | 2 | private function _getSortParts(string $field): array |
|
168 | { |
||
169 | 2 | $sortParts = \explode(self::SORTING_SEPARATOR, $field); |
|
170 | 2 | if (\count($sortParts) === 1) |
|
171 | { |
||
172 | 1 | $sortParts[1] = $this->_defaultDirection; |
|
173 | } |
||
174 | |||
175 | 2 | return $sortParts; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * @param Request $request |
||
180 | * @return array |
||
181 | */ |
||
182 | 7 | private function _getRememberedState(Request $request): array |
|
186 | |||
187 | /** |
||
188 | * @param array $sortFields |
||
189 | * @return array |
||
190 | */ |
||
191 | 7 | private function _getDefaultSorting(array $sortFields): array |
|
201 | |||
202 | /** |
||
203 | * @param $headerAttributeName |
||
204 | * @return bool |
||
205 | */ |
||
206 | 7 | private function _showSortLink(string $headerAttributeName): bool |
|
211 | |||
212 | /** |
||
213 | * @param Request $request |
||
214 | * @param string $columnName |
||
215 | * @param string $oldDirection |
||
216 | * @return string |
||
217 | * @throws \RuntimeException |
||
218 | */ |
||
219 | 6 | private function _buildSortUrl(Request $request, string $columnName, string $oldDirection = 'asc') |
|
238 | |||
239 | /** |
||
240 | * Get the next sorting direction. |
||
241 | * |
||
242 | * @param string $oldDirection |
||
243 | * @return string |
||
244 | */ |
||
245 | 6 | private function _getNewDirection(string $oldDirection): string |
|
261 | |||
262 | /** |
||
263 | * @param string $columnName |
||
264 | * @param string $sortParameter |
||
265 | * @param string $oldDirection |
||
266 | * @param string $newSorting |
||
267 | * @return bool |
||
268 | */ |
||
269 | 6 | private function _replaceOldSort(string $columnName, string &$sortParameter, string $oldDirection, string $newSorting): bool |
|
282 | |||
283 | /** |
||
284 | * @param string $sortParameter |
||
285 | * @param string $newSorting |
||
286 | */ |
||
287 | private function _addSortParameter(string &$sortParameter, string $newSorting) |
||
295 | } |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.