MenuGenerator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * Hummingbird Anime Client
4
 *
5
 * An API client for Hummingbird to manage anime and manga watch lists
6
 *
7
 * PHP version 7
8
 *
9
 * @package     HummingbirdAnimeClient
10
 * @author      Timothy J. Warren <[email protected]>
11
 * @copyright   2015 - 2016  Timothy J. Warren
12
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
13
 * @version     3.1
14
 * @link        https://github.com/timw4mail/HummingBirdAnimeClient
15
 */
16
17
namespace Aviat\AnimeClient;
18
19
use Aviat\Ion\
20
{
21
	ArrayWrapper, StringWrapper
22
};
23
use Aviat\Ion\Di\ContainerInterface;
24
25
/**
26
 * Helper object to manage menu creation and selection
27
 */
28
class MenuGenerator extends UrlGenerator {
29
30
	use ArrayWrapper;
31
	use StringWrapper;
32
33
	/**
34
	 * Html generation helper
35
	 *
36
	 * @var Aura\Html\HelperLocator
37
	 */
38
	protected $helper;
39
40
	/**
41
	 * Request object
42
	 *
43
	 * @var Aura\Web\Request
44
	 */
45
	protected $request;
46
47
	/**
48
	 * Create menu generator
49
	 *
50
	 * @param ContainerInterface $container
51
	 */
52
	public function __construct(ContainerInterface $container)
53
	{
54
		parent::__construct($container);
55
		$this->helper = $container->get('html-helper');
56
		$this->request = $container->get('request');
57
	}
58
59
	/**
60
	 * Generate the full menu structure from the config files
61
	 *
62
	 * @param array $menus
63
	 * @return array
64
	 */
65
	protected function parse_config(array $menus)
66
	{
67
		$parsed = [];
68
69
		foreach ($menus as $name => $menu)
70
		{
71
			$parsed[$name] = [];
72
			foreach ($menu['items'] as $path_name => $partial_path)
73
			{
74
				$title = (string)$this->string($path_name)->humanize()->titleize();
75
				$parsed[$name][$title] = (string)$this->string($menu['route_prefix'])->append($partial_path);
76
			}
77
		}
78
79
		return $parsed;
80
	}
81
82
	/**
83
	 * Generate the html structure of the menu selected
84
	 *
85
	 * @param string $menu
86
	 * @return string
87
	 */
88
	public function generate($menu)
89
	{
90
		$menus = $this->config->get('menus');
91
		$parsed_config = $this->parse_config($menus);
92
93
		// Bail out early on invalid menu
94
		if ( ! $this->arr($parsed_config)->hasKey($menu))
95
		{
96
			return '';
97
		}
98
99
		$menu_config = $parsed_config[$menu];
100
101
		foreach ($menu_config as $title => $path)
102
		{
103
			$has = $this->string($this->path())->contains($path);
104
			$selected = ($has && strlen($this->path()) >= strlen($path));
105
106
			$link = $this->helper->a($this->url($path), $title);
107
108
			$attrs = ($selected)
109
				? ['class' => 'selected']
110
				: [];
111
112
			$this->helper->ul()->rawItem($link, $attrs);
113
		}
114
115
		// Create the menu html
116
		return $this->helper->ul();
117
	}
118
}
119
// End of MenuGenerator.php