Skip to content

Commit

Permalink
remove nested (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlutter authored and ivanbarlog committed May 9, 2018
1 parent 9091774 commit 0b39745
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 409 deletions.
41 changes: 30 additions & 11 deletions src/Builder/NavigationBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@
*/
class NavigationBuilder
{
/** @var MatcherInterface */
private $matcher;
/** @var ContainerInterface */
private $container;
/** @var RootNode */
private $root;
/** @var ItemInterface[] */
private $stack = [];
/** @var ItemInterface[] */
/** @var ParentNode[] */
private $used = [];
/** @var ItemInterface */
/** @var ParentNode */
private $current;

public function __construct(ContainerInterface $container, MatcherInterface $matcher)
{
$this->matcher = $matcher;
$this->container = new OrderedContainer($container);
$this->build($matcher);
$this->build();
}

/**
Expand Down Expand Up @@ -66,7 +69,15 @@ public function isAncestor(ItemInterface $item): bool
$ancestors = $this->getBreadcrumbs();
array_pop($ancestors);

return in_array($item, $ancestors);
return in_array(
$item,
array_map(
function (ParentNode $node) {
return $node->getItem();
},
$ancestors
)
);
}

/**
Expand All @@ -80,6 +91,15 @@ public function getCurrent(): ItemInterface
throw new NoCurrentItemFoundException();
}

return $this->current->getItem();
}

public function getCurrentNode(): ParentNode
{
if (!$this->current) {
throw new NoCurrentItemFoundException();
}

return $this->current;
}

Expand All @@ -88,12 +108,10 @@ public function getRoot(): RootNode
return $this->root;
}

private function build(MatcherInterface $matcher)
private function build()
{
$this->root = new RootNode();
foreach ($this->container->getItems() as $item) {
$this->setCurrentItem($matcher, $item);

if ($item instanceof NestableInterface) {
$this->getRootItem($item);
$this->addItemsFromStack();
Expand Down Expand Up @@ -136,15 +154,16 @@ private function addItem(RootNode $root, ItemInterface $item): void
return;
}

$parentNode = new ParentNode($item);
$parentNode = new ParentNode($item, $root);
$this->used[$name] = $parentNode;
$root->addChild($parentNode);
$this->setCurrentItem($parentNode);
}

private function setCurrentItem(MatcherInterface $matcher, ItemInterface $item): void
private function setCurrentItem(ParentNode $node): void
{
if (!$this->current && $matcher->isCurrent($item)) {
$this->current = $item;
if (!$this->current && $this->matcher->isCurrent($node->getItem())) {
$this->current = $node;
}
}
}
10 changes: 9 additions & 1 deletion src/Builder/ParentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,22 @@ class ParentNode extends RootNode
{
/** @var ItemInterface */
private $item;
/** @var RootNode */
private $parent;

public function __construct(ItemInterface $item)
public function __construct(ItemInterface $item, RootNode $parent)
{
$this->item = $item;
$this->parent = $parent;
}

public function getItem(): ItemInterface
{
return $this->item;
}

public function getParent(): RootNode
{
return $this->parent;
}
}
35 changes: 0 additions & 35 deletions src/Nested/AdvancedNavigationInterface.php

This file was deleted.

154 changes: 0 additions & 154 deletions src/Nested/Builder/NavigationBuilder.php

This file was deleted.

77 changes: 0 additions & 77 deletions src/Nested/Builder/ParentNode.php

This file was deleted.

Loading

0 comments on commit 0b39745

Please sign in to comment.