Completed
Pull Request — master (#1488)
by Elf
02:08
created

DataTables::make()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 12
rs 9.4285
1
<?php
2
3
namespace Yajra\DataTables;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use Illuminate\Support\Collection;
9
10
class DataTables
11
{
12
    /**
13
     * DataTables request object.
14
     *
15
     * @var \Yajra\DataTables\Utilities\Request
16
     */
17
    protected $request;
18
19
    /**
20
     * HTML builder instance.
21
     *
22
     * @var \Yajra\DataTables\Html\Builder
23
     */
24
    protected $html;
25
26
    /**
27
     * Make a DataTable instance from source.
28
     * Alias of make for backward compatibility.
29
     *
30
     * @param  mixed $source
31
     * @return mixed
32
     * @throws \Exception
33
     */
34
    public function of($source)
35
    {
36
        return $this->make($source);
37
    }
38
39
    /**
40
     * Make a DataTable instance from source.
41
     *
42
     * @param mixed $source
43
     * @return mixed
44
     * @throws \Exception
45
     */
46
    public function make($source)
47
    {
48
        if (is_array($source)) {
49
            $source = new Collection($source);
50
        }
51
52
        if ($engine = $this->getEngineForSource($source)) {
53
            return $this->createDataTable($engine, $source);
54
        }
55
56
        throw new Exception('No available engine for ' . get_class($source));
57
    }
58
59
    /**
60
     * Get the optimum engine for the given data source.
61
     *
62
     * @param  mixed  $source
63
     * @return string|null
64
     */
65
    protected function getEngineForSource($source)
66
    {
67
        $result = null;
68
69
        foreach (config('datatables.builders') as $type => $engine) {
70
            if ($source instanceof $type) {
71
                if (! isset($tmpType) || is_subclass_of($type, $tmpType)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if $tmpType can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
72
                    $tmpType = $type;
73
                    $result = $engine;
74
                }
75
            }
76
        }
77
78
        return $result;
79
    }
80
81
    /**
82
     * Create a new DataTable instance.
83
     *
84
     * @param  string  $engine
85
     * @param  mixed  $source
86
     * @return mixed
87
     * @throws \Exception
88
     */
89
    protected function createDataTable($engine, $source)
90
    {
91
        if ($class = class_exists($engine) ? $engine : Arr::get(config('datatables.engines'), $engine)) {
92
            return new $class($source);
93
        }
94
95
        throw new Exception("Unsupported DataTable engine [$engine]");
96
    }
97
98
    /**
99
     * Get request object.
100
     *
101
     * @return \Yajra\DataTables\Utilities\Request
102
     */
103
    public function getRequest()
104
    {
105
        return app('datatables.request');
106
    }
107
108
    /**
109
     * Get config instance.
110
     *
111
     * @return \Yajra\DataTables\Utilities\Config
112
     */
113
    public function getConfig()
114
    {
115
        return app('datatables.config');
116
    }
117
118
    /**
119
     * @deprecated Please use query() instead, this method will be removed in a next version.
120
     * @param $builder
121
     * @return QueryDataTable
122
     */
123
    public function queryBuilder($builder)
124
    {
125
        return $this->query($builder);
0 ignored issues
show
Documentation Bug introduced by
The method query does not exist on object<Yajra\DataTables\DataTables>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
126
    }
127
128
    /**
129
     * Get html builder instance.
130
     *
131
     * @return \Yajra\DataTables\Html\Builder
132
     * @throws \Exception
133
     */
134
    public function getHtmlBuilder()
135
    {
136
        if (! class_exists('\Yajra\DataTables\Html\Builder')) {
137
            throw new \Exception('Please install yajra/laravel-datatables-html to be able to use this function.');
138
        }
139
140
        return $this->html ?: $this->html = app('datatables.html');
141
    }
142
143
    /**
144
     * Make a DataTable instance by using method name as engine.
145
     *
146
     * @param  string  $method
147
     * @param  array  $parameters
148
     * @return mixed
149
     */
150
    public function __call($method, $parameters)
151
    {
152
        return $this->createDataTable(Str::snake($method), ...$parameters);
153
    }
154
}
155