2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-09-14 19:54:34 -04:00
|
|
|
/**
|
2017-02-16 11:09:37 -05:00
|
|
|
* Hummingbird Anime List Client
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2017-02-16 11:09:37 -05:00
|
|
|
* An API client for Kitsu and MyAnimeList to manage anime and manga watch lists
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2016-10-20 22:09:36 -04:00
|
|
|
* PHP version 7
|
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>
|
2017-02-16 11:09:37 -05:00
|
|
|
* @copyright 2015 - 2017 Timothy J. Warren
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2017-02-16 11:09:37 -05:00
|
|
|
* @version 4.0
|
2017-03-07 20:53:58 -05: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
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2017-02-15 16:30:14 -05:00
|
|
|
use Aviat\Ion\StringWrapper;
|
2015-09-17 23:11:18 -04:00
|
|
|
|
2015-09-14 19:54:34 -04:00
|
|
|
/**
|
|
|
|
* Base for routing/url classes
|
|
|
|
*/
|
2015-09-14 15:49:20 -04:00
|
|
|
class RoutingBase {
|
2015-10-09 14:34:55 -04:00
|
|
|
|
2017-02-15 16:30:14 -05:00
|
|
|
use StringWrapper;
|
2015-10-09 14:34:55 -04:00
|
|
|
|
2015-09-14 15:49:20 -04:00
|
|
|
/**
|
|
|
|
* Injection Container
|
2015-10-12 14:27:20 -04:00
|
|
|
* @var ContainerInterface $container
|
2015-09-14 15:49:20 -04:00
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Config Object
|
2017-02-22 14:46:35 -05:00
|
|
|
* @var \Aviat\Ion\Config
|
2015-09-14 15:49:20 -04:00
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Routing array
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $routes;
|
|
|
|
|
2016-02-02 21:28:32 -05:00
|
|
|
/**
|
|
|
|
* Route configuration options
|
|
|
|
* @var array
|
|
|
|
*/
|
2017-02-15 16:30:14 -05:00
|
|
|
protected $routeConfig;
|
2016-02-02 21:28:32 -05:00
|
|
|
|
2015-09-14 15:49:20 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2015-09-14 15:49:20 -04:00
|
|
|
*/
|
2015-09-17 23:11:18 -04:00
|
|
|
public function __construct(ContainerInterface $container)
|
2015-09-14 15:49:20 -04:00
|
|
|
{
|
|
|
|
$this->container = $container;
|
|
|
|
$this->config = $container->get('config');
|
2017-02-15 16:30:14 -05:00
|
|
|
$baseRoutes = $this->config->get('routes');
|
|
|
|
$this->routes = $baseRoutes['routes'];
|
|
|
|
$this->routeConfig = $baseRoutes['route_config'];
|
2015-09-14 15:49:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retreive the appropriate value for the routing key
|
|
|
|
*
|
|
|
|
* @param string $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($key)
|
|
|
|
{
|
2017-02-15 16:30:14 -05:00
|
|
|
$routingConfig =& $this->routeConfig;
|
2015-09-14 15:49:20 -04:00
|
|
|
|
2017-02-15 16:30:14 -05:00
|
|
|
if (array_key_exists($key, $routingConfig))
|
2015-09-14 15:49:20 -04:00
|
|
|
{
|
2017-02-15 16:30:14 -05:00
|
|
|
return $routingConfig[$key];
|
2015-09-14 15:49:20 -04:00
|
|
|
}
|
|
|
|
}
|
2015-10-09 14:34:55 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current url path
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function path()
|
|
|
|
{
|
|
|
|
$request = $this->container->get('request');
|
2016-02-17 10:29:05 -05:00
|
|
|
$path = $request->getUri()->getPath();
|
2017-02-15 16:30:14 -05:00
|
|
|
$cleanedPath = $this->string($path)
|
2016-03-03 16:53:17 -05:00
|
|
|
->replace('%20', '')
|
2015-10-09 14:34:55 -04:00
|
|
|
->trim()
|
|
|
|
->trimRight('/')
|
|
|
|
->ensureLeft('/');
|
|
|
|
|
2017-02-15 16:30:14 -05:00
|
|
|
return (string)$cleanedPath;
|
2015-10-09 14:34:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the url segments
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function segments()
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
*
|
|
|
|
* @param int $num
|
|
|
|
* @return string|null
|
|
|
|
*/
|
2017-02-15 16:30:14 -05:00
|
|
|
public function getSegment($num)
|
2015-10-09 14:34:55 -04:00
|
|
|
{
|
|
|
|
$segments = $this->segments();
|
|
|
|
return (array_key_exists($num, $segments)) ? $segments[$num] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the last url segment
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-02-15 16:30:14 -05:00
|
|
|
public function lastSegment()
|
2015-10-09 14:34:55 -04:00
|
|
|
{
|
|
|
|
$segments = $this->segments();
|
|
|
|
return end($segments);
|
|
|
|
}
|
2015-09-14 15:49:20 -04:00
|
|
|
}
|
|
|
|
// End of RoutingBase.php
|