Skip to content

Commit 5c029d7

Browse files
committed
Add htmlBuilder method/class handler.
Add ability to extract service html() method to own class.
1 parent 732c1cc commit 5c029d7

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Contracts;
4+
5+
use Yajra\DataTables\Html\Builder;
6+
7+
interface DataTableHtmlBuilder
8+
{
9+
/**
10+
* Handle building of dataTables html.
11+
*
12+
* @return \Yajra\DataTables\Html\Builder
13+
* @throws \Exception
14+
*/
15+
public function handle();
16+
}

src/Html/DataTableHtml.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html;
4+
5+
use BadMethodCallException;
6+
use Yajra\DataTables\Contracts\DataTableHtmlBuilder;
7+
8+
/**
9+
* @mixin Builder
10+
*/
11+
abstract class DataTableHtml implements DataTableHtmlBuilder
12+
{
13+
/**
14+
* @var \Yajra\DataTables\Html\Builder
15+
*/
16+
protected $htmlBuilder;
17+
18+
/**
19+
* @return \Yajra\DataTables\Html\Builder
20+
*/
21+
public static function make()
22+
{
23+
return app(static::class)->handle();
24+
}
25+
26+
/**
27+
* @param string $name
28+
* @param mixed $arguments
29+
* @return mixed
30+
* @throws \Exception
31+
*/
32+
public function __call($name, $arguments)
33+
{
34+
if (method_exists($this->getHtmlBuilder(), $name)) {
35+
return $this->getHtmlBuilder()->{$name}(...$arguments);
36+
}
37+
38+
throw new BadMethodCallException("Method {$name} does not exists");
39+
}
40+
41+
/**
42+
* @return \Yajra\DataTables\Html\Builder
43+
*/
44+
protected function getHtmlBuilder()
45+
{
46+
if ($this->htmlBuilder) {
47+
return $this->htmlBuilder;
48+
}
49+
50+
return $this->htmlBuilder = app(Builder::class);
51+
}
52+
53+
/**
54+
* @param mixed $builder
55+
* @return static
56+
*/
57+
public function setHtmlBuilder($builder)
58+
{
59+
$this->htmlBuilder = $builder;
60+
61+
return $this;
62+
}
63+
}

src/Services/DataTable.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,15 @@ public function html()
289289
*/
290290
public function builder()
291291
{
292-
return $this->htmlBuilder ?: $this->htmlBuilder = app('datatables.html');
292+
if ($this->htmlBuilder) {
293+
return $this->htmlBuilder;
294+
}
295+
296+
if (method_exists($this, 'htmlBuilder')) {
297+
return $this->htmlBuilder = app()->call([$this, 'htmlBuilder']);
298+
}
299+
300+
return $this->htmlBuilder = app('datatables.html');
293301
}
294302

295303
/**

0 commit comments

Comments
 (0)