Completed
Push — master ( ac2ac0...5540a6 )
by Arjay
02:04 queued 10s
created

DataTables::resource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\DataTables;
4
5
use Illuminate\Support\Traits\Macroable;
6
7
class DataTables
8
{
9
    use Macroable;
10
11
    /**
12
     * DataTables request object.
13
     *
14
     * @var \Yajra\DataTables\Utilities\Request
15
     */
16
    protected $request;
17
18
    /**
19
     * HTML builder instance.
20
     *
21
     * @var \Yajra\DataTables\Html\Builder
22
     */
23
    protected $html;
24
25
    /**
26
     * Make a DataTable instance from source.
27
     * Alias of make for backward compatibility.
28
     *
29
     * @param  mixed $source
30
     * @return mixed
31
     * @throws \Exception
32
     */
33
    public static function of($source)
34
    {
35
        return self::make($source);
36
    }
37
38
    /**
39
     * Make a DataTable instance from source.
40
     *
41
     * @param mixed $source
42
     * @return mixed
43
     * @throws \Exception
44
     */
45
    public static function make($source)
46
    {
47
        $engines  = config('datatables.engines');
48
        $builders = config('datatables.builders');
49
50
        $args = func_get_args();
51
        foreach ($builders as $class => $engine) {
52
            if ($source instanceof $class) {
53
                return call_user_func_array([$engines[$engine], 'create'], $args);
54
            }
55
        }
56
57
        foreach ($engines as $engine => $class) {
58
            if (call_user_func_array([$engines[$engine], 'canCreate'], $args)) {
59
                return call_user_func_array([$engines[$engine], 'create'], $args);
60
            }
61
        }
62
63
        throw new \Exception('No available engine for ' . get_class($source));
64
    }
65
66
    /**
67
     * Get request object.
68
     *
69
     * @return \Yajra\DataTables\Utilities\Request
70
     */
71
    public function getRequest()
72
    {
73
        return app('datatables.request');
74
    }
75
76
    /**
77
     * Get config instance.
78
     *
79
     * @return \Yajra\DataTables\Utilities\Config
80
     */
81
    public function getConfig()
82
    {
83
        return app('datatables.config');
84
    }
85
86
    /**
87
     * @deprecated Please use query() instead, this method will be removed in a next version.
88
     * @param $builder
89
     * @return QueryDataTable
90
     */
91
    public function queryBuilder($builder)
92
    {
93
        return $this->query($builder);
94
    }
95
96
    /**
97
     * DataTables using Query.
98
     *
99
     * @param \Illuminate\Database\Query\Builder|mixed $builder
100
     * @return DataTableAbstract|QueryDataTable
101
     */
102
    public function query($builder)
103
    {
104
        return QueryDataTable::create($builder);
105
    }
106
107
    /**
108
     * DataTables using Eloquent Builder.
109
     *
110
     * @param \Illuminate\Database\Eloquent\Builder|mixed $builder
111
     * @return DataTableAbstract|EloquentDataTable
112
     */
113
    public function eloquent($builder)
114
    {
115
        return EloquentDataTable::create($builder);
116
    }
117
118
    /**
119
     * DataTables using Collection.
120
     *
121
     * @param \Illuminate\Support\Collection|array $collection
122
     * @return DataTableAbstract|CollectionDataTable
123
     */
124
    public function collection($collection)
125
    {
126
        return CollectionDataTable::create($collection);
127
    }
128
129
    /**
130
     * DataTables using Collection.
131
     *
132
     * @param \Illuminate\Http\Resources\Json\AnonymousResourceCollection|array $collection
0 ignored issues
show
Bug introduced by
There is no parameter named $collection. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
133
     * @return DataTableAbstract|ApiResourceDataTable
134
     */
135
    public function resource($resource)
136
    {
137
        return ApiResourceDataTable::create($resource);
138
    }
139
140
    /**
141
     * Get html builder instance.
142
     *
143
     * @return \Yajra\DataTables\Html\Builder
144
     * @throws \Exception
145
     */
146
    public function getHtmlBuilder()
147
    {
148
        if (! class_exists('\Yajra\DataTables\Html\Builder')) {
149
            throw new \Exception('Please install yajra/laravel-datatables-html to be able to use this function.');
150
        }
151
152
        return $this->html ?: $this->html = app('datatables.html');
153
    }
154
}
155