1 | <?php |
||
21 | class SortableHeader implements HeaderFormatter { |
||
22 | |||
23 | const SORTING_SEPARATOR = '~'; |
||
24 | const COLUMN_SEPARATOR = '.'; |
||
25 | |||
26 | /** @var array */ |
||
27 | private $_sortableHeaders; |
||
28 | |||
29 | /** @var array */ |
||
30 | private $_dontSort; |
||
31 | |||
32 | /** @var array */ |
||
33 | private $_sortingSymbols = ['asc' => '▲', 'desc' => '▼', 'none' => '⇵']; |
||
34 | |||
35 | /** |
||
36 | * SortableHeader constructor. |
||
37 | * |
||
38 | * @param array $sortableHeaders |
||
39 | * @param array $dontSort |
||
40 | */ |
||
41 | public function __construct(array $sortableHeaders = [], array $dontSort = []) |
||
47 | |||
48 | /** |
||
49 | * @param array $sortingSymbols |
||
50 | * @return $this |
||
51 | */ |
||
52 | public function sortingSymbols(array $sortingSymbols) |
||
58 | |||
59 | /** |
||
60 | * @param array $array |
||
61 | * @param string $key |
||
62 | */ |
||
63 | private function _removeIndex(array $array, string $key) |
||
71 | |||
72 | /** |
||
73 | * Add a field to the sortable fields. |
||
74 | * |
||
75 | * @param string $header |
||
76 | * @return $this |
||
77 | */ |
||
78 | public function makeSortable(string $header) |
||
85 | |||
86 | /** |
||
87 | * Remove the ability to sort by this column/header. |
||
88 | * |
||
89 | * @param string $header |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function dontSort(string $header) |
||
99 | |||
100 | /** |
||
101 | * @param Request $request |
||
102 | * @return array |
||
103 | */ |
||
104 | private function _getRememberedState(Request $request): array |
||
108 | |||
109 | /** |
||
110 | * Get the sorted fields from the request. |
||
111 | * |
||
112 | * @param Request $request |
||
113 | * @return array |
||
114 | */ |
||
115 | private function _getSortFields(Request $request): array |
||
137 | |||
138 | /** |
||
139 | * @param Request $request |
||
140 | * @return array |
||
141 | */ |
||
142 | private function _extractSortFields(Request $request) |
||
146 | |||
147 | /** |
||
148 | * Get the next sorting direction. |
||
149 | * |
||
150 | * @param string $oldDirection |
||
151 | * @return string |
||
152 | */ |
||
153 | private function _getNewDirection(string $oldDirection): string |
||
169 | |||
170 | /** |
||
171 | * @param Request $request |
||
172 | * @param string $column |
||
173 | * @param string $oldDirection |
||
174 | * @return string |
||
175 | * @throws \RuntimeException |
||
176 | */ |
||
177 | private function _buildSortUrl(Request $request, string $column, string $oldDirection = 'asc') |
||
210 | |||
211 | /** |
||
212 | * @param $headerAttributeName |
||
213 | * @return bool |
||
214 | */ |
||
215 | private function _showSortLink(string $headerAttributeName): bool |
||
220 | |||
221 | /** |
||
222 | * Adds a link to sort by this header/column. |
||
223 | * Also indicates how the columns are sorted (when sorted). |
||
224 | * |
||
225 | * @param Header $header |
||
226 | * @param Request $request |
||
227 | * @throws \RuntimeException |
||
228 | */ |
||
229 | public function format(Header $header, Request $request) |
||
242 | } |