HummingBirdAnimeClient/src/AnimeClient/RoutingBase.php

104 lines
2.1 KiB
PHP
Raw Normal View History

2016-10-20 22:09:36 -04:00
<?php declare(strict_types=1);
2015-09-14 19:54:34 -04: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
*
2023-07-13 11:08:05 -04:00
* PHP version 8.1
2016-08-30 10:01:18 -04:00
*
2023-07-13 11:08:05 -04:00
* @copyright 2015 - 2023 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
2023-07-13 11:08:05 -04:00
* @link https://git.timshomepage.net/timw4mail/HummingBirdAnimeClient
2015-09-14 19:54:34 -04:00
*/
2016-10-20 22:09:36 -04:00
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient;
2015-09-14 15:49:20 -04:00
2023-10-02 11:48:34 -04:00
use Aura\Router\Generator;
2020-04-10 20:01:46 -04:00
use Aviat\Ion\ConfigInterface;
2015-09-17 23:11:18 -04:00
use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
2019-12-09 14:34:23 -05:00
use Aviat\Ion\Exception\ConfigException;
2020-04-10 20:01:46 -04:00
use Aviat\Ion\Type\StringType;
2021-02-11 19:54:22 -05:00
use Psr\Http\Message\ServerRequestInterface;
2015-09-17 23:11:18 -04:00
2015-09-14 19:54:34 -04:00
/**
* Base for routing/url classes
*/
2023-10-02 11:48:34 -04:00
abstract class RoutingBase
{
2015-09-14 15:49:20 -04:00
/**
* Config Object
*/
2020-04-10 20:01:46 -04:00
protected ConfigInterface $config;
2015-09-14 15:49:20 -04:00
/**
2019-01-07 09:08:00 -05:00
* Class wrapper for input superglobals
2015-09-14 15:49:20 -04:00
*/
2021-02-11 19:54:22 -05:00
protected ServerRequestInterface $request;
2015-09-14 15:49:20 -04:00
2023-10-02 11:48:34 -04:00
/**
* Aura url generator
*/
protected Generator $routerUrl;
2015-09-14 15:49:20 -04:00
/**
* Constructor
*
* @throws ConfigException
2019-12-09 14:34:23 -05:00
* @throws ContainerException
* @throws NotFoundException
2015-09-14 15:49:20 -04:00
*/
public function __construct(protected ContainerInterface $container)
2015-09-14 15:49:20 -04:00
{
$this->config = $container->get('config');
2019-01-07 09:08:00 -05:00
$this->request = $container->get('request');
2023-10-02 11:48:34 -04:00
$this->routerUrl = $container->get('aura-router')->getGenerator();
2015-09-14 15:49:20 -04:00
}
2015-10-09 14:34:55 -04:00
/**
* Get the current url path
*/
2018-02-02 09:50:58 -05:00
public function path(): string
2015-10-09 14:34:55 -04:00
{
2019-01-07 09:08:00 -05:00
$path = $this->request->getUri()->getPath();
2020-04-10 20:01:46 -04:00
$cleanedPath = StringType::from($path)
2016-03-03 16:53:17 -05:00
->replace('%20', '')
2015-10-09 14:34:55 -04:00
->trim()
->trimRight('/')
->ensureLeft('/');
return (string) $cleanedPath;
2015-10-09 14:34:55 -04:00
}
/**
* Get the url segments
*/
2018-02-02 09:50:58 -05:00
public function segments(): array
2015-10-09 14:34:55 -04:00
{
$path = $this->path();
2015-11-09 11:10:15 -05:00
return explode('/', $path);
2015-10-09 14:34:55 -04:00
}
/**
* Get a segment of the current url
*/
2021-02-11 19:54:22 -05:00
public function getSegment(int $num): ?string
2015-10-09 14:34:55 -04:00
{
$segments = $this->segments();
2018-02-02 09:50:58 -05:00
return $segments[$num] ?? NULL;
2015-10-09 14:34:55 -04:00
}
/**
* Retrieve the last url segment
*/
2018-02-02 09:50:58 -05:00
public function lastSegment(): string
2015-10-09 14:34:55 -04:00
{
$segments = $this->segments();
2015-10-09 14:34:55 -04:00
return end($segments);
}
}