1 | <?php |
||
15 | class Sorter extends DataComponent { |
||
16 | |||
17 | const SORTING_SEPARATOR = '~'; |
||
18 | const COLUMN_SEPARATOR = '.'; |
||
19 | |||
20 | /** @var array */ |
||
21 | private $_sortFields = []; |
||
22 | |||
23 | /** |
||
24 | * Sorter constructor. |
||
25 | * @param null|array $fields |
||
26 | * @param bool $remember |
||
27 | */ |
||
28 | 10 | public function __construct(array $fields = null, bool $remember = false) |
|
29 | { |
||
30 | 10 | $this->_rememberKey = 'sort'; |
|
31 | 10 | $this->_rememberState = $remember; |
|
32 | |||
33 | 10 | if ($fields !== null) |
|
34 | { |
||
35 | 7 | foreach ($fields as $fieldName => $direction) |
|
36 | { |
||
37 | 7 | if (\is_int($fieldName)) |
|
38 | { |
||
39 | 3 | $fieldName = $direction; |
|
40 | 3 | $direction = 'asc'; |
|
41 | } |
||
42 | |||
43 | 7 | $this->_sortFields[$fieldName] = \mb_strtolower($direction); |
|
44 | } |
||
45 | } |
||
46 | 10 | } |
|
47 | |||
48 | /** |
||
49 | * Sort by this column. |
||
50 | * |
||
51 | * @param string $field |
||
52 | * @param string $direction |
||
53 | * |
||
54 | * @return Sorter |
||
55 | */ |
||
56 | 1 | public function addField(string $field, string $direction = 'asc') : Sorter |
|
57 | { |
||
58 | 1 | $this->_sortFields[$field] = \mb_strtolower($direction); |
|
59 | |||
60 | 1 | return $this; |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | 1 | public function render() : string |
|
67 | { |
||
68 | 1 | return implode(', ', \array_keys($this->_sortFields)); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * @return Builder |
||
73 | */ |
||
74 | 8 | protected function _shapeData() : Builder |
|
75 | { |
||
76 | 8 | if (\count($this->_sortFields) > 0) |
|
77 | { |
||
78 | 6 | foreach ($this->_sortFields as $fieldName => $direction) |
|
79 | { |
||
80 | 6 | if ($direction === 'none') |
|
81 | { |
||
82 | 1 | $this->removeField($fieldName); |
|
83 | 1 | continue; |
|
84 | } |
||
85 | |||
86 | 5 | $this->_sortField($fieldName, $direction); |
|
87 | } |
||
88 | } |
||
89 | |||
90 | 8 | return $this->_dataTable->query(); |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Stop sorting by this column |
||
95 | * |
||
96 | * @param string $field |
||
97 | * |
||
98 | * @return Sorter |
||
99 | */ |
||
100 | 1 | public function removeField(string $field) : Sorter |
|
101 | { |
||
102 | 1 | if (isset($this->_sortFields[$field])) |
|
103 | { |
||
104 | 1 | unset($this->_sortFields[$field]); |
|
105 | } |
||
106 | |||
107 | 1 | return $this; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param string $fieldName |
||
112 | * @param string $direction |
||
113 | */ |
||
114 | private function _sortField(string $fieldName, string $direction) : void |
||
115 | { |
||
116 | /** @var Column $column */ |
||
117 | 5 | $column = \array_first($this->_dataTable->getColumns(), function($column) use ($fieldName) { |
|
118 | /** @var Column $column */ |
||
119 | 5 | return $column->getKey() === $fieldName; |
|
120 | 5 | }); |
|
121 | |||
122 | 5 | if ($column !== null) |
|
123 | { |
||
124 | 5 | $this->_dataTable->query()->orderBy(DB::raw($column->getAttributeName()), $direction); |
|
1 ignored issue
–
show
|
|||
125 | } |
||
126 | 5 | } |
|
127 | |||
128 | 1 | protected function _readFromSession() : void |
|
132 | |||
133 | 1 | protected function _storeInSession() : void |
|
137 | |||
138 | 9 | protected function _afterInit() : void |
|
149 | |||
150 | /** |
||
151 | * @param string $fields |
||
152 | */ |
||
153 | 2 | private function _initFields(string $fields) : void |
|
172 | } |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.