Skip to content

Commit 94ca929

Browse files
authored
Merge pull request #2861 from OzanKurt/patch-3
Methods pointing to the "uncustomizable" classes.
2 parents 7067ea6 + a79a273 commit 94ca929

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

src/DataTables.php

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,12 @@ public function getConfig()
112112
*/
113113
public function query(QueryBuilder $builder): QueryDataTable
114114
{
115-
return QueryDataTable::create($builder);
115+
/** @var string */
116+
$dataTable = config('datatables.engines.query');
117+
118+
$this->validateDataTable($dataTable, QueryDataTable::class);
119+
120+
return $dataTable::create($builder);
116121
}
117122

118123
/**
@@ -123,7 +128,12 @@ public function query(QueryBuilder $builder): QueryDataTable
123128
*/
124129
public function eloquent(EloquentBuilder $builder): EloquentDataTable
125130
{
126-
return EloquentDataTable::create($builder);
131+
/** @var string */
132+
$dataTable = config('datatables.engines.eloquent');
133+
134+
$this->validateDataTable($dataTable, EloquentDataTable::class);
135+
136+
return $dataTable::create($builder);
127137
}
128138

129139
/**
@@ -134,7 +144,12 @@ public function eloquent(EloquentBuilder $builder): EloquentDataTable
134144
*/
135145
public function collection($collection): CollectionDataTable
136146
{
137-
return CollectionDataTable::create($collection);
147+
/** @var string */
148+
$dataTable = config('datatables.engines.collection');
149+
150+
$this->validateDataTable($dataTable, CollectionDataTable::class);
151+
152+
return $dataTable::create($collection);
138153
}
139154

140155
/**
@@ -163,4 +178,32 @@ public function getHtmlBuilder()
163178

164179
return $this->html ?: $this->html = app('datatables.html');
165180
}
181+
182+
/**
183+
* @param string $engine
184+
* @param string $parent
185+
*
186+
* @return void
187+
*
188+
* @throws \Yajra\DataTables\Exceptions\Exception
189+
*/
190+
public function validateDataTable(string $engine, string $parent): void
191+
{
192+
if (! ($engine == $parent || is_subclass_of($engine, $parent))) {
193+
$this->throwInvalidEngineException($engine, $parent);
194+
}
195+
}
196+
197+
/**
198+
* @param string $engine
199+
* @param string $parent
200+
*
201+
* @return void
202+
*
203+
* @throws \Yajra\DataTables\Exceptions\Exception
204+
*/
205+
public function throwInvalidEngineException(string $engine, string $parent): void
206+
{
207+
throw new Exception("The given datatable engine `{$engine}` is not compatible with `{$parent}`.");
208+
}
166209
}

0 commit comments

Comments
 (0)