Completed
Push — master ( 8fe0a2...525fa2 )
by Nils
02:51
created

DatatableView::table()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 7
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
namespace OpenSkill\Datatable\Views;
4
5
use Illuminate\Contracts\View\Factory;
6
use Illuminate\Contracts\View\View;
7
use OpenSkill\Datatable\Columns\ColumnConfiguration;
8
use OpenSkill\Datatable\Versions\Version;
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
    /**
20
     * @var ColumnConfiguration[] the column configuration if any
21
     */
22
    private $columnConfigurations;
23
24
    /**
25
     * Indicates if the columnConfigurations should be reset on a call to columns.
26
     */
27
    private $resetColumns = true;
28
    /**
29
     * @var null|string
30
     */
31
    private $tableView;
32
    /**
33
     * @var null|string
34
     */
35
    private $scriptView;
36
    /**
37
     * @var null|Version
38
     */
39
    private $version;
40
41
    /**
42
     * @var Factory
43
     */
44
    private $viewFactory;
45
46
    /**
47
     * @var bool true if the columns are also printed as headers on the table, false otherwise
48
     */
49
    private $printHeaders;
50
51
    /**
52
     * DatatableView constructor, will take a view as a string if a custom one should be used. will also take the
53
     * column configurations to provide out of the box headers for the view.
54
     * If no columns are given the user must provide them before building the view.
55
     * @param string|null $tableView the name of the view that should be rendered for the table
56
     * @param string|null $scriptView the name of the view that should be rendered for the script
57
     * @param Version|null $version The version that supports the current request
58
     * @param Factory $viewFactory The factory used to render the views
59
     * @param array $columnConfiguration The columnConfiguration of the the server side if available
60
     */
61
    public function __construct(
62
        $tableView = null,
63
        $scriptView = null,
64
        Version $version = null,
65
        Factory $viewFactory,
66
        array $columnConfiguration = []
67
    ) {
68
        $this->columnConfigurations = $columnConfiguration;
69
        $this->tableView = $tableView;
70
        $this->scriptView = $scriptView;
71
        $this->version = $version;
72
        $this->viewFactory = $viewFactory;
73
    }
74
75
    /**
76
     * Indicates that the current columns should have a header on the table
77
     */
78
    public function headers() {
79
        $this->printHeaders = true;
80
    }
81
82
    /**
83
     * Will set the columns for the view
84
     */
85
    public function columns()
86
    {
87
        if ($this->resetColumns) {
88
            $this->columnConfigurations = [];
89
            $this->resetColumns = false;
90
        }
91
    }
92
93
    /**
94
     * Will render the table
95
     *
96
     * @return View the view that represents the table
97
     */
98
    public function table()
99
    {
100
        if (empty($this->columnConfigurations)) {
101
            throw new \InvalidArgumentException("There are no columns defined");
102
        }
103
        return $this->viewFactory->make($this->tableView);
104
    }
105
106
    /**
107
     * Will render the javascript for the table
108
     *
109
     * @return View the view that represents the script
110
     */
111
    public function script()
112
    {
113
        if (empty($this->columnConfigurations)) {
114
            throw new \InvalidArgumentException("There are no columns defined");
115
        }
116
        return $this->viewFactory->make($this->tableView);
117
    }
118
}