Skip to content

Commit 558f9b1

Browse files
committed
refactor: simplify index handling and improve configuration retrieval across DataTable classes
1 parent 6b2c917 commit 558f9b1

File tree

6 files changed

+40
-126
lines changed

6 files changed

+40
-126
lines changed

src/CollectionDataTable.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,7 @@ private function revertIndexColumn($mDataSupport): void
191191
$start = $this->request->start();
192192

193193
$this->collection->transform(function ($data) use ($index, &$start) {
194-
$indexKey = is_string($index) || is_int($index) ? $index : 0;
195-
$data[$indexKey] = ++$start;
194+
$data[$index] = ++$start;
196195

197196
return $data;
198197
});

src/DataTableAbstract.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,9 @@ public function with(mixed $key, mixed $value = ''): static
433433
if (is_array($key)) {
434434
$this->appends = $key;
435435
} else {
436-
$appendsKey = is_string($key) || is_int($key) ? $key : (string) $key;
437-
$this->appends[$appendsKey] = value($value);
436+
/** @var int|string $arrayKey */
437+
$arrayKey = is_int($key) || is_string($key) ? $key : (string) $key;
438+
$this->appends[$arrayKey] = value($value);
438439
}
439440

440441
return $this;

src/DataTables.php

Lines changed: 28 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
use Illuminate\Contracts\Database\Eloquent\Builder as EloquentBuilder;
66
use Illuminate\Contracts\Database\Query\Builder as QueryBuilder;
7+
use Illuminate\Support\Facades\Config;
78
use Illuminate\Support\Traits\Macroable;
89
use Yajra\DataTables\Exceptions\Exception;
9-
use Yajra\DataTables\Utilities\Config;
10+
use Yajra\DataTables\Utilities\Config as DataTablesConfig;
1011
use Yajra\DataTables\Utilities\Request;
1112

1213
class DataTables
@@ -42,123 +43,40 @@ public static function of($source)
4243
*/
4344
public static function make($source)
4445
{
45-
$args = func_get_args();
46-
$engines = (array) config('datatables.engines');
47-
$builders = (array) config('datatables.builders');
48-
49-
$instance = self::tryCreateFromBuilders($source, $builders, $engines, $args);
50-
if ($instance !== null) {
51-
return $instance;
52-
}
53-
54-
$instance = self::tryCreateFromEngines($source, $engines, $args);
55-
if ($instance !== null) {
56-
return $instance;
57-
}
58-
59-
throw new Exception('No available engine for '.$source::class);
60-
}
46+
$engines = Config::array('datatables.engines', []);
47+
$builders = Config::array('datatables.builders', []);
6148

