diff --git a/src/DI/MenuExtension.php b/src/DI/MenuExtension.php index c0c606d..982b8bb 100644 --- a/src/DI/MenuExtension.php +++ b/src/DI/MenuExtension.php @@ -43,7 +43,7 @@ public function getItemSchema(): Schema 'action' => Expect::type('string|array'), 'link' => Expect::string(), 'include' => Expect::type('string|array'), - 'data' => Expect::arrayOf('string', 'string'), + 'data' => Expect::arrayOf('mixed', 'string'), 'items' => Expect::array(), 'visibility' => Expect::from(new MenuVisibility()), ]); diff --git a/src/IMenuItem.php b/src/IMenuItem.php index 7484f57..97c1d85 100644 --- a/src/IMenuItem.php +++ b/src/IMenuItem.php @@ -32,20 +32,20 @@ public function getRealLink(): string; public function getRealAbsoluteLink(): string; /** - * @return array + * @return array */ public function getData(): array; /** - * @param array $data + * @param array $data */ public function setData(array $data): void; public function hasDataItem(string $name): bool; - public function getDataItem(string $name, ?string $default = null): ?string; + public function getDataItem(string $name, mixed $default = null): mixed; - public function addDataItem(string $name, string $value): void; + public function addDataItem(string $name, mixed $value): void; /** * @param string[] $include diff --git a/src/Traits/MenuItemData.php b/src/Traits/MenuItemData.php index 5333156..ec3d57b 100644 --- a/src/Traits/MenuItemData.php +++ b/src/Traits/MenuItemData.php @@ -29,7 +29,7 @@ public function hasDataItem(string $name): bool return array_key_exists($name, $this->data); } - public function getDataItem(string $name, ?string $default = null): ?string + public function getDataItem(string $name, mixed $default = null): mixed { if (!array_key_exists($name, $this->data)) { return $default; @@ -38,7 +38,7 @@ public function getDataItem(string $name, ?string $default = null): ?string return $this->data[$name]; } - public function addDataItem(string $name, string $value): void + public function addDataItem(string $name, mixed $value): void { $this->data[$name] = $value; } diff --git a/tests/cases/DI/MenuExtensionTest.php b/tests/cases/DI/MenuExtensionTest.php index b104df1..0fd8167 100644 --- a/tests/cases/DI/MenuExtensionTest.php +++ b/tests/cases/DI/MenuExtensionTest.php @@ -32,6 +32,21 @@ public function testDI(): void Assert::type(MenuComponent::class, $dic->getService('menu.component.factory')->create('default')); } + public function testDataItems(): void + { + $dic = $this->createContainer(); + + $container = $dic->getService('menu.container'); + /** @var \Contributte\MenuControl\IMenu $menu */ + $menu = $container->getMenu('default'); + + $item = $menu->getItem('Homepage'); + + Assert::type('bool', $item->getDataItem('bool')); + Assert::type('string', $item->getDataItem('icon')); + Assert::type('array', $item->getDataItem('structured')); + } + public function testRender(): void { $dic = $this->createContainer(); diff --git a/tests/cases/DI/config.neon b/tests/cases/DI/config.neon index 97cc065..d9cef0d 100644 --- a/tests/cases/DI/config.neon +++ b/tests/cases/DI/config.neon @@ -22,3 +22,8 @@ menu: items: Category: action: Category:default + data: + icon: fa fa-home + bool: true + structured: + key: value