Skip to content

Commit 787b040

Browse files
committed
Move html callback helper to core
1 parent 73c2015 commit 787b040

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

src/Utilities/Helper.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,39 @@ public static function wildcardString($str, $wildcard, $lowercase = true)
327327

328328
return $wild;
329329
}
330+
331+
public static function toJsonScript(array $parameters, int $options = 0): string
332+
{
333+
$values = [];
334+
$replacements = [];
335+
336+
foreach (Arr::dot($parameters) as $key => $value) {
337+
if (self::isJavascript($value, $key)) {
338+
$values[] = trim($value);
339+
Arr::set($parameters, $key, '%'.$key.'%');
340+
$replacements[] = '"%'.$key.'%"';
341+
}
342+
}
343+
344+
$new = [];
345+
foreach ($parameters as $key => $value) {
346+
Arr::set($new, $key, $value);
347+
}
348+
349+
$json = (string) json_encode($new, $options);
350+
351+
return str_replace($replacements, $values, $json);
352+
}
353+
354+
public static function isJavascript(string $value, string $key): bool
355+
{
356+
if (empty($value)) {
357+
return false;
358+
}
359+
360+
/** @var array $callbacks */
361+
$callbacks = config('datatables.callback', ['$', '$.', 'function']);
362+
363+
return Str::startsWith(trim($value), $callbacks) || Str::contains($key, ['editor', 'minDate', 'maxDate']);
364+
}
330365
}

src/config/datatables.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,9 @@
118118
'options' => 0,
119119
],
120120

121+
/*
122+
* Default condition to determine if a parameter is a callback or not.
123+
* Callbacks needs to start by those terms, or they will be cast to string.
124+
*/
125+
'callback' => ['$', '$.', 'function'],
121126
];

0 commit comments

Comments
 (0)