2015-10-05 16:54:25 -04:00
|
|
|
<?php
|
2015-11-16 11:40:01 -05:00
|
|
|
/**
|
|
|
|
* Hummingbird Anime Client
|
|
|
|
*
|
|
|
|
* An API client for Hummingbird to manage anime and manga watch lists
|
|
|
|
*
|
2016-08-30 10:01:18 -04:00
|
|
|
* PHP version 5.6
|
|
|
|
*
|
2015-11-16 11:40:01 -05:00
|
|
|
* @package HummingbirdAnimeClient
|
2016-08-30 10:01:18 -04:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2016 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 3.1
|
2015-11-16 11:40:01 -05:00
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
*/
|
2015-10-05 16:54:25 -04:00
|
|
|
|
|
|
|
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 {
|
2015-10-05 16:54:25 -04:00
|
|
|
|
|
|
|
use \Aviat\Ion\StringWrapper;
|
|
|
|
use \Aviat\Ion\ArrayWrapper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Html generation helper
|
|
|
|
*
|
|
|
|
* @var Aura\Html\HelperLocator
|
|
|
|
*/
|
|
|
|
protected $helper;
|
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
/**
|
|
|
|
* Request object
|
|
|
|
*
|
|
|
|
* @var Aura\Web\Request
|
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
/**
|
|
|
|
* Create menu generator
|
|
|
|
*
|
|
|
|
* @param ContainerInterface $container
|
|
|
|
*/
|
|
|
|
public function __construct(ContainerInterface $container)
|
|
|
|
{
|
|
|
|
parent::__construct($container);
|
|
|
|
$this->helper = $container->get('html-helper');
|
2015-10-09 14:34:55 -04:00
|
|
|
$this->request = $container->get('request');
|
2015-10-05 16:54:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the full menu structure from the config files
|
|
|
|
*
|
2015-11-16 15:57:37 -05:00
|
|
|
* @param array $menus
|
2015-10-05 16:54:25 -04:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-10-15 09:25:30 -04:00
|
|
|
protected function parse_config(array $menus)
|
2015-10-05 16:54:25 -04:00
|
|
|
{
|
|
|
|
$parsed = [];
|
|
|
|
|
2015-10-15 09:25:30 -04:00
|
|
|
foreach ($menus as $name => $menu)
|
2015-10-05 16:54:25 -04:00
|
|
|
{
|
|
|
|
$parsed[$name] = [];
|
2015-10-06 10:24:48 -04:00
|
|
|
foreach ($menu['items'] as $path_name => $partial_path)
|
2015-10-05 16:54:25 -04:00
|
|
|
{
|
2015-10-09 14:55:15 -04:00
|
|
|
$title = (string)$this->string($path_name)->humanize()->titleize();
|
|
|
|
$parsed[$name][$title] = (string)$this->string($menu['route_prefix'])->append($partial_path);
|
2015-10-05 16:54:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parsed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the html structure of the menu selected
|
|
|
|
*
|
|
|
|
* @param string $menu
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function generate($menu)
|
|
|
|
{
|
2015-10-15 09:25:30 -04:00
|
|
|
$menus = $this->config->get('menus');
|
|
|
|
$parsed_config = $this->parse_config($menus);
|
2015-10-05 16:54:25 -04:00
|
|
|
|
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-05 16:54:25 -04:00
|
|
|
|
2015-10-09 14:55:15 -04:00
|
|
|
foreach ($menu_config as $title => $path)
|
2015-10-09 14:34:55 -04:00
|
|
|
{
|
2015-10-21 11:57:58 -04:00
|
|
|
$has = $this->string($path)->contains($this->path());
|
|
|
|
$selected = ($has && strlen($path) >= strlen($this->path()));
|
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
$link = $this->helper->a($this->url($path), $title);
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
$attrs = ($selected)
|
|
|
|
? ['class' => 'selected']
|
|
|
|
: [];
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
$this->helper->ul()->rawItem($link, $attrs);
|
|
|
|
}
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
// Create the menu html
|
|
|
|
return $this->helper->ul();
|
2015-10-05 16:54:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of MenuGenerator.php
|