Skip to content

Commit ae1e20b

Browse files
authored
Merge pull request #216 from yajra/layout
feat: layout builder
2 parents 01c89e0 + 966e4e6 commit ae1e20b

File tree

4 files changed

+216
-1
lines changed

4 files changed

+216
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
"rector": "./vendor/bin/rector",
5454
"stan": "./vendor/bin/phpstan analyse --memory-limit=2G --ansi --no-progress --no-interaction --configuration=phpstan.neon.dist",
5555
"pr": [
56-
"@pint",
5756
"@rector",
57+
"@pint",
5858
"@stan",
5959
"@test"
6060
]

src/Html/HasOptions.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public function displayStart(int $value = 0): static
7474
*
7575
* @return $this
7676
*
77+
* @deprecated Use layout() method instead.
7778
* @see https://datatables.net/reference/option/dom
7879
*/
7980
public function dom(string $value): static
@@ -83,6 +84,30 @@ public function dom(string $value): static
8384
return $this;
8485
}
8586

87+
/**
88+
* Set layout option value.
89+
*
90+
* @return $this
91+
*
92+
* @see https://datatables.net/reference/option/layout
93+
*/
94+
public function layout(array|Layout|callable $value): static
95+
{
96+
if ($value instanceof Layout) {
97+
$value = $value->toArray();
98+
}
99+
100+
if (is_callable($value)) {
101+
$layout = new Layout;
102+
$value($layout);
103+
$value = $layout->toArray();
104+
}
105+
106+
$this->attributes['layout'] = $value;
107+
108+
return $this;
109+
}
110+
86111
/**
87112
* Set lengthMenu option value.
88113
*

src/Html/Layout.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yajra\DataTables\Html;
6+
7+
use Illuminate\Support\Fluent;
8+
use Illuminate\Support\Traits\Macroable;
9+
10+
class Layout extends Fluent
11+
{
12+
use Macroable;
13+
14+
public static function make(array $options = []): static
15+
{
16+
return new static($options);
17+
}
18+
19+
public function topStart(string|array|null $options, int $order = 0): static
20+
{
21+
return $this->top($options, $order, 'Start');
22+
}
23+
24+
public function topEnd(string|array|null $options, int $order = 0): static
25+
{
26+
return $this->top($options, $order, 'End');
27+
}
28+
29+
public function bottomStart(string|array|null $options, int $order = 0): static
30+
{
31+
return $this->bottom($options, $order, 'Start');
32+
}
33+
34+
public function bottomEnd(string|array|null $options, int $order = 0): static
35+
{
36+
return $this->bottom($options, $order, 'End');
37+
}
38+
39+
public function top(array|string|null $options, ?int $order = null, ?string $position = null): static
40+
{
41+
if ($order > 0) {
42+
$this->attributes["top{$order}{$position}"] = $options;
43+
} else {
44+
$this->attributes["top{$position}"] = $options;
45+
}
46+
47+
return $this;
48+
}
49+
50+
public function bottom(array|string|null $options, ?int $order = null, ?string $position = null): static
51+
{
52+
if ($order > 0) {
53+
$this->attributes["bottom{$order}{$position}"] = $options;
54+
} else {
55+
$this->attributes["bottom{$position}"] = $options;
56+
}
57+
58+
return $this;
59+
}
60+
}

tests/LayoutTest.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
namespace Yajra\DataTables\Html\Tests;
4+
5+
use PHPUnit\Framework\Attributes\Test;
6+
use Yajra\DataTables\Html\Builder;
7+
use Yajra\DataTables\Html\Layout;
8+
9+
class LayoutTest extends TestCase
10+
{
11+
#[Test]
12+
public function it_can_set_positions(): void
13+
{
14+
$layout = new Layout;
15+
$layout->top('test');
16+
$this->assertEquals('test', $layout->get('top'));
17+
18+
$layout->bottom('test');
19+
$this->assertEquals('test', $layout->get('bottom'));
20+
21+
$layout->topStart('test');
22+
$this->assertEquals('test', $layout->get('topStart'));
23+
24+
$layout->topEnd('test');
25+
$this->assertEquals('test', $layout->get('topEnd'));
26+
27+
$layout->bottomStart('test');
28+
$this->assertEquals('test', $layout->get('bottomStart'));
29+
30+
$layout->bottomEnd('test');
31+
$this->assertEquals('test', $layout->get('bottomEnd'));
32+
33+
$layout->top('test', 1);
34+
$this->assertEquals('test', $layout->get('top1'));
35+
36+
$layout->bottom('test', 1);
37+
$this->assertEquals('test', $layout->get('bottom1'));
38+
39+
$layout->top('test', 1, 'Start');
40+
$this->assertEquals('test', $layout->get('top1Start'));
41+
42+
$layout->bottom('test', 1, 'Start');
43+
$this->assertEquals('test', $layout->get('bottom1Start'));
44+
45+
$layout->top('test', 1, 'End');
46+
$this->assertEquals('test', $layout->get('top1End'));
47+
48+
$layout->bottom('test', 1, 'End');
49+
$this->assertEquals('test', $layout->get('bottom1End'));
50+
}
51+
52+
#[Test]
53+
public function it_can_be_used_in_builder(): void
54+
{
55+
$builder = resolve(Builder::class);
56+
$builder->layout(function (Layout $layout) {
57+
$layout->top('test');
58+
$layout->bottom('test');
59+
$layout->topStart('test');
60+
$layout->topEnd('test');
61+
$layout->bottomStart('test');
62+
$layout->bottomEnd('test');
63+
$layout->top('test', 1);
64+
$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');
69+
});
70+
71+
$this->assertArrayHasKey('layout', $builder->getAttributes());
72+
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
73+
$this->assertArrayHasKey('bottom', $builder->getAttributes()['layout']);
74+
$this->assertArrayHasKey('topStart', $builder->getAttributes()['layout']);
75+
$this->assertArrayHasKey('topEnd', $builder->getAttributes()['layout']);
76+
$this->assertArrayHasKey('bottomStart', $builder->getAttributes()['layout']);
77+
$this->assertArrayHasKey('bottomEnd', $builder->getAttributes()['layout']);
78+
$this->assertArrayHasKey('top1', $builder->getAttributes()['layout']);
79+
$this->assertArrayHasKey('bottom1', $builder->getAttributes()['layout']);
80+
$this->assertArrayHasKey('top1Start', $builder->getAttributes()['layout']);
81+
$this->assertArrayHasKey('bottom1Start', $builder->getAttributes()['layout']);
82+
$this->assertArrayHasKey('top1End', $builder->getAttributes()['layout']);
83+
$this->assertArrayHasKey('bottom1End', $builder->getAttributes()['layout']);
84+
}
85+
86+
#[Test]
87+
public function it_has_factory_method(): void
88+
{
89+
$layout = Layout::make();
90+
$this->assertInstanceOf(Layout::class, $layout);
91+
92+
$builder = resolve(Builder::class);
93+
$builder->layout(Layout::make());
94+
95+
$this->assertArrayHasKey('layout', $builder->getAttributes());
96+
}
97+
98+
#[Test]
99+
public function it_can_be_macroable(): void
100+
{
101+
Layout::macro('test', fn () => 'test');
102+
103+
$layout = new Layout;
104+
$this->assertEquals('test', $layout->test());
105+
}
106+
107+
#[Test]
108+
public function it_can_accept_array(): void
109+
{
110+
$layout = new Layout(['top' => 'test']);
111+
$this->assertEquals('test', $layout->get('top'));
112+
}
113+
114+
#[Test]
115+
public function it_can_accept_array_as_parameter(): void
116+
{
117+
$layout = Layout::make(['top' => 'test']);
118+
$this->assertEquals('test', $layout->get('top'));
119+
}
120+
121+
#[Test]
122+
public function it_can_accept_array_as_parameter_in_builder(): void
123+
{
124+
$builder = resolve(Builder::class);
125+
$builder->layout(['top' => 'test']);
126+
127+
$this->assertArrayHasKey('layout', $builder->getAttributes());
128+
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
129+
}
130+
}

0 commit comments

Comments
 (0)