Completed
Push — master ( e46d32...01f863 )
by Arjay
03:35
created

Datatables::of()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\Datatables;
4
5
use Yajra\Datatables\Html\Builder;
6
7
/**
8
 * Class Datatables.
9
 *
10
 * @package Yajra\Datatables
11
 * @author  Arjay Angeles <[email protected]>
12
 */
13
class Datatables
14
{
15
    /**
16
     * Datatables request object.
17
     *
18
     * @var \Yajra\Datatables\Request
19
     */
20
    protected $request;
21
22
    /**
23
     * HTML builder instance.
24
     *
25
     * @var \Yajra\Datatables\Html\Builder
26
     */
27
    protected $html;
28
29
    /**
30
     * Datatables constructor.
31
     *
32
     * @param \Yajra\Datatables\Request $request
33
     */
34
    public function __construct(Request $request)
35
    {
36
        $this->request = $request;
37
    }
38
39
    /**
40
     * Gets query and returns instance of class.
41
     *
42
     * @param  mixed $builder
43
     * @return mixed
44
     * @throws \Exception
45
     */
46
    public static function of($builder)
47
    {
48
        $datatables = app('datatables');
49
        $config     = app('config');
50
        $engines    = $config->get('datatables.engines');
51
        $builders   = $config->get('datatables.builders');
52
53
        foreach ($builders as $class => $engine) {
54
            if ($builder instanceof $class) {
55
                $class = $engines[$engine];
56
57
                return new $class($builder, $datatables->getRequest());
58
            }
59
        }
60
61
        throw new \Exception('No available engine for ' . get_class($builder));
62
    }
63
64
    /**
65
     * Get request object.
66
     *
67
     * @return \Yajra\Datatables\Request
68
     */
69
    public function getRequest()
70
    {
71
        return $this->request;
72
    }
73
74
    /**
75
     * Datatables using Query Builder.
76
     *
77
     * @param \Illuminate\Database\Query\Builder|mixed $builder
78
     * @return \Yajra\Datatables\Engines\QueryBuilderEngine
79
     */
80
    public function queryBuilder($builder)
81
    {
82
        return new Engines\QueryBuilderEngine($builder, $this->request);
83
    }
84
85
    /**
86
     * Datatables using Eloquent Builder.
87
     *
88
     * @param \Illuminate\Database\Eloquent\Builder|mixed $builder
89
     * @return \Yajra\Datatables\Engines\EloquentEngine
90
     */
91
    public function eloquent($builder)
92
    {
93
        return new Engines\EloquentEngine($builder, $this->request);
94
    }
95
96
    /**
97
     * Datatables using Collection.
98
     *
99
     * @param \Illuminate\Support\Collection|mixed $builder
100
     * @return \Yajra\Datatables\Engines\CollectionEngine
101
     */
102
    public function collection($builder)
103
    {
104
        return new Engines\CollectionEngine($builder, $this->request);
105
    }
106
107
    /**
108
     * Get html builder instance.
109
     *
110
     * @return \Yajra\Datatables\Html\Builder
111
     * @throws \Exception
112
     */
113
    public function getHtmlBuilder()
114
    {
115
        if (! class_exists('\Yajra\Datatables\Html\Builder')) {
116
            throw new \Exception('Please install yajra/laravel-datatables-html to be able to use this function.');
117
        }
118
119
        return $this->html ?: $this->html = app('datatables.html');
120
    }
121
}
122