62-
/**
63-
* Try to create a DataTable instance from builders configuration.
64-
*
65-
* @param object $source
66-
*/
67-
private static function tryCreateFromBuilders($source, array $builders, array $engines, array $args): ?DataTableAbstract
68-
{
49+
$args = func_get_args();
6950
foreach ($builders as $class => $engine) {
70-
if (! self::isValidBuilderClass($source, $class)) {
71-
continue;
72-
}
51+
if (is_string($class) && $source instanceof $class) {
52+
/** @var int|string $engineKey */
53+
$engineKey = is_int($engine) || is_string($engine) ? $engine : (string) $engine;
54+
$callback = [$engines[$engineKey], 'create'];
7355

74-
$engineClass = self::getEngineClass($engine, $engines);
75-
if ($engineClass === null) {
76-
continue;
77-
}
56+
if (is_callable($callback)) {
57+
/** @var \Yajra\DataTables\DataTableAbstract $instance */
58+
$instance = call_user_func_array($callback, $args);
7859

79-
$instance = self::createInstance($engineClass, $args);
80-
if ($instance !== null) {
81-
return $instance;
60+
return $instance;
61+
}
8262
}
8363
}
8464

85-
return null;
86-
}
87-
88-
/**
89-
* Try to create a DataTable instance from engines configuration.
90-
*
91-
* @param object $source
92-
*/
93-
private static function tryCreateFromEngines($source, array $engines, array $args): ?DataTableAbstract
94-
{
9565
foreach ($engines as $engine) {
96-
if (! self::canCreateInstance($engine, $args)) {
97-
continue;
98-
}
66+
$canCreate = [$engine, 'canCreate'];
67+
if (is_callable($canCreate) && call_user_func_array($canCreate, $args)) {
68+
$create = [$engine, 'create'];
9969

100-
$instance = self::createInstance($engine, $args);
101-
if ($instance !== null) {
102-
return $instance;
103-
}
104-
}
70+
if (is_callable($create)) {
71+
/** @var \Yajra\DataTables\DataTableAbstract $instance */
72+
$instance = call_user_func_array($create, $args);
10573

106-
return null;
107-
}
108-
109-
/**
110-
* Check if the source is a valid instance of the builder class.
111-
*
112-
* @param object $source
113-
* @param string $class
114-
*/
115-
private static function isValidBuilderClass($source, $class): bool
116-
{
117-
return is_string($class) && class_exists($class) && $source instanceof $class;
118-
}
119-
120-
/**
121-
* Get the engine class from the engine name.
122-
*
123-
* @param mixed $engine
124-
*/
125-
private static function getEngineClass($engine, array $engines): ?string
126-
{
127-
if (! is_string($engine) || ! isset($engines[$engine])) {
128-
return null;
129-
}
130-
131-
return $engines[$engine];
132-
}
133-
134-
/**
135-
* Check if an engine can create an instance with the given arguments.
136-
*
137-
* @param string $engine
138-
*/
139-
private static function canCreateInstance($engine, array $args): bool
140-
{
141-
$canCreate = [$engine, 'canCreate'];
142-
143-
return is_callable($canCreate) && call_user_func_array($canCreate, $args);
144-
}
145-
146-
/**
147-
* Create a DataTable instance from the engine class.
148-
*
149-
* @param string $engineClass
150-
*/
151-
private static function createInstance($engineClass, array $args): ?DataTableAbstract
152-
{
153-
$callback = [$engineClass, 'create'];
154-
if (! is_callable($callback)) {
155-
return null;
74+
return $instance;
75+
}
76+
}
15677
}
15778

158-
/** @var \Yajra\DataTables\DataTableAbstract $instance */
159-
$instance = call_user_func_array($callback, $args);
160-
161-
return $instance;
79+
throw new Exception('No available engine for '.$source::class);
16280
}
16381

16482
/**
@@ -172,7 +90,7 @@ public function getRequest(): Request
17290
/**
17391
* Get config instance.
17492
*/
175-
public function getConfig(): Config
93+
public function getConfig(): DataTablesConfig
17694
{
17795
return app('datatables.config');
17896
}
@@ -184,8 +102,7 @@ public function getConfig(): Config
184102
*/
185103
public function query(QueryBuilder $builder): QueryDataTable
186104
{
187-
/** @var string $dataTable */
188-
$dataTable = config('datatables.engines.query');
105+
$dataTable = Config::string('datatables.engines.query');
189106

190107
$this->validateDataTable($dataTable, QueryDataTable::class);
191108

@@ -199,8 +116,7 @@ public function query(QueryBuilder $builder): QueryDataTable
199116
*/
200117
public function eloquent(EloquentBuilder $builder): EloquentDataTable
201118
{
202-
/** @var string $dataTable */
203-
$dataTable = config('datatables.engines.eloquent');
119+
$dataTable = Config::string('datatables.engines.eloquent');
204120

205121
$this->validateDataTable($dataTable, EloquentDataTable::class);
206122

@@ -216,8 +132,7 @@ public function eloquent(EloquentBuilder $builder): EloquentDataTable
216132
*/
217133
public function collection($collection): CollectionDataTable
218134
{
219-
/** @var string $dataTable */
220-
$dataTable = config('datatables.engines.collection');
135+
$dataTable = Config::string('datatables.engines.collection');
221136

222137
$this->validateDataTable($dataTable, CollectionDataTable::class);
223138

src/Processors/DataProcessor.php

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

8181
if ($this->includeIndex) {
82-
$indexKey = is_string($indexColumn) ? $indexColumn : 'DT_RowIndex';
83-
$value[$indexKey] = ++$this->start;
82+
$value[$indexColumn] = ++$this->start;
8483
}
8584

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

src/QueryDataTable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ public function columnControlSearch(): void
382382
if ($type === 'date') {
383383
try {
384384
// column control replaces / with - on date value
385-
if ($mask && str_contains((string) $mask, '/')) {
385+
if ($mask && str_contains($mask, '/')) {
386386
$value = str_replace('-', '/', $value);
387387
}
388388

src/Utilities/Helper.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Support\Arrayable;
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Facades\Blade;
10+
use Illuminate\Support\Facades\Config;
1011
use Illuminate\Support\Str;
1112
use ReflectionFunction;
1213
use ReflectionMethod;
@@ -18,11 +19,11 @@ class Helper
1819
*/
1920
public static function includeInArray(array $item, array $array): array
2021
{
21-
$itemName = isset($item['name']) && is_string($item['name']) ? $item['name'] : '';
22-
$itemContent = $item['content'] ?? null;
22+
/** @var int|string $itemName */
23+
$itemName = is_int($item['name']) || is_string($item['name']) ? $item['name'] : (string) $item['name'];
2324

2425
if (self::isItemOrderInvalid($item, $array)) {
25-
return array_merge($array, [$itemName => $itemContent]);
26+
return array_merge($array, [$itemName => $item['content']]);
2627
}
2728

2829
$count = 0;
@@ -39,7 +40,7 @@ public static function includeInArray(array $item, array $array): array
3940
$count++;
4041
}
4142

42-
return array_merge($first, [$itemName => $itemContent], $last);
43+
return array_merge($first, [$itemName => $item['content']], $last);
4344
}
4445

4546
/**
@@ -357,8 +358,7 @@ public static function isJavascript(string|array|object|null $value, string $key
357358
return false;
358359
}
359360

360-
/** @var array $callbacks */
361-
$callbacks = config('datatables.callback', ['$', '$.', 'function']);
361+
$callbacks = Config::array('datatables.callback', ['$', '$.', 'function']);
362362

363363
if (Str::startsWith($key, 'language.')) {
364364
return false;

0 commit comments

Comments
 (0)