Skip to content

Commit ce1c4c3

Browse files
committed
Migrate more methods into traits
1 parent 44dd5f5 commit ce1c4c3

File tree

6 files changed

+84
-69
lines changed

6 files changed

+84
-69
lines changed

src/Views/Columns/Traits/Configuration/ColumnConfiguration.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,9 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Configuration;
44

5-
use Rappasoft\LaravelLivewireTables\Views\Column;
65

76
trait ColumnConfiguration
87
{
9-
public function label(callable $callback): self
10-
{
11-
$this->from = null;
12-
$this->field = null;
13-
$this->labelCallback = $callback;
14-
15-
return $this;
16-
}
17-
18-
public function format(callable $callable): Column
19-
{
20-
$this->formatCallback = $callable;
21-
22-
return $this;
23-
}
248

259
public function html(): self
2610
{
@@ -36,13 +20,6 @@ public function setTable(string $table): self
3620
return $this;
3721
}
3822

39-
public function eagerLoadRelations(): self
40-
{
41-
$this->eagerLoadRelations = true;
42-
43-
return $this;
44-
}
45-
4623
public function unclickable(): self
4724
{
4825
$this->clickable = false;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits;
4+
5+
use Rappasoft\LaravelLivewireTables\Views\Column;
6+
7+
trait HasLabelFormat
8+
{
9+
protected mixed $labelCallback = null;
10+
11+
protected mixed $formatCallback = null;
12+
13+
public function hasFormatter(): bool
14+
{
15+
return $this->formatCallback !== null;
16+
}
17+
18+
public function getFormatCallback(): ?callable
19+
{
20+
return $this->formatCallback;
21+
}
22+
23+
// TODO
24+
public function getLabelCallback(): ?callable
25+
{
26+
return $this->labelCallback;
27+
}
28+
29+
public function label(callable $callback): self
30+
{
31+
$this->from = null;
32+
$this->field = null;
33+
$this->labelCallback = $callback;
34+
35+
return $this;
36+
}
37+
38+
public function format(callable $callable): Column
39+
{
40+
$this->formatCallback = $callable;
41+
42+
return $this;
43+
}
44+
}

src/Views/Columns/Traits/Helpers/RelationshipHelpers.php renamed to src/Views/Columns/Traits/HasRelations.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
<?php
22

3-
namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers;
3+
namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits;
44

55
use Illuminate\Support\Collection;
66

7-
trait RelationshipHelpers
7+
trait HasRelations
88
{
9+
// An array of relationships: i.e. address.group.name => ['address', 'group']
10+
protected array $relations = [];
11+
12+
protected bool $eagerLoadRelations = false;
13+
914
public function isBaseColumn(): bool
1015
{
1116
return ! $this->hasRelations() && $this->hasField();
@@ -29,4 +34,17 @@ public function getRelationString(): ?string
2934

3035
return null;
3136
}
32-
}
37+
38+
public function eagerLoadRelations(): self
39+
{
40+
$this->eagerLoadRelations = true;
41+
42+
return $this;
43+
}
44+
45+
public function eagerLoadRelationsIsEnabled(): bool
46+
{
47+
return $this->eagerLoadRelations === true;
48+
}
49+
50+
}

src/Views/Columns/Traits/HasCustomSlug.php renamed to src/Views/Columns/Traits/HasSlug.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits;
44

5-
trait HasCustomSlug
5+
use Illuminate\Support\Str;
6+
7+
trait HasSlug
68
{
79
protected ?string $customSlug = null;
810

11+
public function getSlug(): string
12+
{
13+
return Str::slug($this->hasCustomSlug() ? $this->getCustomSlug() : $this->getTitle());
14+
}
15+
916
public function getCustomSlug(): string
1017
{
1118
return $this->customSlug;
@@ -22,4 +29,10 @@ public function setCustomSlug(string $customSlug): self
2229

2330
return $this;
2431
}
32+
33+
public function isColumnBySlug(string $slug): bool
34+
{
35+
return $this->getSlug() === $slug;
36+
}
37+
2538
}

src/Views/Columns/Traits/Helpers/ColumnHelpers.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers;
44

55
use Illuminate\Database\Eloquent\Model;
6-
use Illuminate\Support\{HtmlString,Str};
6+
use Illuminate\Support\HtmlString;
77
use Rappasoft\LaravelLivewireTables\Exceptions\DataTableConfigurationException;
88
use Rappasoft\LaravelLivewireTables\Views\Column;
99
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
@@ -25,11 +25,6 @@ public function getTitle(): string
2525
return $this->title;
2626
}
2727

28-
public function getSlug(): string
29-
{
30-
return Str::slug($this->hasCustomSlug() ? $this->getCustomSlug() : $this->getTitle());
31-
}
32-
3328
public function getField(): ?string
3429
{
3530
return $this->field;
@@ -50,11 +45,6 @@ public function isColumnBySelectName(string $name): bool
5045
return $this->getColumnSelectName() === $name;
5146
}
5247

53-
public function isColumnBySlug(string $slug): bool
54-
{
55-
return $this->getSlug() === $slug;
56-
}
57-
5848
public function hasField(): bool
5949
{
6050
return $this->getField() !== null;
@@ -132,31 +122,12 @@ public function getValue(Model $row): mixed
132122
return $row->{$this->getRelationString().'.'.$this->getField()};
133123
}
134124

135-
public function eagerLoadRelationsIsEnabled(): bool
136-
{
137-
return $this->eagerLoadRelations === true;
138-
}
139125

140126
public function isReorderColumn(): bool
141127
{
142128
return $this->isReorderColumn;
143129
}
144130

145-
public function hasFormatter(): bool
146-
{
147-
return $this->formatCallback !== null;
148-
}
149-
150-
public function getFormatCallback(): ?callable
151-
{
152-
return $this->formatCallback;
153-
}
154-
155-
// TODO
156-
public function getLabelCallback(): ?callable
157-
{
158-
return $this->labelCallback;
159-
}
160131

161132
public function isHtml(): bool
162133
{

src/Views/Columns/Traits/IsColumn.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
use Rappasoft\LaravelLivewireTables\Traits\Core\HasLocalisations;
66
use Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Configuration\ColumnConfiguration;
7-
use Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers\{ColumnHelpers,RelationshipHelpers};
7+
use Rappasoft\LaravelLivewireTables\Views\Columns\Traits\Helpers\{ColumnHelpers};
88
use Rappasoft\LaravelLivewireTables\Views\Traits\Core\{HasAttributes, HasLabelAttributes, HasTheme};
99

1010
trait IsColumn
1111
{
1212
use HasLocalisations,
1313
HasDataTableComponent,
14-
HasCustomSlug,
14+
HasRelations,
15+
HasLabelFormat,
16+
HasSlug,
1517
ColumnConfiguration,
1618
ColumnHelpers,
17-
RelationshipHelpers,
1819
IsCollapsible,
1920
IsSearchable,
2021
IsSelectable,
@@ -42,17 +43,8 @@ trait IsColumn
4243
// The table of the columns or relationship
4344
protected ?string $table = null;
4445

45-
// An array of relationships: i.e. address.group.name => ['address', 'group']
46-
protected array $relations = [];
47-
48-
protected bool $eagerLoadRelations = false;
49-
50-
protected mixed $formatCallback = null;
51-
5246
protected bool $html = false;
5347

54-
protected mixed $labelCallback = null;
55-
5648
protected bool $clickable = true;
5749

5850
protected bool $hasTableRowUrl = false;

0 commit comments

Comments
 (0)