Completed
Push — master ( 928d8a...2f7023 )
by Arjay
07:15
created

Factory::getRequest()   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 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\DataTables;
4
5
use Illuminate\Support\Collection;
6
7
class Factory
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
     * Gets query and returns instance of class.
25
     *
26
     * @param  mixed $source
27
     * @return mixed
28
     * @throws \Exception
29
     */
30
    public static function of($source)
31
    {
32
        $engines  = config('datatables.engines');
33
        $builders = config('datatables.builders');
34
35
        if (is_array($source)) {
36
            $source = new Collection($source);
37
        }
38
39
        foreach ($builders as $class => $engine) {
40
            if ($source instanceof $class) {
41
                $class = $engines[$engine];
42
43
                return new $class($source);
44
            }
45
        }
46
47
        throw new \Exception('No available engine for ' . get_class($source));
48
    }
49
50
    /**
51
     * Get request object.
52
     *
53
     * @return \Yajra\DataTables\Utilities\Request
54
     */
55
    public function getRequest()
56
    {
57
        return resolve('datatables.request');
58
    }
59
60
    /**
61
     * Get config instance.
62
     *
63
     * @return \Yajra\DataTables\Utilities\Config
64
     */
65
    public function getConfig()
66
    {
67
        return resolve('datatables.config');
68
    }
69
70
    /**
71
     * DataTables using Query Builder.
72
     *
73
     * @param \Illuminate\Database\Query\Builder|mixed $builder
74
     * @return \Yajra\DataTables\QueryDataTable
75
     */
76
    public function queryBuilder($builder)
77
    {
78
        return new QueryDataTable($builder);
79
    }
80
81
    /**
82
     * DataTables using Eloquent Builder.
83
     *
84
     * @param \Illuminate\Database\Eloquent\Builder|mixed $builder
85
     * @return \Yajra\DataTables\EloquentDataTable
86
     */
87
    public function eloquent($builder)
88
    {
89
        return new EloquentDataTable($builder);
90
    }
91
92
    /**
93
     * DataTables using Collection.
94
     *
95
     * @param \Illuminate\Support\Collection|array $collection
96
     * @return \Yajra\DataTables\CollectionDataTable
97
     */
98
    public function collection($collection)
99
    {
100
        if (is_array($collection)) {
101
            $collection = new Collection($collection);
102
        }
103
104
        return new CollectionDataTable($collection);
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 = resolve('datatables.html');
120
    }
121
}
122