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
|
|
|
*
|
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
|
|
|
*/
|
2015-10-05 16:54:25 -04:00
|
|
|
|
|
|
|
namespace Aviat\AnimeClient;
|
|
|
|
|
2019-12-09 14:34:23 -05:00
|
|
|
use Aura\Html\HelperLocator;
|
2015-10-05 16:54:25 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2022-03-03 17:26:09 -05:00
|
|
|
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
|
2018-01-16 14:58:07 -05:00
|
|
|
use Aviat\Ion\Exception\ConfigException;
|
2022-03-03 17:26:09 -05:00
|
|
|
use Aviat\Ion\Type\{ArrayType, StringType};
|
2021-02-11 19:54:22 -05:00
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
2015-10-05 16:54:25 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper object to manage menu creation and selection
|
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
final class MenuGenerator extends UrlGenerator
|
|
|
|
{
|
2015-10-05 16:54:25 -04:00
|
|
|
/**
|
|
|
|
* Html generation helper
|
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
protected HelperLocator $helper;
|
2015-10-05 16:54:25 -04:00
|
|
|
|
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
|
|
|
|
2020-08-21 12:30:01 -04:00
|
|
|
public static function new(ContainerInterface $container): self
|
2015-10-05 16:54:25 -04:00
|
|
|
{
|
2020-12-10 17:04:45 -05:00
|
|
|
return new self($container);
|
2015-10-05 16:54:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate the html structure of the menu selected
|
|
|
|
*
|
2018-01-16 14:58:07 -05:00
|
|
|
* @throws ConfigException
|
2015-10-05 16:54:25 -04:00
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
public function generate(string $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
|
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];
|
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
|
|
|
{
|
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));
|
2015-10-21 11:57:58 -04:00
|
|
|
|
2020-05-18 12:53:00 -04:00
|
|
|
$linkAttrs = ($selected)
|
|
|
|
? ['aria-current' => 'location']
|
|
|
|
: [];
|
|
|
|
$link = $this->helper->a($this->url($path), $title, $linkAttrs);
|
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
|
|
|
}
|
2020-08-21 12:30:01 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2022-03-03 13:25:10 -05:00
|
|
|
* @return array<mixed, array<string, string>>
|
2020-08-21 12:30:01 -04:00
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
private function parseConfig(array $menus): array
|
2020-08-21 12:30:01 -04:00
|
|
|
{
|
|
|
|
$parsed = [];
|
|
|
|
|
|
|
|
foreach ($menus as $name => $menu)
|
|
|
|
{
|
|
|
|
$parsed[$name] = [];
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-08-21 12:30:01 -04:00
|
|
|
foreach ($menu['items'] as $pathName => $partialPath)
|
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
$title = (string) StringType::from($pathName)->humanize()->titleize();
|
|
|
|
$parsed[$name][$title] = (string) StringType::from($menu['route_prefix'])->append($partialPath);
|
2020-08-21 12:30:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $parsed;
|
|
|
|
}
|
2015-10-05 16:54:25 -04:00
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
// End of MenuGenerator.php
|