HummingBirdAnimeClient/src/UrlGenerator.php

114 lines
2.5 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
*
2018-10-01 11:35:51 -04:00
* PHP version 7.1
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>
2018-01-15 14:43:15 -05:00
* @copyright 2015 - 2018 Timothy J. Warren
2016-08-30 10:01:18 -04:00
* @license http://www.opensource.org/licenses/mit-license.html MIT License
2018-10-01 11:35:51 -04:00
* @version 4.1
* @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-06-26 16:39:10 -04:00
2015-10-20 15:59:51 -04:00
use Aviat\Ion\Di\ContainerInterface;
2017-02-15 15:56:10 -05:00
use InvalidArgumentException;
2015-10-20 15:59:51 -04:00
/**
* UrlGenerator class.
*/
2015-09-14 15:49:20 -04:00
class UrlGenerator extends RoutingBase {
2015-10-20 15:59:51 -04:00
/**
* The current HTTP host
2016-08-30 10:57:41 -04:00
* @var string
2015-10-20 15:59:51 -04:00
*/
protected $host;
/**
* Constructor
*
* @param ContainerInterface $container
2018-02-02 09:50:58 -05:00
* @throws \Aviat\Ion\Di\ContainerException
* @throws \Aviat\Ion\Di\NotFoundException
2015-10-20 15:59:51 -04:00
*/
public function __construct(ContainerInterface $container)
{
parent::__construct($container);
$this->host = $container->get('request')->getServerParams()['HTTP_HOST'];
2015-10-20 15:59:51 -04:00
}
/**
* Get the base url for css/js/images
*
2018-02-02 09:50:58 -05:00
* @param string[] ...$args
* @return string
*/
2018-02-02 09:50:58 -05:00
public function assetUrl(string ...$args): string
{
2018-02-02 09:50:58 -05:00
$baseUrl = rtrim($this->url(''), '/')
. $this->__get('asset_path');
2015-09-14 15:49:20 -04:00
2017-02-15 15:56:10 -05:00
array_unshift($args, $baseUrl);
2018-02-02 09:50:58 -05:00
return implode('/', $args);
}
/**
* Generate a proper url from the path
*
* @param string $path
* @return string
*/
2017-02-17 10:55:17 -05:00
public function url(string $path): string
{
$path = trim($path, '/');
$path = preg_replace('`{/.*?}`i', '', $path);
2015-10-20 15:59:51 -04:00
// Remove any optional parameters from the route
// and replace them with existing route parameters, if they exist
$path_segments = explode('/', $path);
2015-11-04 16:53:22 -05:00
$segment_count = count($path_segments);
2015-10-20 15:59:51 -04:00
$segments = $this->segments();
2015-11-04 16:53:22 -05:00
for ($i = 0; $i < $segment_count; $i++)
2015-10-20 15:59:51 -04:00
{
if ( ! array_key_exists($i + 1, $segments))
{
$segments[$i + 1] = "";
}
2018-02-02 09:50:58 -05:00
$path_segments[$i] = preg_replace('`{.*?}`', $segments[$i + 1], $path_segments[$i]);
2015-10-20 15:59:51 -04:00
}
$path = implode('/', $path_segments);
2015-10-20 15:59:51 -04:00
return "//{$this->host}/{$path}";
}
2015-09-14 15:49:20 -04:00
/**
* Full default path for the list pages
*
* @param string $type
2017-02-15 15:56:10 -05:00
* @throws InvalidArgumentException
2015-09-14 15:49:20 -04:00
* @return string
*/
2017-02-17 10:55:17 -05:00
public function defaultUrl(string $type): string
{
$type = trim($type);
2017-02-15 15:56:10 -05:00
$defaultPath = $this->__get("default_{$type}_list_path");
2018-02-02 09:50:58 -05:00
if ($defaultPath !== NULL)
{
2017-02-15 15:56:10 -05:00
return $this->url("{$type}/{$defaultPath}");
}
2017-02-15 15:56:10 -05:00
throw new InvalidArgumentException("Invalid default type: '{$type}'");
}
}
2015-10-06 12:15:19 -04:00
// End of UrlGenerator.php