HummingBirdAnimeClient/src/Aviat/AnimeClient/MenuGenerator.php

115 lines
2.1 KiB
PHP
Raw Normal View History

<?php
namespace Aviat\AnimeClient;
use Aviat\Ion\Di\ContainerInterface;
/**
* Helper object to manage menu creation and selection
*/
2015-10-09 14:34:55 -04:00
class MenuGenerator extends UrlGenerator {
use \Aviat\Ion\StringWrapper;
use \Aviat\Ion\ArrayWrapper;
/**
* Html generation helper
*
* @var Aura\Html\HelperLocator
*/
protected $helper;
/**
* Menu config array
*
* @var array
*/
protected $menus;
2015-10-09 14:34:55 -04:00
/**
* Request object
*
* @var Aura\Web\Request
*/
protected $request;
/**
* Create menu generator
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
parent::__construct($container);
2015-10-06 12:15:19 -04:00
$this->menus = $this->config->get('menus');
$this->helper = $container->get('html-helper');
2015-10-09 14:34:55 -04:00
$this->request = $container->get('request');
}
/**
* Generate the full menu structure from the config files
*
* @return array
*/
protected function parse_config()
{
// Note: Children menus have urls based on the
// current url path
/*
2015-10-09 14:34:55 -04:00
parsed = {
menu_name: {
title: 'full_url_path'
}
}
*/
$parsed = [];
foreach ($this->menus as $name => $menu)
{
$parsed[$name] = [];
foreach ($menu['items'] as $path_name => $partial_path)
{
2015-10-09 14:34:55 -04:00
$title = (string) $this->string($path_name)->humanize()->titleize();
$parsed[$name][$title] = (string) $this->string($menu['route_prefix'])->append($partial_path);
}
}
return $parsed;
}
/**
* Generate the html structure of the menu selected
*
* @param string $menu
* @return string
*/
public function generate($menu)
{
$parsed_config = $this->parse_config();
2015-10-09 14:34:55 -04:00
// Bail out early on invalid menu
if ( ! $this->arr($parsed_config)->has_key($menu))
{
return '';
}
$menu_config = $parsed_config[$menu];
2015-10-09 14:34:55 -04:00
foreach($menu_config as $title => $path)
{
$selected = $this->string($path)->contains($this->path());
$link = $this->helper->a($this->url($path), $title);
2015-10-09 14:34:55 -04:00
$attrs = ($selected)
? ['class' => 'selected']
: [];
2015-10-09 14:34:55 -04:00
$this->helper->ul()->rawItem($link, $attrs);
}
2015-10-09 14:34:55 -04:00
// Create the menu html
return $this->helper->ul();
}
}
// End of MenuGenerator.php