diff --git a/composer.json b/composer.json index 0aeeff5..4ad2e92 100644 --- a/composer.json +++ b/composer.json @@ -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" ] diff --git a/src/Html/HasOptions.php b/src/Html/HasOptions.php index beb1c19..14fcd6d 100644 --- a/src/Html/HasOptions.php +++ b/src/Html/HasOptions.php @@ -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 @@ -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. * diff --git a/src/Html/Layout.php b/src/Html/Layout.php new file mode 100644 index 0000000..04ac908 --- /dev/null +++ b/src/Html/Layout.php @@ -0,0 +1,60 @@ +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; + } +} diff --git a/tests/LayoutTest.php b/tests/LayoutTest.php new file mode 100644 index 0000000..311ea0d --- /dev/null +++ b/tests/LayoutTest.php @@ -0,0 +1,130 @@ +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']); + } +}