1: <?php
2:
3: namespace Charcoal\Ui\Menu;
4:
5: use \Pimple\Container;
6:
7: // From 'charcoal-factory'
8: use \Charcoal\Factory\FactoryInterface;
9:
10: /**
11: * Menu Builder
12: */
13: class MenuBuilder
14: {
15: /**
16: * The default, concrete, menu model.
17: *
18: * @const string
19: */
20: const DEFAULT_TYPE = 'charcoal/ui/menu/generic';
21:
22: /**
23: * Store the menu factory instance.
24: *
25: * @var FactoryInterface
26: */
27: protected $factory;
28:
29: /**
30: * Store the dependency-injection container to fulfill the required services.
31: *
32: * @var Container $container
33: */
34: protected $container;
35:
36: /**
37: * Return a new menu builder.
38: *
39: * @param FactoryInterface $factory A menu factory.
40: * @param Container $container The DI container.
41: */
42: public function __construct(FactoryInterface $factory, Container $container)
43: {
44: $this->factory = $factory;
45: $this->container = $container;
46: }
47:
48: /**
49: * Build and return a new menu.
50: *
51: * @param array|\ArrayAccess $options The menu build options.
52: * @return MenuInterface
53: */
54: public function build($options)
55: {
56: $container = $this->container;
57: $objType = isset($options['type']) ? $options['type'] : self::DEFAULT_TYPE;
58:
59: $obj = $this->factory->create($objType, [
60: 'logger' => $container['logger'],
61: 'view' => $container['view'],
62: 'menu_item_builder' => $container['menu/item/builder'],
63: 'container' => $container
64: ]);
65: $obj->setData($options);
66:
67: return $obj;
68: }
69: }
70: