1: <?php
2:
3: namespace Yajra\Datatables;
4:
5: use DateTime;
6: use Illuminate\Contracts\Support\Arrayable;
7: use Illuminate\Filesystem\Filesystem;
8: use Illuminate\Support\Str;
9: use Illuminate\View\Compilers\BladeCompiler;
10:
11: 12: 13: 14: 15: 16:
17: class Helper
18: {
19: 20: 21: 22: 23: 24: 25:
26: public static function includeInArray($item, $array)
27: {
28: if (self::isItemOrderInvalid($item, $array)) {
29: return array_merge($array, [$item['name'] => $item['content']]);
30: } else {
31: $count = 0;
32: $last = $array;
33: $first = [];
34: foreach ($array as $key => $value) {
35: if ($count == $item['order']) {
36: return array_merge($first, [$item['name'] => $item['content']], $last);
37: }
38:
39: unset($last[$key]);
40: $first[$key] = $value;
41:
42: $count++;
43: }
44: }
45: }
46:
47: 48: 49: 50: 51: 52: 53:
54: protected static function isItemOrderInvalid($item, $array)
55: {
56: return $item['order'] === false || $item['order'] >= count($array);
57: }
58:
59: 60: 61: 62: 63: 64: 65: 66:
67: public static function compileContent($content, array $data, $param)
68: {
69: if (is_string($content)) {
70: return static::compileBlade($content, static::getMixedValue($data, $param));
71: } elseif (is_callable($content)) {
72: return $content($param);
73: }
74:
75: return $content;
76: }
77:
78: 79: 80: 81: 82: 83: 84: 85:
86: public static function compileBlade($str, $data = [])
87: {
88: if (view()->exists($str)) {
89: return view($str, $data)->render();
90: }
91:
92: $empty_filesystem_instance = new Filesystem();
93: $blade = new BladeCompiler($empty_filesystem_instance, 'datatables');
94: $parsed_string = $blade->compileString($str);
95:
96: ob_start() && extract($data, EXTR_SKIP);
97:
98: try {
99: eval('?>' . $parsed_string);
100: } catch (\Exception $e) {
101: ob_end_clean();
102: throw $e;
103: }
104:
105: $str = ob_get_contents();
106: ob_end_clean();
107:
108: return $str;
109: }
110:
111: 112: 113: 114: 115: 116: 117:
118: public static function getMixedValue(array $data, $param)
119: {
120: $param = self::castToArray($param);
121:
122: foreach ($data as $key => $value) {
123: if (isset($param[$key])) {
124: $data[$key] = $param[$key];
125: }
126: }
127:
128: return $data;
129: }
130:
131: 132: 133: 134: 135: 136:
137: public static function castToArray($param)
138: {
139: if ($param instanceof \stdClass) {
140: $param = (array) $param;
141:
142: return $param;
143: }
144:
145: if ($param instanceof Arrayable) {
146: return $param->toArray();
147: }
148:
149: return $param;
150: }
151:
152: 153: 154: 155: 156: 157:
158: public static function getOrMethod($method)
159: {
160: if (! Str::contains(Str::lower($method), 'or')) {
161: return 'or' . ucfirst($method);
162: }
163:
164: return $method;
165: }
166:
167: 168: 169: 170: 171: 172: 173:
174: public static function wrapDatabaseValue($database, $value)
175: {
176: $parts = explode('.', $value);
177: $column = '';
178: foreach ($parts as $key) {
179: $column = static::wrapDatabaseColumn($database, $key, $column);
180: }
181:
182: return substr($column, 0, strlen($column) - 1);
183: }
184:
185: 186: 187: 188: 189: 190: 191: 192:
193: public static function wrapDatabaseColumn($database, $key, $column)
194: {
195: switch ($database) {
196: case 'mysql':
197: $column .= '`' . str_replace('`', '``', $key) . '`' . '.';
198: break;
199:
200: case 'sqlsrv':
201: $column .= '[' . str_replace(']', ']]', $key) . ']' . '.';
202: break;
203:
204: case 'pgsql':
205: case 'sqlite':
206: $column .= '"' . str_replace('"', '""', $key) . '"' . '.';
207: break;
208:
209: default:
210: $column .= $key . '.';
211: }
212:
213: return $column;
214: }
215:
216: 217: 218: 219: 220: 221:
222: public static function convertToArray($row)
223: {
224: $data = $row instanceof Arrayable ? $row->toArray() : (array) $row;
225: foreach (array_keys($data) as $key) {
226: if (is_object($data[$key]) || is_array($data[$key])) {
227: $data[$key] = self::convertToArray($data[$key]);
228: }
229: }
230:
231: return $data;
232: }
233:
234: 235: 236: 237:
238: public static function transform(array $data)
239: {
240: return array_map(function ($row) {
241: return self::transformRow($row);
242: }, $data);
243: }
244:
245: 246: 247: 248: 249: 250:
251: protected static function transformRow($row)
252: {
253: foreach ($row as $key => $value) {
254: if ($value instanceof DateTime) {
255: $row[$key] = $value->format('Y-m-d H:i:s');
256: } else {
257: if (is_object($value)) {
258: $row[$key] = (string) $value;
259: } else {
260: $row[$key] = $value;
261: }
262: }
263: }
264:
265: return $row;
266: }
267:
268: 269: 270: 271: 272: 273:
274: public static function buildParameters(array $args)
275: {
276: $parameters = [];
277:
278: if (count($args) > 2) {
279: $parameters[] = $args[0];
280: foreach ($args[1] as $param) {
281: $parameters[] = $param;
282: }
283: } else {
284: foreach ($args[0] as $param) {
285: $parameters[] = $param;
286: }
287: }
288:
289: return $parameters;
290: }
291:
292: 293: 294: 295: 296: 297: 298: 299:
300: public static function replacePatternWithKeyword(array $subject, $keyword, $pattern = '$1')
301: {
302: $parameters = [];
303: foreach ($subject as $param) {
304: if (is_array($param)) {
305: $parameters[] = self::replacePatternWithKeyword($param, $keyword, $pattern);
306: } else {
307: $parameters[] = str_replace($pattern, $keyword, $param);
308: }
309: }
310:
311: return $parameters;
312: }
313: }
314: