HummingBirdAnimeClient/src/AnimeClient/MenuGenerator.php

133 lines
3.0 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
2015-11-16 11:40:01 -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
*
2020-12-10 17:06:50 -05:00
* PHP version 7.4+
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
2020-12-10 17:06:50 -05:00
* @version 5.2
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
2015-11-16 11:40:01 -05:00
*/
namespace Aviat\AnimeClient;
2019-12-09 14:34:23 -05:00
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
use Aura\Html\HelperLocator;
use Aviat\Ion\Di\ContainerInterface;
2018-01-16 14:58:07 -05:00
use Aviat\Ion\Exception\ConfigException;
2020-04-10 20:01:46 -04:00
use Aviat\Ion\Type\ArrayType;
use Aviat\Ion\Type\StringType;
2019-12-09 14:34:23 -05:00
use Psr\Http\Message\RequestInterface;
/**
* Helper object to manage menu creation and selection
*/
final class MenuGenerator extends UrlGenerator {
/**
* Html generation helper
*
2019-12-09 14:34:23 -05:00
* @var HelperLocator
*/
2020-04-10 20:01:46 -04:00
protected HelperLocator $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
*/
2020-04-10 20:01:46 -04:00
protected RequestInterface $request;
2015-10-09 14:34:55 -04:00
/**
* @param ContainerInterface $container
* @return static
*/
public static function new(ContainerInterface $container): self
{
2020-12-10 17:04:45 -05:00
return new self($container);
}
/**
* Generate the html structure of the menu selected
*
* @param string $menu
2018-01-16 14:58:07 -05:00
* @throws ConfigException
* @return string
*/
2020-12-10 17:04:45 -05:00
public function generate(string $menu) : string
{
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-09 14:34:55 -04:00
// Bail out early on invalid menu
2020-04-10 20:01:46 -04:00
if ( ! ArrayType::from($parsedConfig)->hasKey($menu))
2015-10-09 14:34:55 -04:00
{
return '';
}
2017-02-16 14:30:06 -05:00
$menuConfig = $parsedConfig[$menu];
2017-02-16 14:30:06 -05:00
foreach ($menuConfig as $title => $path)
2015-10-09 14:34:55 -04:00
{
2020-04-10 20:01:46 -04:00
$has = StringType::from($this->path())->contains($path);
2017-04-17 16:13:36 -04:00
$selected = ($has && mb_strlen($this->path()) >= mb_strlen($path));
$linkAttrs = ($selected)
? ['aria-current' => 'location']
: [];
$link = $this->helper->a($this->url($path), $title, $linkAttrs);
2018-11-09 10:38:35 -05:00
$attrs = $selected
2015-10-09 14:34:55 -04:00
? ['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 (string) $this->helper->ul();
}
/**
* MenuGenerator constructor.
*
* @param ContainerInterface $container
* @throws ContainerException
* @throws NotFoundException
*/
private function __construct(ContainerInterface $container)
{
parent::__construct($container);
$this->helper = $container->get('html-helper');
$this->request = $container->get('request');
}
/**
* Generate the full menu structure from the config files
*
* @param array $menus
* @return array
*/
private function parseConfig(array $menus) : array
{
$parsed = [];
foreach ($menus as $name => $menu)
{
$parsed[$name] = [];
foreach ($menu['items'] as $pathName => $partialPath)
{
$title = (string)StringType::from($pathName)->humanize()->titleize();
$parsed[$name][$title] = (string)StringType::from($menu['route_prefix'])->append($partialPath);
}
}
return $parsed;
}
}
// End of MenuGenerator.php