|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace OpenSkill\Datatable\Composers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
|
6
|
|
|
use Illuminate\Contracts\View\Factory; |
|
7
|
|
|
use OpenSkill\Datatable\Columns\ColumnConfiguration; |
|
8
|
|
|
use OpenSkill\Datatable\Columns\ColumnConfigurationBuilder; |
|
9
|
|
|
use OpenSkill\Datatable\Columns\Orderable\Orderable; |
|
10
|
|
|
use OpenSkill\Datatable\Columns\Searchable\Searchable; |
|
11
|
|
|
use OpenSkill\Datatable\DatatableService; |
|
12
|
|
|
use OpenSkill\Datatable\Providers\Provider; |
|
13
|
|
|
use OpenSkill\Datatable\Versions\VersionEngine; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class ColumnComposer |
|
17
|
|
|
* @package OpenSkill\Datatable\Composers |
|
18
|
|
|
* |
|
19
|
|
|
* The composer is responsible to collect all column configuration as well as view configurations and to pass them |
|
20
|
|
|
* to the DTProvider when the data needs to be collected. |
|
21
|
|
|
*/ |
|
22
|
|
|
class ColumnComposer |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Provider The Provider for the underlying data. |
|
26
|
|
|
*/ |
|
27
|
|
|
private $provider; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var VersionEngine The version engine that will parse the request parameter. |
|
31
|
|
|
*/ |
|
32
|
|
|
private $version; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var ColumnConfiguration[] An array of the configurations of the columns |
|
36
|
|
|
*/ |
|
37
|
|
|
private $columnConfiguration = []; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var Factory |
|
41
|
|
|
*/ |
|
42
|
|
|
private $viewFactory; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var Repository |
|
46
|
|
|
*/ |
|
47
|
|
|
private $configRepository; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Will create a new datatable composer instance with the given provider |
|
51
|
|
|
* @param Provider $provider the provider that will process the underlying data |
|
52
|
|
|
* @param VersionEngine $versionEngine The version engine to handle the request data |
|
53
|
|
|
* @param Factory $viewFactory The factory providing the views |
|
54
|
|
|
* @param Repository $configRepository The repository providing the user defined options |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __construct( |
|
57
|
|
|
Provider $provider, |
|
58
|
|
|
VersionEngine $versionEngine, |
|
59
|
|
|
Factory $viewFactory, |
|
60
|
|
|
Repository $configRepository |
|
61
|
|
|
) { |
|
62
|
|
|
$this->provider = $provider; |
|
63
|
|
|
$this->version = $versionEngine; |
|
64
|
|
|
$this->viewFactory = $viewFactory; |
|
65
|
|
|
$this->configRepository = $configRepository; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Will return the Provider for the underlying data. |
|
70
|
|
|
* @return Provider |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getProvider() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->provider; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Will return the internal column configurations that are registered with the current composer. |
|
79
|
|
|
* |
|
80
|
|
|
* @return ColumnConfiguration[] |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getColumnConfiguration() |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->columnConfiguration; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Will create a new ColumnConfiguration with all defaults but allows overriding of all properties through the method. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name The name of the configuration, required for the configuration |
|
91
|
|
|
* @param string|callable $callable The function to execute, defaults to null which means the default will be set. |
|
92
|
|
|
* @param Searchable $searchable If the column should be searchable or not |
|
93
|
|
|
* @param Orderable $orderable If the column should be orderable or not |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function column($name, $callable = null, Searchable $searchable = null, Orderable $orderable = null) |
|
97
|
|
|
{ |
|
98
|
|
|
/** |
|
99
|
|
|
* @var ColumnConfigurationBuilder |
|
100
|
|
|
*/ |
|
101
|
|
|
$config = ColumnConfigurationBuilder::create(); |
|
102
|
|
|
|
|
103
|
|
|
if (is_string($name)) { |
|
104
|
|
|
$config->name($name); |
|
105
|
|
|
} else { |
|
106
|
|
|
throw new \InvalidArgumentException('$name must be a string'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (!is_null($callable)) { |
|
110
|
|
|
$this->setCallableColumn($config, $callable); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (is_null($searchable)) { |
|
114
|
|
|
$config->searchable(Searchable::NORMAL()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
if (is_null($orderable)) { |
|
118
|
|
|
$config->orderable(Orderable::BOTH()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$this->columnConfiguration[] = $config->build(); |
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* This method will add the given ColumnConfiguration to the composer. |
|
127
|
|
|
* |
|
128
|
|
|
* @param ColumnConfiguration $configuration the configuration to add to the composer |
|
129
|
|
|
* |
|
130
|
|
|
* @return $this |
|
131
|
|
|
*/ |
|
132
|
|
|
public function add(ColumnConfiguration $configuration) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->columnConfiguration[] = $configuration; |
|
135
|
|
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return DatatableService Will return the fully built DatatableService that will contain the ColumnConfiguration |
|
140
|
|
|
*/ |
|
141
|
|
|
public function build() |
|
142
|
|
|
{ |
|
143
|
|
|
return new DatatableService( |
|
144
|
|
|
$this->provider, |
|
145
|
|
|
$this->columnConfiguration, |
|
146
|
|
|
$this->version, |
|
147
|
|
|
$this->viewFactory, |
|
148
|
|
|
$this->configRepository |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Determine a sane configuration value for a column's callable function |
|
154
|
|
|
* @param $config |
|
155
|
|
|
* @param $callable |
|
156
|
|
|
*/ |
|
157
|
|
|
private function setCallableColumn($config, $callable) |
|
158
|
|
|
{ |
|
159
|
|
|
if (is_callable($callable)) { |
|
160
|
|
|
$config->withCallable($callable); |
|
161
|
|
|
} elseif (is_string($callable)) { |
|
162
|
|
|
$config->withCallable(function () use ($callable) { |
|
163
|
|
|
return $callable; |
|
164
|
|
|
}); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |