Various tweaks

This commit is contained in:
Timothy Warren 2023-10-02 11:48:34 -04:00
parent c8b642be1c
commit 7559f79ef6
4 changed files with 25 additions and 7 deletions

View File

@ -106,7 +106,7 @@ final class Model
public function createListItem(array $data, string $type = 'anime'): ?Request public function createListItem(array $data, string $type = 'anime'): ?Request
{ {
$mediaId = $this->getMediaId($data, $type); $mediaId = $this->getMediaId($data, $type);
if ($mediaId === NULL) if ($mediaId === NULL || $mediaId === "undefined")
{ {
return NULL; return NULL;
} }

View File

@ -14,6 +14,7 @@
namespace Aviat\AnimeClient; namespace Aviat\AnimeClient;
use Aura\Router\Generator;
use Aviat\Ion\ConfigInterface; use Aviat\Ion\ConfigInterface;
use Aviat\Ion\Di\ContainerInterface; use Aviat\Ion\Di\ContainerInterface;
use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException}; use Aviat\Ion\Di\Exception\{ContainerException, NotFoundException};
@ -24,7 +25,7 @@ use Psr\Http\Message\ServerRequestInterface;
/** /**
* Base for routing/url classes * Base for routing/url classes
*/ */
class RoutingBase abstract class RoutingBase
{ {
/** /**
* Config Object * Config Object
@ -36,6 +37,11 @@ class RoutingBase
*/ */
protected ServerRequestInterface $request; protected ServerRequestInterface $request;
/**
* Aura url generator
*/
protected Generator $routerUrl;
/** /**
* Constructor * Constructor
* *
@ -47,6 +53,7 @@ class RoutingBase
{ {
$this->config = $container->get('config'); $this->config = $container->get('config');
$this->request = $container->get('request'); $this->request = $container->get('request');
$this->routerUrl = $container->get('aura-router')->getGenerator();
} }
/** /**

View File

@ -54,6 +54,11 @@ class UrlGenerator extends RoutingBase
return implode('/', $args); return implode('/', $args);
} }
public function fromRoute(string $route, array $args = []): string
{
return $this->hostUrl($this->routerUrl->generate($route, $args));
}
/** /**
* Generate a proper url from the path * Generate a proper url from the path
*/ */
@ -81,9 +86,7 @@ class UrlGenerator extends RoutingBase
$path = implode('/', $pathSegments); $path = implode('/', $pathSegments);
$scheme = $this->config->get('secure_urls') !== FALSE ? 'https:' : 'http:'; return $this->hostUrl($path);
return "{$scheme}//{$this->host}/{$path}";
} }
/** /**
@ -103,6 +106,14 @@ class UrlGenerator extends RoutingBase
throw new InvalidArgumentException("Invalid default type: '{$type}'"); throw new InvalidArgumentException("Invalid default type: '{$type}'");
} }
private function hostUrl(string $path): string
{
$path = trim($path, '/');
$scheme = $this->config->get('secure_urls') !== FALSE ? 'https:' : 'http:';
return "{$scheme}//{$this->host}/{$path}";
}
} }
// End of UrlGenerator.php // End of UrlGenerator.php

View File

@ -25,9 +25,9 @@ const SRC_DIR = __DIR__;
const USER_AGENT = "Tim's Anime Client/5.2"; const USER_AGENT = "Tim's Anime Client/5.2";
// Regex patterns // Regex patterns
const ALPHA_SLUG_PATTERN = '[a-z_]+'; const ALPHA_SLUG_PATTERN = '[a-zA-Z_]+';
const NUM_PATTERN = '[0-9]+'; const NUM_PATTERN = '[0-9]+';
const SLUG_PATTERN = '[a-z0-9\-]+'; const SLUG_PATTERN = '[a-zA-Z0-9\-]+';
const SLUG_SPACE_PATTERN = '[a-zA-Z_\- ]+'; const SLUG_SPACE_PATTERN = '[a-zA-Z_\- ]+';
// Why doesn't this already exist? // Why doesn't this already exist?