mirror of
https://github.com/Karaka-Management/phpOMS.git
synced 2026-01-11 09:48:40 +00:00
391 lines
6.8 KiB
PHP
391 lines
6.8 KiB
PHP
<?php
|
|
/**
|
|
* Orange Management
|
|
*
|
|
* PHP Version 8.0
|
|
*
|
|
* @package phpOMS\Uri
|
|
* @copyright Dennis Eichhorn
|
|
* @license OMS License 1.0
|
|
* @version 1.0.0
|
|
* @link https://orange-management.org
|
|
*/
|
|
declare(strict_types=1);
|
|
|
|
namespace phpOMS\Uri;
|
|
|
|
use phpOMS\Utils\StringUtils;
|
|
|
|
/**
|
|
* Console argument class.
|
|
*
|
|
* Considers arguments used in ca CLI as uri
|
|
*
|
|
* @package phpOMS\Uri
|
|
* @license OMS License 1.0
|
|
* @link https://orange-management.org
|
|
* @since 1.0.0
|
|
*
|
|
* @SuppressWarnings(PHPMD.Superglobals)
|
|
*/
|
|
final class Argument implements UriInterface
|
|
{
|
|
/**
|
|
* Root path.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
private string $rootPath = '/';
|
|
|
|
/**
|
|
* Path offset.
|
|
*
|
|
* @var int
|
|
* @since 1.0.0
|
|
*/
|
|
private int $pathOffset = 0;
|
|
|
|
/**
|
|
* Path elements.
|
|
*
|
|
* @var string[]
|
|
* @since 1.0.0
|
|
*/
|
|
private array $pathElements = [];
|
|
|
|
/**
|
|
* Uri.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
private string $uri = '';
|
|
|
|
/**
|
|
* Uri scheme.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
public string $scheme = '';
|
|
|
|
/**
|
|
* Uri host.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
public string $host = '';
|
|
|
|
/**
|
|
* Uri port.
|
|
*
|
|
* @var int
|
|
* @since 1.0.0
|
|
*/
|
|
public int $port = 0;
|
|
|
|
/**
|
|
* Uri user.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
public string $user = '';
|
|
|
|
/**
|
|
* Uri password.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
public string $pass = '';
|
|
|
|
/**
|
|
* Uri path.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
private string $path = '';
|
|
|
|
/**
|
|
* Uri query.
|
|
*
|
|
* @var array<string, mixed>
|
|
* @since 1.0.0
|
|
*/
|
|
private array $query = [];
|
|
|
|
/**
|
|
* Uri query.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
private string $queryString = '';
|
|
|
|
/**
|
|
* Uri fragment.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
public string $fragment = '';
|
|
|
|
/**
|
|
* Uri base.
|
|
*
|
|
* @var string
|
|
* @since 1.0.0
|
|
*/
|
|
private string $base = '';
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param string $uri Uri
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function __construct(string $uri = '')
|
|
{
|
|
$this->set($uri);
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function set(string $uri) : void
|
|
{
|
|
$this->uri = $uri;
|
|
|
|
$this->setInternalPath($uri);
|
|
$this->setQuery($uri);
|
|
$this->setInternalFragment($uri);
|
|
}
|
|
|
|
/**
|
|
* Set path from uri.
|
|
*
|
|
* @param string $uri Uri to parse
|
|
*
|
|
* @return void
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private function setInternalPath(string $uri) : void
|
|
{
|
|
$start = stripos($uri, ':');
|
|
|
|
if ($start === false) {
|
|
return;
|
|
}
|
|
|
|
$end = stripos($uri, ' ', $start + 1);
|
|
|
|
if ($end === false) {
|
|
$end = \strlen($uri); // @codeCoverageIgnore
|
|
}
|
|
|
|
$path = $start < 8 ? substr($uri, $start + 1, $end - $start - 1) : $uri;
|
|
$this->path = $path === false ? '' : ltrim($path, ':');
|
|
|
|
if (StringUtils::endsWith($this->path, '.php')) {
|
|
$path = substr($this->path, 0, -4);
|
|
|
|
if ($path === false) {
|
|
throw new \Exception(); // @codeCoverageIgnore
|
|
}
|
|
|
|
$this->path = $path;
|
|
}
|
|
|
|
$this->pathElements = explode('/', ltrim($this->path, '/'));
|
|
}
|
|
|
|
/**
|
|
* Set query from uri.
|
|
*
|
|
* @param string $uri Uri to parse
|
|
*
|
|
* @return void
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private function setQuery(string $uri) : void
|
|
{
|
|
$result = preg_match_all('/\?([a-zA-Z0-9]*)(=)([a-zA-Z0-9]*)/', $uri, $matches);
|
|
|
|
if ($result === false || empty($matches)) {
|
|
return;
|
|
}
|
|
|
|
foreach ($matches[1] as $key => $value) {
|
|
$this->query[$value] = $matches[3][$key];
|
|
$this->queryString .= ' ?' . $value . '=' . $matches[3][$key];
|
|
}
|
|
|
|
$this->queryString = ltrim($this->queryString);
|
|
}
|
|
|
|
/**
|
|
* Set fragment from uri.
|
|
*
|
|
* @param string $uri Uri to parse
|
|
*
|
|
* @return void
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
private function setInternalFragment(string $uri) : void
|
|
{
|
|
$result = preg_match('/#([a-zA-Z0-9]*)/', $uri, $matches);
|
|
|
|
if ($result === 1) {
|
|
$this->fragment = $matches[1] ?? '';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public static function isValid(string $uri) : bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getRootPath() : string
|
|
{
|
|
return $this->rootPath;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function setRootPath(string $root) : void
|
|
{
|
|
$this->rootPath = $root;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function setPathOffset(int $offset = 0) : void
|
|
{
|
|
$this->pathOffset = $offset;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getPath() : string
|
|
{
|
|
return $this->path;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function setPath(string $path) : void
|
|
{
|
|
$this->path = $path;
|
|
$this->pathElements = explode('/', ltrim($this->path, '/'));
|
|
}
|
|
|
|
/**
|
|
* Get path offset.
|
|
*
|
|
* @return int
|
|
*
|
|
* @since 1.0.0
|
|
*/
|
|
public function getPathOffset() : int
|
|
{
|
|
return $this->pathOffset;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getRoute() : string
|
|
{
|
|
$query = $this->getQuery();
|
|
return $this->path . (!empty($query) ? ' ' . $this->getQuery() : '');
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getQuery(string $key = null) : string
|
|
{
|
|
if ($key !== null) {
|
|
$key = strtolower($key);
|
|
|
|
return $this->query[$key] ?? '';
|
|
}
|
|
|
|
return $this->queryString;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getPathElement(int $pos = 0) : string
|
|
{
|
|
return $this->pathElements[$pos + $this->pathOffset] ?? '';
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getPathElements() : array
|
|
{
|
|
return $this->pathElements;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getQueryArray() : array
|
|
{
|
|
return $this->query;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getBase() : string
|
|
{
|
|
return $this->base;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function __toString()
|
|
{
|
|
return $this->uri;
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getAuthority() : string
|
|
{
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function getUserInfo() : string
|
|
{
|
|
return '';
|
|
}
|
|
}
|