Completed
Pull Request — master (#1488)
by Elf
01:39
created

DataTables   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 145
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 145
rs 10
c 2
b 0
f 0
wmc 18
lcom 2
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A of() 0 4 1
A make() 0 12 4
A checkType() 0 12 3
A createDataTable() 0 10 3
A getRequest() 0 4 1
A getConfig() 0 4 1
A queryBuilder() 0 4 1
A getHtmlBuilder() 0 8 3
A __call() 0 4 1
1
<?php
2
3
namespace Yajra\DataTables;
4
5
use Exception;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
9
class DataTables
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 function of($source)
34
    {
35
        return $this->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 function make($source)
46
    {
47
        foreach (config('datatables.builders') as $engine => $types) {
48
            foreach ((array) $types as $type) {
49
                if ($this->checkType($source, $type)) {
50
                    return $this->createDataTable($engine, $source);
51
                }
52
            }
53
        }
54
55
        throw new Exception('No available engine for ' . gettype($source));
56
    }
57
58
    /**
59
     * Check whether a variable is the given type.
60
     *
61
     * @param  mixed  $var
62
     * @param  string  $type
63
     * @return bool
64
     */
65
    protected function checkType($var, $type)
66
    {
67
        if (is_object($var)) {
68
            return $var instanceof $type;
69
        }
70
71
        if (function_exists($func = "is_$type")) {
72
            return $func($var);
73
        }
74
75
        return false;
76
    }
77
78
    /**
79
     * Create a new DataTable instance.
80
     *
81
     * @param  string  $engine
82
     * @param  mixed  $source
83
     * @return mixed
84
     * @throws \Exception
85
     */
86
    protected function createDataTable($engine, $source)
87
    {
88
        $class = class_exists($engine) ? $engine : Arr::get(config('datatables.engines'), $engine);
89
90
        if (! $class) {
91
            throw new Exception("Unsupported DataTable engine [$engine]");
92
        }
93
94
        return new $class($source);
95
    }
96
97
    /**
98
     * Get request object.
99
     *
100
     * @return \Yajra\DataTables\Utilities\Request
101
     */
102
    public function getRequest()
103
    {
104
        return app('datatables.request');
105
    }
106
107
    /**
108
     * Get config instance.
109
     *
110
     * @return \Yajra\DataTables\Utilities\Config
111
     */
112
    public function getConfig()
113
    {
114
        return app('datatables.config');
115
    }
116
117
    /**
118
     * @deprecated Please use query() instead, this method will be removed in a next version.
119
     * @param $builder
120
     * @return QueryDataTable
121
     */
122
    public function queryBuilder($builder)
123
    {
124
        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...
125
    }
126
127
    /**
128
     * Get html builder instance.
129
     *
130
     * @return \Yajra\DataTables\Html\Builder
131
     * @throws \Exception
132
     */
133
    public function getHtmlBuilder()
134
    {
135
        if (! class_exists('\Yajra\DataTables\Html\Builder')) {
136
            throw new \Exception('Please install yajra/laravel-datatables-html to be able to use this function.');
137
        }
138
139
        return $this->html ?: $this->html = app('datatables.html');
140
    }
141
142
    /**
143
     * Make a DataTable instance by using method name as engine.
144
     *
145
     * @param  string  $method
146
     * @param  array  $parameters
147
     * @return mixed
148
     */
149
    public function __call($method, $parameters)
150
    {
151
        return $this->createDataTable(Str::snake($method), ...$parameters);
152
    }
153
}
154