Completed
Push — master ( 6cdc89...7b03c6 )
by Arjay
02:14
created

DataTables   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 128
rs 10
c 0
b 0
f 0
wmc 14
lcom 2
cbo 4

8 Methods

Rating   Name   Duplication   Size   Complexity  
A of() 0 4 1
A make() 0 19 4
A getRequest() 0 4 1
A getConfig() 0 4 1
A queryBuilder() 0 4 1
A eloquent() 0 4 1
A collection() 0 8 2
A getHtmlBuilder() 0 8 3
1
<?php
2
3
namespace Yajra\DataTables;
4
5
use Illuminate\Support\Collection;
6
7
class DataTables
8
{
9
    /**
10
     * DataTables request object.
11
     *
12
     * @var \Yajra\DataTables\Utilities\Request
13
     */
14
    protected $request;
15
16
    /**
17
     * HTML builder instance.
18
     *
19
     * @var \Yajra\DataTables\Html\Builder
20
     */
21
    protected $html;
22
23
    /**
24
     * Make a DataTable instance from source.
25
     * Alias of make for backward compatibility.
26
     *
27
     * @param  mixed $source
28
     * @return mixed
29
     * @throws \Exception
30
     */
31
    public static function of($source)
32
    {
33
        return self::make($source);
34
    }
35
36
    /**
37
     * Make a DataTable instance from source.
38
     *
39
     * @param mixed $source
40
     * @return mixed
41
     * @throws \Exception
42
     */
43
    public static function make($source)
44
    {
45
        $engines  = config('datatables.engines');
46
        $builders = config('datatables.builders');
47
48
        if (is_array($source)) {
49
            $source = new Collection($source);
50
        }
51
52
        foreach ($builders as $class => $engine) {
53
            if ($source instanceof $class) {
54
                $class = $engines[$engine];
55
56
                return new $class($source);
57
            }
58
        }
59
60
        throw new \Exception('No available engine for ' . get_class($source));
61
    }
62
63
    /**
64
     * Get request object.
65
     *
66
     * @return \Yajra\DataTables\Utilities\Request
67
     */
68
    public function getRequest()
69
    {
70
        return resolve('datatables.request');
71
    }
72
73
    /**
74
     * Get config instance.
75
     *
76
     * @return \Yajra\DataTables\Utilities\Config
77
     */
78
    public function getConfig()
79
    {
80
        return resolve('datatables.config');
81
    }
82
83
    /**
84
     * DataTables using Query Builder.
85
     *
86
     * @param \Illuminate\Database\Query\Builder|mixed $builder
87
     * @return \Yajra\DataTables\QueryDataTable
88
     */
89
    public function queryBuilder($builder)
90
    {
91
        return new QueryDataTable($builder);
92
    }
93
94
    /**
95
     * DataTables using Eloquent Builder.
96
     *
97
     * @param \Illuminate\Database\Eloquent\Builder|mixed $builder
98
     * @return \Yajra\DataTables\EloquentDataTable
99
     */
100
    public function eloquent($builder)
101
    {
102
        return new EloquentDataTable($builder);
103
    }
104
105
    /**
106
     * DataTables using Collection.
107
     *
108
     * @param \Illuminate\Support\Collection|array $collection
109
     * @return \Yajra\DataTables\CollectionDataTable
110
     */
111
    public function collection($collection)
112
    {
113
        if (is_array($collection)) {
114
            $collection = new Collection($collection);
115
        }
116
117
        return new CollectionDataTable($collection);
118
    }
119
120
    /**
121
     * Get html builder instance.
122
     *
123
     * @return \Yajra\DataTables\Html\Builder
124
     * @throws \Exception
125
     */
126
    public function getHtmlBuilder()
127
    {
128
        if (!class_exists('\Yajra\DataTables\Html\Builder')) {
129
            throw new \Exception('Please install yajra/laravel-datatables-html to be able to use this function.');
130
        }
131
132
        return $this->html ?: $this->html = resolve('datatables.html');
133
    }
134
}
135