HummingBirdAnimeClient/src/AnimeClient/MenuGenerator.php

121 lines
2.8 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
*
2021-02-04 11:57:01 -05:00
* PHP version 8
2016-08-30 10:01:18 -04:00
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
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
2022-03-04 15:50:35 -05:00
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
2015-11-16 11:40:01 -05:00
*/
namespace Aviat\AnimeClient;
2019-12-09 14:34:23 -05:00
use Aura\Html\HelperLocator;
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
2018-01-16 14:58:07 -05:00
use Aviat\Ion\Exception\ConfigException;
use Aviat\Ion\Type\{ArrayType, StringType};
2021-02-11 19:54:22 -05:00
use Psr\Http\Message\ServerRequestInterface;
/**
* Helper object to manage menu creation and selection
*/
final class MenuGenerator extends UrlGenerator
{
/**
* Html generation helper
*/
2020-04-10 20:01:46 -04:00
protected HelperLocator $helper;
2015-10-09 14:34:55 -04:00
/**
* Request object
*/
2021-02-11 19:54:22 -05:00
protected ServerRequestInterface $request;
2015-10-09 14:34:55 -04:00
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
*
2018-01-16 14:58:07 -05:00
* @throws ConfigException
*/
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.
*
* @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
*
* @return array<mixed, array<string, string>>
*/
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