-
-
Notifications
You must be signed in to change notification settings - Fork 862
/
Copy pathHelper.php
328 lines (281 loc) · 7.97 KB
/
Helper.php
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
<?php
namespace Yajra\DataTables\Utilities;
use DateTime;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
class Helper
{
/**
* Places item of extra columns into results by care of their order.
*
* @param array $item
* @param array $array
* @return array
*/
public static function includeInArray($item, $array)
{
if (self::isItemOrderInvalid($item, $array)) {
return array_merge($array, [$item['name'] => $item['content']]);
}
$count = 0;
$last = $array;
$first = [];
foreach ($array as $key => $value) {
if ($count == $item['order']) {
return array_merge($first, [$item['name'] => $item['content']], $last);
}
unset($last[$key]);
$first[$key] = $value;
$count++;
}
}
/**
* Check if item order is valid.
*
* @param array $item
* @param array $array
* @return bool
*/
protected static function isItemOrderInvalid($item, $array)
{
return $item['order'] === false || $item['order'] >= count($array);
}
/**
* Determines if content is callable or blade string, processes and returns.
*
* @param mixed $content Pre-processed content
* @param array $data data to use with blade template
* @param mixed $param parameter to call with callable
* @return mixed
*/
public static function compileContent($content, array $data, $param)
{
if (is_string($content)) {
return static::compileBlade($content, static::getMixedValue($data, $param));
}
if (is_callable($content)) {
$reflection = new \ReflectionFunction($content);
$arguments = $reflection->getParameters();
if (count($arguments) > 0) {
return app()->call($content, [$arguments[0]->name => $param]);
}
return $content($param);
}
return $content;
}
/**
* Parses and compiles strings by using Blade Template System.
*
* @param string $str
* @param array $data
* @return mixed
*
* @throws \Exception
*/
public static function compileBlade($str, $data = [])
{
if (view()->exists($str)) {
return view($str, $data)->render();
}
ob_start() && extract($data, EXTR_SKIP);
eval('?>' . app('blade.compiler')->compileString($str));
$str = ob_get_contents();
ob_end_clean();
return $str;
}
/**
* Get a mixed value of custom data and the parameters.
*
* @param array $data
* @param mixed $param
* @return array
*/
public static function getMixedValue(array $data, $param)
{
$casted = self::castToArray($param);
$data['model'] = $param;
foreach ($data as $key => $value) {
if (isset($casted[$key])) {
$data[$key] = $casted[$key];
}
}
return $data;
}
/**
* Cast the parameter into an array.
*
* @param mixed $param
* @return array
*/
public static function castToArray($param)
{
if ($param instanceof \stdClass) {
$param = (array) $param;
return $param;
}
if ($param instanceof Arrayable) {
return $param->toArray();
}
return $param;
}
/**
* Get equivalent or method of query builder.
*
* @param string $method
* @return string
*/
public static function getOrMethod($method)
{
if (! Str::contains(Str::lower($method), 'or')) {
return 'or' . ucfirst($method);
}
return $method;
}
/**
* Converts array object values to associative array.
*
* @param mixed $row
* @param array $filters
* @return array
*/
public static function convertToArray($row, $filters = [])
{
$row = is_object($row) && method_exists($row, 'makeHidden') ? $row->makeHidden(Arr::get($filters, 'hidden', [])) : $row;
$row = is_object($row) && method_exists($row, 'makeVisible') ? $row->makeVisible(Arr::get($filters, 'visible', [])) : $row;
$data = $row instanceof Arrayable ? $row->toArray() : (array) $row;
foreach ($data as &$value) {
if (is_object($value) || is_array($value)) {
$value = self::convertToArray($value);
}
unset($value);
}
return $data;
}
/**
* @param array $data
* @return array
*/
public static function transform(array $data)
{
return array_map(function ($row) {
return self::transformRow($row);
}, $data);
}
/**
* Transform row data into an array.
*
* @param mixed $row
* @return array
*/
protected static function transformRow($row)
{
foreach ($row as $key => $value) {
if ($value instanceof DateTime) {
$row[$key] = $value->format('Y-m-d H:i:s');
} else {
if (is_object($value)) {
$row[$key] = (string) $value;
} else {
$row[$key] = $value;
}
}
}
return $row;
}
/**
* Build parameters depending on # of arguments passed.
*
* @param array $args
* @return array
*/
public static function buildParameters(array $args)
{
$parameters = [];
if (count($args) > 2) {
$parameters[] = $args[0];
foreach ($args[1] as $param) {
$parameters[] = $param;
}
} else {
foreach ($args[0] as $param) {
$parameters[] = $param;
}
}
return $parameters;
}
/**
* Replace all pattern occurrences with keyword.
*
* @param array $subject
* @param string $keyword
* @param string $pattern
* @return array
*/
public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1')
{
$parameters = [];
foreach ($subject as $param) {
if (is_array($param)) {
$parameters[] = self::replacePatternWithKeyword($param, $keyword, $pattern);
} else {
$parameters[] = str_replace($pattern, $keyword, $param);
}
}
return $parameters;
}
/**
* Get column name from string.
*
* @param string $str
* @param bool $wantsAlias
* @return string
*/
public static function extractColumnName($str, $wantsAlias)
{
$matches = explode(' as ', Str::lower($str));
if (! empty($matches)) {
if ($wantsAlias) {
return array_pop($matches);
}
return array_shift($matches);
} elseif (strpos($str, '.')) {
$array = explode('.', $str);
return array_pop($array);
}
return $str;
}
/**
* Adds % wildcards to the given string.
*
* @param string $str
* @param bool $lowercase
* @return string
*/
public static function wildcardLikeString($str, $lowercase = true)
{
return static::wildcardString($str, '%', $lowercase);
}
/**
* Adds wildcards to the given string.
*
* @param string $str
* @param string $wildcard
* @param bool $lowercase
* @return string
*/
public static function wildcardString($str, $wildcard, $lowercase = true)
{
$wild = $wildcard;
$chars = preg_split('//u', $str, -1, PREG_SPLIT_NO_EMPTY);
if (count($chars) > 0) {
foreach ($chars as $char) {
$wild .= $char . $wildcard;
}
}
if ($lowercase) {
$wild = Str::lower($wild);
}
return $wild;
}
}