HummingBirdAnimeClient/src/AnimeClient/Util.php

116 lines
2.4 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
2016-08-30 10:57:41 -04:00
/**
* Hummingbird Anime List Client
2016-08-30 10:57:41 -04:00
*
2018-08-22 13:48:27 -04:00
* An API client for Kitsu to manage anime and manga watch lists
2016-08-30 10:57:41 -04:00
*
2021-02-04 11:57:01 -05:00
* PHP version 8
2016-08-30 10:57:41 -04:00
*
2022-03-04 15:50:35 -05:00
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
2016-08-30 10:57:41 -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
2016-08-30 10:57:41 -04:00
*/
namespace Aviat\AnimeClient;
2019-12-06 15:46:56 -05:00
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
use Aviat\Ion\Di\{ContainerAware, ContainerInterface};
2016-08-30 10:57:41 -04:00
/**
* Utility method class
*/
class Util
{
2016-12-20 12:58:37 -05:00
use ContainerAware;
2016-08-30 10:57:41 -04:00
/**
* Routes that don't require a second navigation level
*/
2020-04-10 20:01:46 -04:00
private static array $formPages = [
2016-08-30 10:57:41 -04:00
'edit',
'add',
'update',
'update_form',
'login',
'logout',
2017-03-08 12:55:49 -05:00
'details',
2017-03-08 13:46:50 -05:00
'character',
'me',
2016-08-30 10:57:41 -04:00
];
/**
* Set up the Util class
*/
public function __construct(ContainerInterface $container)
{
$this->setContainer($container);
}
/**
* Absolutely equal?
*/
public static function eq(mixed $left, mixed $right): bool
{
return $left === $right;
}
/**
* Set aria-current attribute based on a condition check
*/
public static function ariaCurrent(bool $condition): string
{
return $condition ? 'true' : 'false';
}
2016-08-30 10:57:41 -04:00
/**
* HTML selection helper function
*
2020-03-16 15:06:55 -04:00
* @param string $left - First item to compare
* @param string $right - Second item to compare
2016-08-30 10:57:41 -04:00
*/
2020-03-16 15:06:55 -04:00
public static function isSelected(string $left, string $right): string
2016-08-30 10:57:41 -04:00
{
return static::eq($left, $right) ? 'selected' : '';
2016-08-30 10:57:41 -04:00
}
/**
* Inverse of selected helper function
*
2020-03-16 15:06:55 -04:00
* @param string $left - First item to compare
* @param string $right - Second item to compare
2016-08-30 10:57:41 -04:00
*/
2020-03-16 15:06:55 -04:00
public static function isNotSelected(string $left, string $right): string
2016-08-30 10:57:41 -04:00
{
2020-03-16 15:06:55 -04:00
return ($left !== $right) ? 'selected' : '';
2016-08-30 10:57:41 -04:00
}
/**
* Determine whether to show the sub-menu
*
2019-12-06 15:46:56 -05:00
* @throws ContainerException
* @throws NotFoundException
2016-08-30 10:57:41 -04:00
*/
2018-02-02 09:50:58 -05:00
public function isViewPage(): bool
2016-08-30 10:57:41 -04:00
{
2017-03-22 11:15:40 -04:00
$url = $this->container->get('request')->getUri();
2018-02-02 09:50:58 -05:00
$pageSegments = explode('/', (string) $url);
2016-08-30 10:57:41 -04:00
2017-02-16 14:30:06 -05:00
$intersect = array_intersect($pageSegments, self::$formPages);
2016-08-30 10:57:41 -04:00
return empty($intersect);
}
/**
* Determine whether the page is a page with a form, and
* not suitable for redirection
*
2019-12-06 15:46:56 -05:00
* @throws ContainerException
* @throws NotFoundException
2016-08-30 10:57:41 -04:00
*/
2018-02-02 09:50:58 -05:00
public function isFormPage(): bool
2016-08-30 10:57:41 -04:00
{
2017-02-16 14:30:06 -05:00
return ! $this->isViewPage();
2016-08-30 10:57:41 -04:00
}
}