Skip to content

Commit b4ba2ec

Browse files
PavelJurasekfoxycode
authored andcommitted
MenuItem: allow data to be structured
1 parent e2642ed commit b4ba2ec

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

src/DI/MenuExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function getItemSchema(): Schema
4343
'action' => Expect::type('string|array'),
4444
'link' => Expect::string(),
4545
'include' => Expect::type('string|array'),
46-
'data' => Expect::arrayOf('string', 'string'),
46+
'data' => Expect::arrayOf('mixed', 'string'),
4747
'items' => Expect::array(),
4848
'visibility' => Expect::from(new MenuVisibility()),
4949
]);

src/IMenuItem.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ public function getRealLink(): string;
3232
public function getRealAbsoluteLink(): string;
3333

3434
/**
35-
* @return array<string, string>
35+
* @return array<string, mixed>
3636
*/
3737
public function getData(): array;
3838

3939
/**
40-
* @param array<string, string> $data
40+
* @param array<string, mixed> $data
4141
*/
4242
public function setData(array $data): void;
4343

4444
public function hasDataItem(string $name): bool;
4545

46-
public function getDataItem(string $name, ?string $default = null): ?string;
46+
public function getDataItem(string $name, mixed $default = null): mixed;
4747

48-
public function addDataItem(string $name, string $value): void;
48+
public function addDataItem(string $name, mixed $value): void;
4949

5050
/**
5151
* @param string[] $include

src/Traits/MenuItemData.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function hasDataItem(string $name): bool
2929
return array_key_exists($name, $this->data);
3030
}
3131

32-
public function getDataItem(string $name, ?string $default = null): ?string
32+
public function getDataItem(string $name, mixed $default = null): mixed
3333
{
3434
if (!array_key_exists($name, $this->data)) {
3535
return $default;
@@ -38,7 +38,7 @@ public function getDataItem(string $name, ?string $default = null): ?string
3838
return $this->data[$name];
3939
}
4040

41-
public function addDataItem(string $name, string $value): void
41+
public function addDataItem(string $name, mixed $value): void
4242
{
4343
$this->data[$name] = $value;
4444
}

tests/cases/DI/MenuExtensionTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ public function testDI(): void
3232
Assert::type(MenuComponent::class, $dic->getService('menu.component.factory')->create('default'));
3333
}
3434

35+
public function testDataItems(): void
36+
{
37+
$dic = $this->createContainer();
38+
39+
$container = $dic->getService('menu.container');
40+
/** @var \Contributte\MenuControl\IMenu $menu */
41+
$menu = $container->getMenu('default');
42+
43+
$item = $menu->getItem('Homepage');
44+
45+
Assert::type('bool', $item->getDataItem('bool'));
46+
Assert::type('string', $item->getDataItem('icon'));
47+
Assert::type('array', $item->getDataItem('structured'));
48+
}
49+
3550
public function testRender(): void
3651
{
3752
$dic = $this->createContainer();

tests/cases/DI/config.neon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,8 @@ menu:
2222
items:
2323
Category:
2424
action: Category:default
25+
data:
26+
icon: fa fa-home
27+
bool: true
28+
structured:
29+
key: value

0 commit comments

Comments
 (0)