forked from KnpLabs/KnpMenuBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenuFactory.php
57 lines (49 loc) · 1.38 KB
/
MenuFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
namespace Knplabs\Bundle\MenuBundle;
/**
* Factory to create a menu from a tree
*/
class MenuFactory
{
/**
* Create a menu item from a NodeInterface
*
* @param NodeInterface $node
* @return MenuItem
*/
public function createFromNode(NodeInterface $node)
{
$item = new MenuItem($node->getName(), $this->getUriFromNode($node), $node->getAttributes());
$item->setLabel($node->getLabel());
foreach ($node->getChildren() as $childNode) {
$item->addChild($this->createFromNode($childNode));
}
return $item;
}
/**
* Creates a new menu item (and tree if $data['children'] is set).
*
* The source is an array of data that should match the output from MenuItem->toArray().
*
* @param array $data The array of data to use as a source for the menu tree
* @return MenuItem
*/
public function createFromArray(array $data)
{
$class = isset($data['class']) ? $data['class'] : 'MenuItem';
$name = isset($data['name']) ? $data['name'] : null;
$menu = new $class($name);
$menu->fromArray($data);
return $menu;
}
/**
* Get the uri for the given node
*
* @param NodeInterface $node
* @return string
*/
protected function getUriFromNode(NodeInterface $node)
{
return $node->getUri();
}
}