-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWithSearchableColumns.php
More file actions
286 lines (243 loc) · 6.86 KB
/
Copy pathWithSearchableColumns.php
File metadata and controls
286 lines (243 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
namespace AjCastro\Searchable;
use AjCastro\Searchable\Columns;
use AjCastro\Searchable\TableColumns;
use Illuminate\Support\Arr;
trait WithSearchableColumns
{
/**
* Return the searchable columns for this model's table.
*
* @return array
*/
public function searchableColumns()
{
if (property_exists($this, 'searchableColumns')) {
return $this->searchableColumns;
}
if (property_exists($this, 'searchable') && array_key_exists('columns', $this->searchable)) {
return $this->searchable['columns'];
}
return TableColumns::get($this->getTable());
}
/**
* Return the sortable columns for this model's table.
*
* @return array
*/
public function sortableColumns()
{
if (property_exists($this, 'sortableColumns')) {
return $this->sortableColumns;
}
if (property_exists($this, 'searchable') && array_key_exists('sortable_columns', $this->searchable)) {
return $this->searchable['sortable_columns'];
}
return TableColumns::get($this->getTable());
}
/**
* Identifies if the column is a valid column, either a regular table column or derived column.
* Useful for checking valid columns to avoid sql injection especially in orderBy query.
*
* @param string $column
* @return boolean
*/
public function isColumnValid($column)
{
return (bool) $this->buildAllColumns()->find($column);
}
/**
* Build columns from both searchable and sortable columns
*/
public function buildAllColumns(): Columns
{
return Columns::make(array_merge($this->searchableColumns(), $this->sortableColumns()));
}
/**
* Build columns from searchable
*/
public function buildSearchableColumns(): Columns
{
return Columns::make($this->searchableColumns());
}
/**
* Build columns from sortable
*/
public function buildSortableColumns(): Columns
{
return Columns::make($this->sortableColumns());
}
/**
* Get the actual column from both searchable and sortable columns
*
* @param string $column
* @return void
*/
public function getColumn($column)
{
return $this->buildAllColumns()->find($column);
}
/**
* Get the actual sortable column.
*
* @param string $column
* @return string|mixed
*/
public function getSearchableColumn($column)
{
return $this->buildSearchableColumns()->find($column);
}
/**
* Get the actual sortable column.
*
* @param string $column
* @return string|mixed
*/
public function getSortableColumn($column)
{
return $this->buildSortableColumns()->find($column);
}
/**
* Return the searchable joins for the search query.
*
* @return array
*/
public function searchableJoins()
{
if (property_exists($this, 'searchableJoins')) {
return $this->searchableJoins;
}
if (property_exists($this, 'searchable') && array_key_exists('joins', $this->searchable)) {
return $this->searchable['joins'];
}
return [];
}
/**
* Set $searchable.
*
* @param array $config
* @return $this
*/
public function setSearchable($config)
{
$this->setSearchableColumns(Arr::get($config, 'columns'));
$this->setSearchableJoins(Arr::get($config, 'joins'));
$this->setSortableColumns(Arr::get($config, 'sortable_columns'));
return $this;
}
/**
* Set searchable columns.
*
* @param array $columns
* @return $this
*/
public function setSearchableColumns($columns)
{
if (property_exists($this, 'searchableColumns')) {
$this->searchableColumns = $columns ?? [];
}
if (property_exists($this, 'searchable')) {
$this->searchable['columns'] = $columns ?? [];
}
return $this;
}
/**
* Set searchable joins.
*
* @param array $joins
* @return $this
*/
public function setSearchableJoins($joins)
{
if (property_exists($this, 'searchableJoins')) {
$this->searchableJoins = $joins ?? [];
}
if (property_exists($this, 'searchable')) {
$this->searchable['joins'] = $joins ?? [];
}
return $this;
}
/**
* Set sortable columns.
*
* @param array $columns
* @return $this
*/
public function setSortableColumns($columns)
{
if (property_exists($this, 'sortableColumns')) {
$this->sortableColumns = $columns ?? [];
}
if (property_exists($this, 'searchable')) {
$this->searchable['sortable_columns'] = $columns ?? [];
}
return $this;
}
/**
* Add searchable.
*
* @param array $config
* @return $this
*/
public function addSearchable($config)
{
if ($columns = Arr::get($config, 'columns')) {
$this->addSearchableColumns($columns);
}
if ($columns = Arr::get($config, 'sortable_columns')) {
$this->addSortableColumns($columns);
}
if ($joins = Arr::get($config, 'joins')) {
$this->addSearchableJoins($joins);
}
return $this;
}
/**
* Add searchable columns.
*
* @param array $columns
* @return $this
*/
public function addSearchableColumns($columns)
{
if (property_exists($this, 'searchableColumns')) {
$this->searchableColumns = array_merge($this->searchableColumns, $columns);
}
if (property_exists($this, 'searchable')) {
$this->searchable['columns'] = array_merge($this->searchable['columns'], $columns);
}
return $this;
}
/**
* Add searchable joins.
*
* @param array $joins
* @return $this
*/
public function addSearchableJoins($joins)
{
if (property_exists($this, 'searchableJoins')) {
$this->searchableJoins = array_merge($this->searchableJoins, $joins);
}
if (property_exists($this, 'searchable')) {
$this->searchable['joins'] = array_merge($this->searchable['joins'], $joins);
}
return $this;
}
/**
* Add sortable columns.
*
* @param array $columns
* @return $this
*/
public function addSortableColumns($columns)
{
if (property_exists($this, 'sortableColumns')) {
$this->sortableColumns = array_merge($this->sortableColumns, $columns);
}
if (property_exists($this, 'searchable')) {
$this->searchable['sortable_columns'] = array_merge($this->searchable['sortable_columns'], $columns);
}
return $this;
}
}