Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
"rector": "./vendor/bin/rector",
"stan": "./vendor/bin/phpstan analyse --memory-limit=2G --ansi --no-progress --no-interaction --configuration=phpstan.neon.dist",
"pr": [
"@pint",
"@rector",
"@pint",
"@stan",
"@test"
]
Expand Down
25 changes: 25 additions & 0 deletions src/Html/HasOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public function displayStart(int $value = 0): static
*
* @return $this
*
* @deprecated Use layout() method instead.
* @see https://datatables.net/reference/option/dom
*/
public function dom(string $value): static
Expand All @@ -83,6 +84,30 @@ public function dom(string $value): static
return $this;
}

/**
* Set layout option value.
*
* @return $this
*
* @see https://datatables.net/reference/option/layout
*/
public function layout(array|Layout|callable $value): static
{
if ($value instanceof Layout) {
$value = $value->toArray();
}

if (is_callable($value)) {
$layout = new Layout;
$value($layout);
$value = $layout->toArray();
}

$this->attributes['layout'] = $value;

return $this;
}

/**
* Set lengthMenu option value.
*
Expand Down
60 changes: 60 additions & 0 deletions src/Html/Layout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace Yajra\DataTables\Html;

use Illuminate\Support\Fluent;
use Illuminate\Support\Traits\Macroable;

class Layout extends Fluent
{
use Macroable;

public static function make(array $options = []): static
{
return new static($options);
}

public function topStart(string|array|null $options, int $order = 0): static
{
return $this->top($options, $order, 'Start');
}

public function topEnd(string|array|null $options, int $order = 0): static
{
return $this->top($options, $order, 'End');
}

public function bottomStart(string|array|null $options, int $order = 0): static
{
return $this->bottom($options, $order, 'Start');
}

public function bottomEnd(string|array|null $options, int $order = 0): static
{
return $this->bottom($options, $order, 'End');
}

public function top(array|string|null $options, ?int $order = null, ?string $position = null): static
{
if ($order > 0) {
$this->attributes["top{$order}{$position}"] = $options;
} else {
$this->attributes["top{$position}"] = $options;
}

return $this;
}

public function bottom(array|string|null $options, ?int $order = null, ?string $position = null): static
{
if ($order > 0) {
$this->attributes["bottom{$order}{$position}"] = $options;
} else {
$this->attributes["bottom{$position}"] = $options;
}

return $this;
}
}
130 changes: 130 additions & 0 deletions tests/LayoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace Yajra\DataTables\Html\Tests;

use PHPUnit\Framework\Attributes\Test;
use Yajra\DataTables\Html\Builder;
use Yajra\DataTables\Html\Layout;

class LayoutTest extends TestCase
{
#[Test]
public function it_can_set_positions(): void
{
$layout = new Layout;
$layout->top('test');
$this->assertEquals('test', $layout->get('top'));

$layout->bottom('test');
$this->assertEquals('test', $layout->get('bottom'));

$layout->topStart('test');
$this->assertEquals('test', $layout->get('topStart'));

$layout->topEnd('test');
$this->assertEquals('test', $layout->get('topEnd'));

$layout->bottomStart('test');
$this->assertEquals('test', $layout->get('bottomStart'));

$layout->bottomEnd('test');
$this->assertEquals('test', $layout->get('bottomEnd'));

$layout->top('test', 1);
$this->assertEquals('test', $layout->get('top1'));

$layout->bottom('test', 1);
$this->assertEquals('test', $layout->get('bottom1'));

$layout->top('test', 1, 'Start');
$this->assertEquals('test', $layout->get('top1Start'));

$layout->bottom('test', 1, 'Start');
$this->assertEquals('test', $layout->get('bottom1Start'));

$layout->top('test', 1, 'End');
$this->assertEquals('test', $layout->get('top1End'));

$layout->bottom('test', 1, 'End');
$this->assertEquals('test', $layout->get('bottom1End'));
}

#[Test]
public function it_can_be_used_in_builder(): void
{
$builder = resolve(Builder::class);
$builder->layout(function (Layout $layout) {
$layout->top('test');
$layout->bottom('test');
$layout->topStart('test');
$layout->topEnd('test');
$layout->bottomStart('test');
$layout->bottomEnd('test');
$layout->top('test', 1);
$layout->bottom('test', 1);
$layout->top('test', 1, 'Start');
$layout->bottom('test', 1, 'Start');
$layout->top('test', 1, 'End');
$layout->bottom('test', 1, 'End');
});

$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('bottom', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('topStart', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('topEnd', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('bottomStart', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('bottomEnd', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('top1', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('bottom1', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('top1Start', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('bottom1Start', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('top1End', $builder->getAttributes()['layout']);
$this->assertArrayHasKey('bottom1End', $builder->getAttributes()['layout']);
}

#[Test]
public function it_has_factory_method(): void
{
$layout = Layout::make();
$this->assertInstanceOf(Layout::class, $layout);

$builder = resolve(Builder::class);
$builder->layout(Layout::make());

$this->assertArrayHasKey('layout', $builder->getAttributes());
}

#[Test]
public function it_can_be_macroable(): void
{
Layout::macro('test', fn () => 'test');

$layout = new Layout;
$this->assertEquals('test', $layout->test());
}

#[Test]
public function it_can_accept_array(): void
{
$layout = new Layout(['top' => 'test']);
$this->assertEquals('test', $layout->get('top'));
}

#[Test]
public function it_can_accept_array_as_parameter(): void
{
$layout = Layout::make(['top' => 'test']);
$this->assertEquals('test', $layout->get('top'));
}

#[Test]
public function it_can_accept_array_as_parameter_in_builder(): void
{
$builder = resolve(Builder::class);
$builder->layout(['top' => 'test']);

$this->assertArrayHasKey('layout', $builder->getAttributes());
$this->assertArrayHasKey('top', $builder->getAttributes()['layout']);
}
}