Completed
Push — master ( 525fa2...dc2210 )
by Nils
02:11
created

DatatableView::id()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4286
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace OpenSkill\Datatable\Views;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\View\Factory;
7
use Illuminate\Contracts\View\View;
8
use OpenSkill\Datatable\Columns\ColumnConfiguration;
9
10
/**
11
 * Class DatatableView
12
 * @package OpenSkill\Datatable\Views
13
 *
14
 * The class is used to prepare the view with the table and the javascript of the current version.
15
 */
16
class DatatableView
17
{
18
19
    /** @var array the columns map with name -> label */
20
    private $columns;
21
22
    /** @var bool Indicates if the columnConfigurations should be reset on a call to columns. */
23
    private $resetColumns = true;
24
25
    /** @var string The view that should be used to render the table */
26
    private $tableView;
27
28
    /** @var string The view that should be used to render the script */
29
    private $scriptView;
30
31
    /** @var string The id that the table will get in the DOM. Used to create a fitting script for the table */
32
    private $tableId;
33
34
    /** @var array An array of options that should be noted in the script view of the table */
35
    private $scriptOptions = [];
36
37
    /** @var array An array of callback that should be noted in the script view of the table. Only differs in encoding */
38
    private $scriptCallbacks = [];
39
40
    /** @var Factory The factory responsible to render the view with the given data */
41
    private $viewFactory;
42
43
    /** @var bool true if the columns are also printed as headers on the table, false otherwise */
44
    private $printHeaders = false;
45
46
    /** @var Repository The repository responsible for the config value resolution */
47
    private $configRepository;
48
49
    /**
50
     * DatatableView constructor, will take a view as a string if a custom one should be used. will also take the
51
     * column configurations to provide out of the box headers for the view.
52
     * If no columns are given the user must provide them before building the view.
53
     * @param string|null $tableView the name of the view that should be rendered for the table
54
     * @param string|null $scriptView the name of the view that should be rendered for the script
55
     * @param Factory $viewFactory The factory used to render the views
56
     * @param Repository $configRepository The repository responsible for config resolution
57
     * @param ColumnConfiguration[] $columnConfiguration The columnConfiguration of the the server side if available
58
     */
59
    public function __construct(
60
        $tableView,
61
        $scriptView,
62
        Factory $viewFactory,
63
        Repository $configRepository,
64
        array $columnConfiguration = []
65
    ) {
66
        $this->tableView = $tableView;
67
        $this->scriptView = $scriptView;
68
        $this->viewFactory = $viewFactory;
69
        $this->configRepository = $configRepository;
70
        foreach ($columnConfiguration as $item) {
71
            $this->columns[$item->getName()] = $item->getName();
72
        }
73
        // set table id
74
        $this->id($this->configRepository->get('datatable.defaultTableId'));
75
    }
76
77
    /**
78
     * Will set a new id on the table.
79
     * @param string $tableId The new id that should be used for the DOM table
80
     * @return $this
81
     */
82
    public function id($tableId)
83
    {
84
        if (!is_string($tableId)) {
85
            throw new \InvalidArgumentException('$tableId should be a string');
86
        }
87
        $this->tableId = $tableId;
88
        return $this;
89
    }
90
91
    /**
92
     * @param string $name the name of the option
93
     * @param mixed $options an array of options
94
     * @return $this
95
     */
96
    public function option($name, $options)
97
    {
98
        $this->scriptOptions[$name] = $options;
99
        return $this;
100
    }
101
102
    /**
103
     * @param string $name the name of the callback function
104
     * @param string $callback the body of the callback function
105
     * @return $this
106
     */
107
    public function callback($name, $callback)
108
    {
109
        $this->scriptCallbacks[$name] = $callback;
110
        return $this;
111
    }
112
113
    /**
114
     * Indicates that the current columns should have a header on the table
115
     * @return $this
116
     */
117
    public function headers()
118
    {
119
        $this->printHeaders = true;
120
        return $this;
121
    }
122
123
    /**
124
     * Will set the columns for the view
125
     * @param string $columnName The name of the column
126
     * @param string $label The label for this column
127
     * @return $this
128
     */
129
    public function columns($columnName, $label = null)
130
    {
131
        if (!is_string($columnName)) {
132
            throw new \InvalidArgumentException('$columnName must be set');
133
        }
134
135
        if ($this->resetColumns) {
136
            $this->columns = [];
137
            $this->resetColumns = false;
138
        }
139
        if (is_null($label)) {
140
            $label = $columnName;
141
        }
142
        $this->columns[$columnName] = $label;
143
        return $this;
144
    }
145
146
    /**
147
     * Will render the table
148
     *
149
     * @return string the rendered view that represents the table
150
     */
151 View Code Duplication
    public function table()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153
        if (empty($this->columns)) {
154
            throw new \InvalidArgumentException("There are no columns defined");
155
        }
156
157
        return $this->viewFactory
158
            ->make($this->tableView, [
159
                'columns' => $this->columns,
160
                'showHeaders' => $this->printHeaders,
161
                'id' => $this->tableId
162
            ])
163
            ->render();
164
    }
165
166
    /**
167
     * Will render the javascript for the table
168
     *
169
     * @return string the rendered view that represents the script
170
     */
171 View Code Duplication
    public function script()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
172
    {
173
        if (empty($this->columns)) {
174
            throw new \InvalidArgumentException("There are no columns defined");
175
        }
176
        return $this->viewFactory
177
            ->make($this->scriptView, [
178
                'id' => $this->tableId,
179
                'columns' => $this->columns,
180
                'options' => $this->scriptOptions,
181
                'callbacks' => $this->scriptCallbacks,
182
            ])
183
            ->render();
184
    }
185
186
    /**
187
     * Will render the table and directly the script after it. This is a shortcut for
188
     * {@link #table} followed by {@link script}
189
     * @return string Will return the rendered table and the rendered script as string
190
     */
191
    public function html()
192
    {
193
        return $this->table() . $this->script();
194
    }
195
}