Skip to content

Commit d805449

Browse files
committed
Add column authorization
Fix #162
1 parent 482dfe7 commit d805449

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

src/Html/Column.php

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@
3434
class Column extends Fluent
3535
{
3636
use SearchPanes;
37+
use HasAuthorizations;
3738

3839
/**
3940
* @param array $attributes
4041
*/
4142
public function __construct($attributes = [])
4243
{
43-
$attributes['title'] ??= self::titleFormat($attributes['data']);
44+
$attributes['title'] ??= self::titleFormat($attributes['data'] ?? '');
4445
$attributes['orderable'] ??= true;
4546
$attributes['searchable'] ??= true;
4647
$attributes['exportable'] ??= true;
@@ -74,6 +75,20 @@ public static function titleFormat(string $value): string
7475
return Str::title(str_replace(['.', '_'], ' ', Str::snake($value)));
7576
}
7677

78+
/**
79+
* Set column title.
80+
*
81+
* @param string $value
82+
* @return $this
83+
* @see https://datatables.net/reference/option/columns.title
84+
*/
85+
public function title(string $value): static
86+
{
87+
$this->attributes['title'] = $value;
88+
89+
return $this;
90+
}
91+
7792
/**
7893
* Create a computed column that is not searchable/orderable.
7994
*
@@ -118,33 +133,22 @@ public function orderable(bool $flag = true): static
118133
return $this;
119134
}
120135

121-
/**
122-
* Set column title.
123-
*
124-
* @param string $value
125-
* @return $this
126-
* @see https://datatables.net/reference/option/columns.title
127-
*/
128-
public function title(string $value): static
129-
{
130-
$this->attributes['title'] = $value;
131-
132-
return $this;
133-
}
134-
135136
/**
136137
* Make a new column instance.
137138
*
138-
* @param string $data
139+
* @param array|string $data
139140
* @param string $name
140141
* @return static
141142
*/
142-
public static function make(string $data, string $name = ''): static
143+
public static function make(array|string $data, string $name = ''): static
143144
{
144-
$attr = [
145-
'data' => $data,
146-
'name' => $name ?: $data,
147-
];
145+
$attr = $data;
146+
if (is_string($data)) {
147+
$attr = [
148+
'data' => $data,
149+
'name' => $name ?: $data,
150+
];
151+
}
148152

149153
return new static($attr);
150154
}
@@ -600,6 +604,10 @@ public function exportFormat(string $format): static
600604
*/
601605
public function toArray(): array
602606
{
607+
if (! $this->isAuthorized()) {
608+
return [];
609+
}
610+
603611
return Arr::except($this->attributes, [
604612
'printable',
605613
'exportable',

src/Html/HasAuthorizations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public static function makeIfCannot(
8888
return static::make($options);
8989
}
9090

91-
return static::make([])->authorized(false);
91+
return app(static::class)->authorized(false);
9292
}
9393

9494
/**

tests/ColumnTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,5 +189,37 @@ public function it_can_add_class()
189189
$this->assertEquals('text-sm font-bold', $column->className);
190190
}
191191

192+
/** @test */
193+
public function it_has_authorizations()
194+
{
195+
$column = Column::makeIf(true, 'name');
196+
$this->assertEquals([
197+
'name' => 'name',
198+
'data' => 'name',
199+
'title' => 'Name',
200+
'orderable' => true,
201+
'searchable' => true,
202+
'attributes' => [],
203+
], $column->toArray());
204+
205+
$column = Column::makeIf(false, 'name');
206+
$this->assertEquals([], $column->toArray());
207+
}
192208

209+
/** @test */
210+
public function it_can_be_serialized()
211+
{
212+
$column = Column::make('name');
213+
$this->assertEquals([
214+
'name' => 'name',
215+
'data' => 'name',
216+
'title' => 'Name',
217+
'orderable' => true,
218+
'searchable' => true,
219+
'attributes' => [],
220+
], $column->toArray());
221+
222+
$expected = '{"data":"name","name":"name","title":"Name","orderable":true,"searchable":true,"attributes":[]}';
223+
$this->assertEquals($expected, $column->toJson());
224+
}
193225
}

0 commit comments

Comments
 (0)