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