Skip to content

Commit affadde

Browse files
committed
refactor: enhance key retrieval logic for array manipulations across multiple classes
1 parent 4d4c713 commit affadde

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

src/CollectionDataTable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ private function revertIndexColumn($mDataSupport): void
189189
$start = $this->request->start();
190190

191191
$this->collection->transform(function ($data) use ($index, &$start) {
192-
$data[$index] = ++$start;
192+
$indexKey = is_string($index) || is_int($index) ? $index : 0;
193+
$data[$indexKey] = ++$start;
193194

194195
return $data;
195196
});

src/DataTableAbstract.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,8 @@ public function with(mixed $key, mixed $value = ''): static
433433
if (is_array($key)) {
434434
$this->appends = $key;
435435
} else {
436-
$this->appends[$key] = value($value);
436+
$appendsKey = is_string($key) || is_int($key) ? $key : (string) $key;
437+
$this->appends[$appendsKey] = value($value);
437438
}
438439

439440
return $this;

src/DataTables.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ public static function make($source)
4747

4848
$args = func_get_args();
4949
foreach ($builders as $class => $engine) {
50-
if ($source instanceof $class) {
51-
$callback = [$engines[$engine], 'create'];
50+
if (is_string($class) && class_exists($class) && $source instanceof $class) {
51+
$engineClass = is_string($engine) && isset($engines[$engine]) ? $engines[$engine] : null;
52+
if ($engineClass === null) {
53+
continue;
54+
}
55+
$callback = [$engineClass, 'create'];
5256

5357
if (is_callable($callback)) {
5458
/** @var \Yajra\DataTables\DataTableAbstract $instance */

src/Processors/DataProcessor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public function process($object = false): array
7878
$value = $this->removeExcessColumns($value);
7979

8080
if ($this->includeIndex) {
81-
$value[$indexColumn] = ++$this->start;
81+
$indexKey = is_string($indexColumn) ? $indexColumn : 'DT_RowIndex';
82+
$value[$indexKey] = ++$this->start;
8283
}
8384

8485
$this->output[] = $object ? $value : $this->flatten($value);

src/Utilities/Helper.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@ class Helper
1818
*/
1919
public static function includeInArray(array $item, array $array): array
2020
{
21+
$itemName = isset($item['name']) && is_string($item['name']) ? $item['name'] : '';
22+
$itemContent = $item['content'] ?? null;
23+
2124
if (self::isItemOrderInvalid($item, $array)) {
22-
return array_merge($array, [$item['name'] => $item['content']]);
25+
return array_merge($array, [$itemName => $itemContent]);
2326
}
2427

2528
$count = 0;
@@ -36,7 +39,7 @@ public static function includeInArray(array $item, array $array): array
3639
$count++;
3740
}
3841

39-
return array_merge($first, [$item['name'] => $item['content']], $last);
42+
return array_merge($first, [$itemName => $itemContent], $last);
4043
}
4144

4245
/**

0 commit comments

Comments
 (0)