1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Utilities; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
9
|
|
|
|
10
|
|
|
class Helper |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Places item of extra columns into results by care of their order. |
14
|
|
|
* |
15
|
|
|
* @param array $item |
16
|
|
|
* @param array $array |
17
|
|
|
* @return array |
18
|
|
|
*/ |
19
|
|
|
public static function includeInArray($item, $array) |
20
|
|
|
{ |
21
|
|
|
if (self::isItemOrderInvalid($item, $array)) { |
22
|
|
|
return array_merge($array, [$item['name'] => $item['content']]); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$count = 0; |
26
|
|
|
$last = $array; |
27
|
|
|
$first = []; |
28
|
|
|
foreach ($array as $key => $value) { |
29
|
|
|
if ($count == $item['order']) { |
30
|
|
|
return array_merge($first, [$item['name'] => $item['content']], $last); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
unset($last[$key]); |
34
|
|
|
$first[$key] = $value; |
35
|
|
|
|
36
|
|
|
$count++; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Check if item order is valid. |
42
|
|
|
* |
43
|
|
|
* @param array $item |
44
|
|
|
* @param array $array |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
|
|
protected static function isItemOrderInvalid($item, $array) |
48
|
|
|
{ |
49
|
|
|
return $item['order'] === false || $item['order'] >= count($array); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Determines if content is callable or blade string, processes and returns. |
54
|
|
|
* |
55
|
|
|
* @param mixed $content Pre-processed content |
56
|
|
|
* @param array $data data to use with blade template |
57
|
|
|
* @param mixed $param parameter to call with callable |
58
|
|
|
* @return mixed |
59
|
|
|
*/ |
60
|
|
|
public static function compileContent($content, array $data, $param) |
61
|
|
|
{ |
62
|
|
|
if (is_string($content)) { |
63
|
|
|
return static::compileBlade($content, static::getMixedValue($data, $param)); |
64
|
|
|
} elseif (is_callable($content)) { |
65
|
|
|
return $content($param); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $content; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Parses and compiles strings by using Blade Template System. |
73
|
|
|
* |
74
|
|
|
* @param string $str |
75
|
|
|
* @param array $data |
76
|
|
|
* @return mixed |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
|
|
public static function compileBlade($str, $data = []) |
80
|
|
|
{ |
81
|
|
|
if (view()->exists($str)) { |
|
|
|
|
82
|
|
|
return view($str, $data)->render(); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
ob_start() && extract($data, EXTR_SKIP); |
86
|
|
|
eval('?>' . app('blade.compiler')->compileString($str)); |
87
|
|
|
$str = ob_get_contents(); |
88
|
|
|
ob_end_clean(); |
89
|
|
|
|
90
|
|
|
return $str; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get a mixed value of custom data and the parameters. |
95
|
|
|
* |
96
|
|
|
* @param array $data |
97
|
|
|
* @param mixed $param |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
public static function getMixedValue(array $data, $param) |
101
|
|
|
{ |
102
|
|
|
$casted = self::castToArray($param); |
103
|
|
|
|
104
|
|
|
$data['model'] = $param; |
105
|
|
|
|
106
|
|
|
foreach ($data as $key => $value) { |
107
|
|
|
if (isset($casted[$key])) { |
108
|
|
|
$data[$key] = $casted[$key]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $data; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Cast the parameter into an array. |
117
|
|
|
* |
118
|
|
|
* @param mixed $param |
119
|
|
|
* @return array |
120
|
|
|
*/ |
121
|
|
|
public static function castToArray($param) |
122
|
|
|
{ |
123
|
|
|
if ($param instanceof \stdClass) { |
124
|
|
|
$param = (array) $param; |
125
|
|
|
|
126
|
|
|
return $param; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if ($param instanceof Arrayable) { |
130
|
|
|
return $param->toArray(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $param; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get equivalent or method of query builder. |
138
|
|
|
* |
139
|
|
|
* @param string $method |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public static function getOrMethod($method) |
143
|
|
|
{ |
144
|
|
|
if (! Str::contains(Str::lower($method), 'or')) { |
145
|
|
|
return 'or' . ucfirst($method); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $method; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Converts array object values to associative array. |
153
|
|
|
* |
154
|
|
|
* @param mixed $row |
155
|
|
|
* @param array $filters |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
public static function convertToArray($row, $filters = []) |
159
|
|
|
{ |
160
|
|
|
$row = method_exists($row, 'makeHidden') ? $row->makeHidden(Arr::get($filters, 'hidden', [])) : $row; |
161
|
|
|
$row = method_exists($row, 'makeVisible') ? $row->makeVisible(Arr::get($filters, 'visible', [])) : $row; |
162
|
|
|
$data = $row instanceof Arrayable ? $row->toArray() : (array) $row; |
163
|
|
|
|
164
|
|
|
foreach ($data as &$value) { |
165
|
|
|
if (is_object($value) || is_array($value)) { |
166
|
|
|
$value = self::convertToArray($value); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
unset($value); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return $data; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array $data |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public static function transform(array $data) |
180
|
|
|
{ |
181
|
|
|
return array_map(function ($row) { |
182
|
|
|
return self::transformRow($row); |
183
|
|
|
}, $data); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Transform row data into an array. |
188
|
|
|
* |
189
|
|
|
* @param mixed $row |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
|
protected static function transformRow($row) |
193
|
|
|
{ |
194
|
|
|
foreach ($row as $key => $value) { |
195
|
|
|
if ($value instanceof DateTime) { |
196
|
|
|
$row[$key] = $value->format('Y-m-d H:i:s'); |
197
|
|
|
} else { |
198
|
|
|
if (is_object($value)) { |
199
|
|
|
$row[$key] = (string) $value; |
200
|
|
|
} else { |
201
|
|
|
$row[$key] = $value; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
return $row; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Build parameters depending on # of arguments passed. |
211
|
|
|
* |
212
|
|
|
* @param array $args |
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
public static function buildParameters(array $args) |
216
|
|
|
{ |
217
|
|
|
$parameters = []; |
218
|
|
|
|
219
|
|
|
if (count($args) > 2) { |
220
|
|
|
$parameters[] = $args[0]; |
221
|
|
|
foreach ($args[1] as $param) { |
222
|
|
|
$parameters[] = $param; |
223
|
|
|
} |
224
|
|
|
} else { |
225
|
|
|
foreach ($args[0] as $param) { |
226
|
|
|
$parameters[] = $param; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $parameters; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Replace all pattern occurrences with keyword. |
235
|
|
|
* |
236
|
|
|
* @param array $subject |
237
|
|
|
* @param string $keyword |
238
|
|
|
* @param string $pattern |
239
|
|
|
* @return array |
240
|
|
|
*/ |
241
|
|
|
public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1') |
242
|
|
|
{ |
243
|
|
|
$parameters = []; |
244
|
|
|
foreach ($subject as $param) { |
245
|
|
|
if (is_array($param)) { |
246
|
|
|
$parameters[] = self::replacePatternWithKeyword($param, $keyword, $pattern); |
247
|
|
|
} else { |
248
|
|
|
$parameters[] = str_replace($pattern, $keyword, $param); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
return $parameters; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Get column name from string. |
257
|
|
|
* |
258
|
|
|
* @param string $str |
259
|
|
|
* @param bool $wantsAlias |
260
|
|
|
* @return string |
261
|
|
|
*/ |
262
|
|
|
public static function extractColumnName($str, $wantsAlias) |
263
|
|
|
{ |
264
|
|
|
$matches = explode(' as ', Str::lower($str)); |
265
|
|
|
|
266
|
|
|
if (! empty($matches)) { |
267
|
|
|
if ($wantsAlias) { |
268
|
|
|
return array_pop($matches); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return array_shift($matches); |
272
|
|
|
} elseif (strpos($str, '.')) { |
273
|
|
|
$array = explode('.', $str); |
274
|
|
|
|
275
|
|
|
return array_pop($array); |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
return $str; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Adds % wildcards to the given string. |
283
|
|
|
* |
284
|
|
|
* @param string $str |
285
|
|
|
* @param bool $lowercase |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
|
|
public static function wildcardLikeString($str, $lowercase = true) |
289
|
|
|
{ |
290
|
|
|
return static::wildcardString($str, '%', $lowercase); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Adds wildcards to the given string. |
295
|
|
|
* |
296
|
|
|
* @param string $str |
297
|
|
|
* @param string $wildcard |
298
|
|
|
* @param bool $lowercase |
299
|
|
|
* @return string |
300
|
|
|
*/ |
301
|
|
|
public static function wildcardString($str, $wildcard, $lowercase = true) |
302
|
|
|
{ |
303
|
|
|
$wild = $wildcard; |
304
|
|
|
$chars = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY); |
305
|
|
|
|
306
|
|
|
if (count($chars) > 0) { |
307
|
|
|
foreach ($chars as $char) { |
308
|
|
|
$wild .= $char . $wildcard; |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if ($lowercase) { |
313
|
|
|
$wild = Str::lower($wild); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
return $wild; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: