Skip to content

Commit 847a53d

Browse files
committed
refactor: LayoutPosition enum
1 parent 2a26523 commit 847a53d

File tree

3 files changed

+45
-27
lines changed

3 files changed

+45
-27
lines changed

src/Html/Enums/LayoutPosition.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yajra\DataTables\Html\Enums;
6+
7+
enum LayoutPosition: string
8+
{
9+
case Start = 'Start';
10+
case End = 'End';
11+
}

src/Html/Layout.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Illuminate\Support\Fluent;
88
use Illuminate\Support\Traits\Macroable;
9+
use Yajra\DataTables\Html\Enums\LayoutPosition;
910

1011
class Layout extends Fluent
1112
{
@@ -18,31 +19,31 @@ public static function make(array $options = []): static
1819

1920
public function topStart(string|array|null $options, int $order = 0): static
2021
{
21-
return $this->top($options, $order, 'Start');
22+
return $this->top($options, $order, LayoutPosition::Start);
2223
}
2324

24-
public function top(array|string|null $options, ?int $order = null, ?string $position = null): static
25+
public function top(array|string|null $options, ?int $order = null, ?LayoutPosition $position = null): static
2526
{
2627
if ($order > 0) {
27-
$this->attributes["top{$order}{$position}"] = $options;
28+
$this->attributes["top{$order}{$position?->value}"] = $options;
2829
} else {
29-
$this->attributes["top{$position}"] = $options;
30+
$this->attributes["top{$position?->value}"] = $options;
3031
}
3132

3233
return $this;
3334
}
3435

3536
public function topEnd(string|array|null $options, int $order = 0): static
3637
{
37-
return $this->top($options, $order, 'End');
38+
return $this->top($options, $order, LayoutPosition::End);
3839
}
3940

4041
public function topEndView(string $selector, int $order = 0): static
4142
{
42-
return $this->topView($selector, $order, 'End');
43+
return $this->topView($selector, $order, LayoutPosition::End);
4344
}
4445

45-
public function topView(string $selector, int $order = 0, ?string $position = null): static
46+
public function topView(string $selector, int $order = 0, ?LayoutPosition $position = null): static
4647
{
4748
$script = "function() { return $('{$selector}').html(); }";
4849

@@ -51,44 +52,44 @@ public function topView(string $selector, int $order = 0, ?string $position = nu
5152

5253
public function bottomStartView(string $selector, int $order = 0): static
5354
{
54-
return $this->bottomView($selector, $order, 'Start');
55+
return $this->bottomView($selector, $order, LayoutPosition::Start);
5556
}
5657

57-
public function bottomView(string $selector, int $order = 0, ?string $position = null): static
58+
public function bottomView(string $selector, int $order = 0, ?LayoutPosition $position = null): static
5859
{
5960
$script = "function() { return $('{$selector}').html(); }";
6061

6162
return $this->bottom($script, $order, $position);
6263
}
6364

64-
public function bottom(array|string|null $options, ?int $order = null, ?string $position = null): static
65+
public function bottom(array|string|null $options, ?int $order = null, ?LayoutPosition $position = null): static
6566
{
6667
if ($order > 0) {
67-
$this->attributes["bottom{$order}{$position}"] = $options;
68+
$this->attributes["bottom{$order}{$position?->value}"] = $options;
6869
} else {
69-
$this->attributes["bottom{$position}"] = $options;
70+
$this->attributes["bottom{$position?->value}"] = $options;
7071
}
7172

7273
return $this;
7374
}
7475

7576
public function bottomEndView(string $selector, int $order = 0): static
7677
{
77-
return $this->bottomView($selector, $order, 'End');
78+
return $this->bottomView($selector, $order, LayoutPosition::End);
7879
}
7980

8081
public function topStartView(string $selector, int $order = 0): static
8182
{
82-
return $this->topView($selector, $order, 'Start');
83+
return $this->topView($selector, $order, LayoutPosition::Start);
8384
}
8485

8586
public function bottomStart(string|array|null $options, int $order = 0): static
8687
{
87-
return $this->bottom($options, $order, 'Start');
88+
return $this->bottom($options, $order, LayoutPosition::Start);
8889
}
8990

9091
public function bottomEnd(string|array|null $options, int $order = 0): static
9192
{
92-
return $this->bottom($options, $order, 'End');
93+
return $this->bottom($options, $order, LayoutPosition::End);
9394
}
9495
}

tests/LayoutTest.php

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPUnit\Framework\Attributes\Test;
66
use Yajra\DataTables\Html\Builder;
7+
use Yajra\DataTables\Html\Enums\LayoutPosition;
78
use Yajra\DataTables\Html\Layout;
89

910
class LayoutTest extends TestCase
@@ -36,16 +37,16 @@ public function it_can_set_positions(): void
3637
$layout->bottom('test', 1);
3738
$this->assertEquals('test', $layout->get('bottom1'));
3839

39-
$layout->top('test', 1, 'Start');
40+
$layout->top('test', 1, LayoutPosition::Start);
4041
$this->assertEquals('test', $layout->get('top1Start'));
4142

42-
$layout->bottom('test', 1, 'Start');
43+
$layout->bottom('test', 1, LayoutPosition::Start);
4344
$this->assertEquals('test', $layout->get('bottom1Start'));
4445

45-
$layout->top('test', 1, 'End');
46+
$layout->top('test', 1, LayoutPosition::End);
4647
$this->assertEquals('test', $layout->get('top1End'));
4748

48-
$layout->bottom('test', 1, 'End');
49+
$layout->bottom('test', 1, LayoutPosition::End);
4950
$this->assertEquals('test', $layout->get('bottom1End'));
5051
}
5152

@@ -62,10 +63,10 @@ public function it_can_be_used_in_builder(): void
6263
$layout->bottomEnd('test');
6364
$layout->top('test', 1);
6465
$layout->bottom('test', 1);
65-
$layout->top('test', 1, 'Start');
66-
$layout->bottom('test', 1, 'Start');
67-
$layout->top('test', 1, 'End');
68-
$layout->bottom('test', 1, 'End');
66+
$layout->top('test', 1, LayoutPosition::Start);
67+
$layout->bottom('test', 1, LayoutPosition::Start);
68+
$layout->top('test', 1, LayoutPosition::End);
69+
$layout->bottom('test', 1, LayoutPosition::End);
6970
});
7071

7172
$this->assertArrayHasKey('layout', $builder->getAttributes());
@@ -156,7 +157,8 @@ public function it_can_accept_js_selector_for_layout_content(): void
156157
$builder->layout(fn (Layout $layout) => $layout->topStartView('#test'));
157158
$this->assertArrayHasKey('layout', $builder->getAttributes());
158159
$this->assertArrayHasKey('topStart', $builder->getAttributes()['layout']);
159-
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['topStart']);
160+
$this->assertEquals("function() { return $('#test').html(); }",
161+
$builder->getAttributes()['layout']['topStart']);
160162

161163
$builder->layout(fn (Layout $layout) => $layout->topEndView('#test'));
162164
$this->assertArrayHasKey('layout', $builder->getAttributes());
@@ -166,11 +168,15 @@ public function it_can_accept_js_selector_for_layout_content(): void
166168
$builder->layout(fn (Layout $layout) => $layout->bottomStartView('#test'));
167169
$this->assertArrayHasKey('layout', $builder->getAttributes());
168170
$this->assertArrayHasKey('bottomStart', $builder->getAttributes()['layout']);
169-
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['bottomStart']);
171+
$this->assertEquals("function() { return $('#test').html(); }",
172+
$builder->getAttributes()['layout']['bottomStart']);
170173

171174
$builder->layout(fn (Layout $layout) => $layout->bottomEndView('#test'));
172175
$this->assertArrayHasKey('layout', $builder->getAttributes());
173176
$this->assertArrayHasKey('bottomEnd', $builder->getAttributes()['layout']);
174-
$this->assertEquals("function() { return $('#test').html(); }", $builder->getAttributes()['layout']['bottomEnd']);
177+
$this->assertEquals(
178+
"function() { return $('#test').html(); }",
179+
$builder->getAttributes()['layout']['bottomEnd']
180+
);
175181
}
176182
}

0 commit comments

Comments
 (0)