Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new sortOrder option to sort menu items when they are added t… #381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions doc/06-Sorting-Menu-Items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Control the sort order of your menu items
=========================================

There are 2 ways to control the order of your menu items:

1. Reorder items by giving a sorted list of item names
2. Sort the items when adding them to the menu

Reorder items by giving a sorted list of item names
-----------------------

```php
$factory = new MenuFactory();
$menu = new MenuItem('root', $factory);
$menu->addChild('c1');
$menu->addChild('c2');
$menu->addChild('c3');
$menu->addChild('c4');

$menu->reorderChildren(['c4', 'c3', 'c2', 'c1']);
$menu->getChildren() //'c4', 'c3', 'c2', 'c1'
```

Sort the items when adding them to the menu
-------------------------------------------

The items can be added in a sorted manner by using the `sortOrder` options.

Caution: The order will be lost when using `ItemInterface->reorderChildren()`!

```php
$factory = new MenuFactory();
$menu = new MenuItem('root', $factory);
$menu->addChild('c1', ['sortOrder' => 2]);
$menu->addChild('c2', ['sortOrder' => 4]);
$menu->addChild('c3', ['sortOrder' => 1]);
$menu->addChild('c4', ['sortOrder' => 3]);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line missing here?
$menu->reorderChildren();

$menu->getChildren() //'c1', 'c2', 'c3', 'c4'
```

Items without the `sortOrder` option will be just appended after the items with `sortOrder` in the order they're added.
File renamed without changes.
2 changes: 2 additions & 0 deletions src/Knp/Menu/Factory/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function buildOptions(array $options): array
'current' => null,
'display' => true,
'displayChildren' => true,
'sortOrder' => null,
],
$options
);
Expand All @@ -40,6 +41,7 @@ public function buildItem(ItemInterface $item, array $options): void
->setCurrent($options['current'])
->setDisplay($options['display'])
->setDisplayChildren($options['displayChildren'])
->setSortOrder($options['sortOrder'])
;

$this->buildExtras($item, $options);
Expand Down
11 changes: 5 additions & 6 deletions src/Knp/Menu/ItemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public function getUri(): ?string;
*/
public function setUri(?string $uri): self;

public function getSortOrder(): ?int;

public function setSortOrder(int $sortOrder): self;

/**
* Returns the label that will be used to render this menu item
*
Expand Down Expand Up @@ -162,14 +166,9 @@ public function setExtras(array $extras): self;
/**
* @param string $name The name of the extra to return
* @param mixed $default The value to return if the extra doesn't exist
*
* @return mixed
*/
public function getExtra(string $name, $default = null);

/**
* @param mixed $value
*/
public function setExtra(string $name, $value): self;

public function getDisplayChildren(): bool;
Expand Down Expand Up @@ -250,7 +249,7 @@ public function getParent(): ?self;
*
* Provides a fluent interface
*/
public function setParent(?self $parent = null): self;
public function setParent(self $parent = null): self;

/**
* Return the children as an array of ItemInterface objects
Expand Down
60 changes: 58 additions & 2 deletions src/Knp/Menu/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ class MenuItem implements ItemInterface
*/
protected $factory;

protected ?int $sortOrder = null;

/**
* Class constructor
*
Expand Down Expand Up @@ -326,6 +328,18 @@ public function setDisplay(bool $bool): ItemInterface
return $this;
}

public function getSortOrder(): ?int
{
return $this->sortOrder;
}

public function setSortOrder(int $sortOrder = null): ItemInterface
{
$this->sortOrder = $sortOrder;

return $this;
}

public function addChild($child, array $options = []): ItemInterface
{
if (!$child instanceof ItemInterface) {
Expand All @@ -336,7 +350,49 @@ public function addChild($child, array $options = []): ItemInterface

$child->setParent($this);

$this->children[$child->getName()] = $child;
$sortOrder = $child->getSortOrder();

$length = $this->count();
if (null === $sortOrder || 0 === $length) {
$this->children[$child->getName()] = $child;

return $child;
}

return $this->addChildAtPosition($child);
}

private function addChildAtPosition(ItemInterface $child): ItemInterface
{
$firstChildSortOrder = $this->children[\array_key_first($this->children)]->getSortOrder();
if (null !== $firstChildSortOrder && $child->getSortOrder() < $firstChildSortOrder) {
$this->children = \array_merge([$child->getName() => $child], $this->children);

return $child;
}

$lastChildSortOrder = $this->children[\array_key_last($this->children)]->getSortOrder();
if (null !== $lastChildSortOrder && $child->getSortOrder() > $lastChildSortOrder) {
$this->children[$child->getName()] = $child;

return $child;
}

$i = -1;
foreach ($this->children as $key => $loopedChild) {
++$i;
if (null !== $loopedChild->getSortOrder() && $child->getSortOrder() >= $loopedChild->getSortOrder()) {
continue;
}

$firstHalf = \array_slice($this->children, 0, $i);
$secondHalf = \array_slice($this->children, $i);

$firstHalf[$child->getName()] = $child;

$this->children = \array_merge($firstHalf, $secondHalf);
break;
}

return $child;
}
Expand Down Expand Up @@ -405,7 +461,7 @@ public function getParent(): ?ItemInterface
return $this->parent;
}

public function setParent(?ItemInterface $parent = null): ItemInterface
public function setParent(ItemInterface $parent = null): ItemInterface
{
if ($parent === $this) {
throw new \InvalidArgumentException('Item cannot be a child of itself');
Expand Down
39 changes: 39 additions & 0 deletions tests/Knp/Menu/Tests/MenuItemSortOrderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Knp\Menu\Tests;

use Knp\Menu\MenuFactory;
use Knp\Menu\MenuItem;
use PHPUnit\Framework\TestCase;

final class MenuItemSortOrderTest extends TestCase
{
public function testItemsWithoutSortOrderShouldBeAppendedToKeepCurrentBehavior(): void
{
$factory = new MenuFactory();
$menu = new MenuItem('root', $factory);
$menu->addChild('c1', ['sortOrder' => 1]);
$menu->addChild('c2');
$menu->addChild('c3', ['sortOrder' => 2]);
$menu->addChild('c4');
$menu->addChild('c5');
$menu->addChild('c6', ['sortOrder' => 1]);
$menu->addChild('c7', ['sortOrder' => 1]);

$arr = \array_keys($menu->getChildren());
$this->assertEquals(['c1', 'c6', 'c7', 'c3', 'c2', 'c4', 'c5'], $arr);
}

public function testItemsAreAddedInTheCorrectOrder(): void
{
$factory = new MenuFactory();
$menu = new MenuItem('root', $factory);
$menu->addChild('c1', ['sortOrder' => 2]);
$menu->addChild('c2', ['sortOrder' => 4]);
$menu->addChild('c3', ['sortOrder' => 1]);
$menu->addChild('c4', ['sortOrder' => 3]);

$arr = \array_keys($menu->getChildren());
$this->assertEquals(['c3', 'c1', 'c4', 'c2'], $arr);
}
}