Completed
Push — master ( 7fba76...4bc414 )
by Tim
04:07 queued 01:48
created

DatatableView::endpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
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
    /** @var string the URL for the endpoint */
50
    private $endpointURL = '/';
51
52
    /**
53
     * DatatableView constructor, will take a view as a string if a custom one should be used. will also take the
54
     * column configurations to provide out of the box headers for the view.
55
     * If no columns are given the user must provide them before building the view.
56
     * @param string|null $tableView the name of the view that should be rendered for the table
57
     * @param string|null $scriptView the name of the view that should be rendered for the script
58
     * @param Factory $viewFactory The factory used to render the views
59
     * @param Repository $configRepository The repository responsible for config resolution
60
     * @param ColumnConfiguration[] $columnConfiguration The columnConfiguration of the the server side if available
61
     */
62
    public function __construct(
63
        $tableView,
64
        $scriptView,
65
        Factory $viewFactory,
66
        Repository $configRepository,
67
        array $columnConfiguration = []
68
    ) {
69
        $this->tableView = $tableView;
70
        $this->scriptView = $scriptView;
71
        $this->viewFactory = $viewFactory;
72
        $this->configRepository = $configRepository;
73
        foreach ($columnConfiguration as $item) {
74
            $this->columns[$item->getName()] = $item->getName();
75
        }
76
        // set table id
77
        $this->id($this->configRepository->get('datatable.defaultTableId'));
78
    }
79
80
    /**
81
     * Will set a new id on the table.
82
     * @param string $tableId The new id that should be used for the DOM table
83
     * @return $this
84
     */
85
    public function id($tableId)
86
    {
87
        if (!is_string($tableId)) {
88
            throw new \InvalidArgumentException('$tableId should be a string');
89
        }
90
        $this->tableId = $tableId;
91
        return $this;
92
    }
93
94
    /**
95
     * @param string $name the name of the option
96
     * @param mixed $options an array of options
97
     * @return $this
98
     */
99
    public function option($name, $options)
100
    {
101
        $this->scriptOptions[$name] = $options;
102
        return $this;
103
    }
104
105
    /**
106
     * @param string $name the name of the callback function
107
     * @param string $callback the body of the callback function
108
     * @return $this
109
     */
110
    public function callback($name, $callback)
111
    {
112
        $this->scriptCallbacks[$name] = $callback;
113
        return $this;
114
    }
115
116
    /**
117
     * Indicates that the current columns should have a header on the table
118
     * @return $this
119
     */
120
    public function headers()
121
    {
122
        $this->printHeaders = true;
123
        return $this;
124
    }
125
126
    /**
127
     * Sets the endpoint URL that will be passed to templates when rendering html & scripts.
128
     * @param $endpoint_url
129
     * @return $this
130
     */
131
    public function endpoint($endpoint_url)
132
    {
133
        $this->endpointURL = $endpoint_url;
134
        return $this;
135
    }
136
137
    /**
138
     * Will set the columns for the view
139
     * @param string $columnName The name of the column
140
     * @param string $label The label for this column
141
     * @return $this
142
     */
143
    public function columns($columnName, $label = null)
144
    {
145
        if (!is_string($columnName)) {
146
            throw new \InvalidArgumentException('$columnName must be set');
147
        }
148
149
        if ($this->resetColumns) {
150
            $this->columns = [];
151
            $this->resetColumns = false;
152
        }
153
        if (is_null($label)) {
154
            $label = $columnName;
155
        }
156
        $this->columns[$columnName] = $label;
157
        return $this;
158
    }
159
160
    /**
161
     * Will render the table
162
     *
163
     * @return string the rendered view that represents the table
164
     */
165 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...
166
    {
167
        if (empty($this->columns)) {
168
            throw new \InvalidArgumentException("There are no columns defined");
169
        }
170
171
        return $this->viewFactory
172
            ->make($this->tableView, [
173
                'columns' => $this->columns,
174
                'showHeaders' => $this->printHeaders,
175
                'id' => $this->tableId,
176
                'endpoint' => $this->endpointURL,
177
            ])
178
            ->render();
179
    }
180
181
    /**
182
     * Will render the javascript for the table
183
     *
184
     * @return string the rendered view that represents the script
185
     */
186 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...
187
    {
188
        if (empty($this->columns)) {
189
            throw new \InvalidArgumentException("There are no columns defined");
190
        }
191
        return $this->viewFactory
192
            ->make($this->scriptView, [
193
                'id' => $this->tableId,
194
                'columns' => $this->columns,
195
                'options' => $this->scriptOptions,
196
                'callbacks' => $this->scriptCallbacks,
197
                'endpoint' => $this->endpointURL,
198
            ])
199
            ->render();
200
    }
201
202
    /**
203
     * Will render the table and directly the script after it. This is a shortcut for
204
     * {@link #table} followed by {@link script}
205
     * @return string Will return the rendered table and the rendered script as string
206
     */
207
    public function html()
208
    {
209
        return $this->table() . $this->script();
210
    }
211
}