2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-11-16 11:40:01 -05:00
|
|
|
/**
|
2017-02-16 11:09:37 -05:00
|
|
|
* Hummingbird Anime List Client
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2019-12-03 15:17:25 -05:00
|
|
|
* PHP version 7.2
|
2016-08-30 10:01:18 -04:00
|
|
|
*
|
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>
|
2020-01-08 15:39:49 -05:00
|
|
|
* @copyright 2015 - 2020 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2019-12-06 09:16:35 -05:00
|
|
|
* @version 4.2
|
2017-03-07 20:53:58 -05:00
|
|
|
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
|
2015-11-16 11:40:01 -05:00
|
|
|
*/
|
2015-10-05 16:54:25 -04:00
|
|
|
|
|
|
|
namespace Aviat\AnimeClient;
|
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
use Aviat\Ion\{ArrayWrapper, StringWrapper};
|
|
|
|
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
|
|
|
|
use Aura\Html\HelperLocator;
|
2015-10-05 16:54:25 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2018-01-16 14:58:07 -05:00
|
|
|
use Aviat\Ion\Exception\ConfigException;
|
2019-12-09 14:34:23 -05:00
|
|
|
use Psr\Http\Message\RequestInterface;
|
2015-10-05 16:54:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper object to manage menu creation and selection
|
|
|
|
*/
|
2018-08-08 10:12:45 -04:00
|
|
|
final class MenuGenerator extends UrlGenerator {
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2016-10-20 22:09:36 -04:00
|
|
|
use ArrayWrapper;
|
|
|
|
use StringWrapper;
|
2015-10-05 16:54:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Html generation helper
|
|
|
|
*
|
2019-12-09 14:34:23 -05:00
|
|
|
* @var HelperLocator
|
2015-10-05 16:54:25 -04:00
|
|
|
*/
|
|
|
|
protected $helper;
|
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
/**
|
|
|
|
* Request object
|
|
|
|
*
|
2019-12-09 14:34:23 -05:00
|
|
|
* @var RequestInterface
|
2015-10-09 14:34:55 -04:00
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
2015-10-05 16:54:25 -04:00
|
|
|
/**
|
2018-11-09 10:38:35 -05:00
|
|
|
* MenuGenerator constructor.
|
2015-10-05 16:54:25 -04:00
|
|
|
*
|
|
|
|
* @param ContainerInterface $container
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws ContainerException
|
|
|
|
* @throws NotFoundException
|
2015-10-05 16:54:25 -04:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
2018-09-20 10:41:28 -04:00
|
|
|
protected function parseConfig(array $menus) : array
|
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
|
2018-01-16 14:58:07 -05:00
|
|
|
* @throws ConfigException
|
2015-10-05 16:54:25 -04:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-09-20 10:41:28 -04:00
|
|
|
public function generate($menu) : string
|
2015-10-05 16:54:25 -04:00
|
|
|
{
|
2015-10-15 09:25:30 -04:00
|
|
|
$menus = $this->config->get('menus');
|
2017-02-16 14:30:06 -05:00
|
|
|
$parsedConfig = $this->parseConfig($menus);
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
// Bail out early on invalid menu
|
2017-02-16 14:30:06 -05:00
|
|
|
if ( ! $this->arr($parsedConfig)->hasKey($menu))
|
2015-10-09 14:34:55 -04:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
$menuConfig = $parsedConfig[$menu];
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
foreach ($menuConfig as $title => $path)
|
2015-10-09 14:34:55 -04:00
|
|
|
{
|
2017-01-25 12:13:37 -05:00
|
|
|
$has = $this->string($this->path())->contains($path);
|
2017-04-17 16:13:36 -04:00
|
|
|
$selected = ($has && mb_strlen($this->path()) >= mb_strlen($path));
|
2015-10-21 11:57:58 -04:00
|
|
|
|
2015-10-09 14:34:55 -04:00
|
|
|
$link = $this->helper->a($this->url($path), $title);
|
2015-10-05 16:54:25 -04:00
|
|
|
|
2018-11-09 10:38:35 -05:00
|
|
|
$attrs = $selected
|
2015-10-09 14:34:55 -04:00
|
|
|
? ['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
|
2018-09-20 10:41:28 -04:00
|
|
|
return (string) $this->helper->ul();
|
2015-10-05 16:54:25 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of MenuGenerator.php
|