Skip to content

Commit

Permalink
MenuItem: allow data to be structured
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelJurasek authored and foxycode committed Aug 1, 2023
1 parent e2642ed commit b4ba2ec
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/DI/MenuExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
]);
Expand Down
8 changes: 4 additions & 4 deletions src/IMenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@ public function getRealLink(): string;
public function getRealAbsoluteLink(): string;

/**
* @return array<string, string>
* @return array<string, mixed>
*/
public function getData(): array;

/**
* @param array<string, string> $data
* @param array<string, mixed> $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
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/MenuItemData.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
15 changes: 15 additions & 0 deletions tests/cases/DI/MenuExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions tests/cases/DI/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ menu:
items:
Category:
action: Category:default
data:
icon: fa fa-home
bool: true
structured:
key: value

0 comments on commit b4ba2ec

Please sign in to comment.