From f632fe5f65d527662426259bc6e35d500c3fbdba Mon Sep 17 00:00:00 2001 From: Jacob Dreesen Date: Fri, 27 Sep 2024 12:31:08 +0200 Subject: [PATCH] Allow adding content directly (without tabs) Note: You can only add either content or tabs --- src/DialogBoxBuilder.php | 31 ++++++++++++-- .../LayoutItem/PanelItem.php | 5 +++ tests/Unit/DialogBoxBuilderTest.php | 42 +++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/DialogBoxBuilder.php b/src/DialogBoxBuilder.php index 7daae9d..1f25824 100644 --- a/src/DialogBoxBuilder.php +++ b/src/DialogBoxBuilder.php @@ -18,11 +18,11 @@ class DialogBoxBuilder { private EditableDialogBoxConfiguration $config; private TabPanelItem $tabs; + private PanelItem $content; public function __construct() { $this->config = new EditableDialogBoxConfiguration(); - $this->tabs = new TabPanelItem(); } /** @@ -55,11 +55,34 @@ public function height(int $height): static return $this; } + /** + * @return $this + */ + public function addContent(EditableItem ...$items): static + { + if (isset($this->tabs)) { + throw new \LogicException('You cannot add content and tabs at the same time.'); + } + + $this->content ??= new PanelItem('', []); + + foreach ($items as $item) { + $this->content->addItem($item); + } + + return $this; + } + /** * @return $this */ public function addTab(string $title, EditableItem ...$items): static { + if (isset($this->content)) { + throw new \LogicException('You cannot add tabs and content at the same time.'); + } + + $this->tabs ??= new TabPanelItem(); $this->tabs->addTab(new PanelItem($title, array_values($items))); return $this; @@ -105,8 +128,10 @@ public function createLink(string $name): LinkItem public function build(): EditableDialogBoxConfiguration { - if (!$this->tabs->isEmpty()) { - $this->config->setItems($this->tabs->toArray()); + $items = $this->content ?? $this->tabs ?? null; + + if ($items && !$items->isEmpty()) { + $this->config->setItems($items->toArray()); } return $this->config; diff --git a/src/EditableDialogBox/LayoutItem/PanelItem.php b/src/EditableDialogBox/LayoutItem/PanelItem.php index 1b8ebe6..18aee94 100644 --- a/src/EditableDialogBox/LayoutItem/PanelItem.php +++ b/src/EditableDialogBox/LayoutItem/PanelItem.php @@ -19,6 +19,11 @@ public function __construct(string $title, array $items) $this->title = $title; } + public function addItem(DialogBoxItem $item): static + { + return parent::addItem($item); + } + protected function getAttributes(): array { return ['title' => $this->title] + parent::getAttributes(); diff --git a/tests/Unit/DialogBoxBuilderTest.php b/tests/Unit/DialogBoxBuilderTest.php index f4a8093..6754f0d 100644 --- a/tests/Unit/DialogBoxBuilderTest.php +++ b/tests/Unit/DialogBoxBuilderTest.php @@ -13,6 +13,26 @@ class DialogBoxBuilderTest extends TestCase { use ProphecyTrait; + /** + * @test + */ + public function addingContent(): void + { + $dialogBuilder = new DialogBoxBuilder(); + $editableItem1 = new EditableItem('type1', 'name1'); + $editableItem2 = new EditableItem('type2', 'name2'); + $editableItem3 = new EditableItem('type3', 'name3'); + + $expected = new PanelItem('', [$editableItem1, $editableItem2, $editableItem3]); + + $dialogBox = $dialogBuilder + ->addContent($editableItem1, $editableItem2) + ->addContent($editableItem3) + ->build(); + + self::assertSame($expected->toArray(), $dialogBox->getItems()); + } + /** * @test */ @@ -34,4 +54,26 @@ public function addingTwoTabsWithSomeItems(): void self::assertSame($expected->toArray(), $dialogBox->getItems()); } + + /** + * @test + */ + public function addingContentAndThenTabs(): void + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('You cannot add tabs and content at the same time.'); + + (new DialogBoxBuilder())->addContent()->addTab('Test'); + } + + /** + * @test + */ + public function addingTabsAndThenContent(): void + { + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('You cannot add content and tabs at the same time.'); + + (new DialogBoxBuilder())->addTab('Test')->addContent(); + } }