mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
05f2493304
- Optimized Uri class. - Request class now uses SimpleRouter to get default-namespace as was originally the intended behavior. - Documentation changes.
164 lines
3.1 KiB
PHP
164 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Pecee\Http;
|
|
|
|
class Uri
|
|
{
|
|
private $originalUrl;
|
|
private $data = [
|
|
'scheme' => '',
|
|
'host' => '',
|
|
'port' => '',
|
|
'user' => '',
|
|
'pass' => '',
|
|
'path' => '',
|
|
'query' => '',
|
|
'fragment' => '',
|
|
];
|
|
|
|
public function __construct($url)
|
|
{
|
|
$this->originalUrl = $url;
|
|
$this->data = $this->parseUrl($url) + $this->data;
|
|
|
|
if (isset($this->data['path']) === true && $this->data['path'] !== '/') {
|
|
$this->data['path'] = rtrim($this->data['path'], '/') . '/';
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Check if url is using a secure protocol like https
|
|
* @return bool
|
|
*/
|
|
public function isSecure()
|
|
{
|
|
return (strtolower($this->getScheme()) === 'https');
|
|
}
|
|
|
|
/**
|
|
* Checks if url is relative
|
|
* @return bool
|
|
*/
|
|
public function isRelative()
|
|
{
|
|
return ($this->getHost() === null);
|
|
}
|
|
|
|
/**
|
|
* Get url scheme
|
|
* @return string|null
|
|
*/
|
|
public function getScheme()
|
|
{
|
|
return $this->data['scheme'];
|
|
}
|
|
|
|
/**
|
|
* Get url host
|
|
* @return string|null
|
|
*/
|
|
public function getHost()
|
|
{
|
|
return $this->data['host'];
|
|
}
|
|
|
|
/**
|
|
* Get url port
|
|
* @return int|null
|
|
*/
|
|
public function getPort()
|
|
{
|
|
return ($this->data['port'] !== null) ? (int)$this->data['port'] : null;
|
|
}
|
|
|
|
/**
|
|
* Parse username from url
|
|
* @return string|null
|
|
*/
|
|
public function getUserName()
|
|
{
|
|
return $this->data['user'];
|
|
}
|
|
|
|
/**
|
|
* Parse password from url
|
|
* @return string|null
|
|
*/
|
|
public function getPassword()
|
|
{
|
|
return $this->data['pass'];
|
|
}
|
|
|
|
/**
|
|
* Get path from url
|
|
* @return string
|
|
*/
|
|
public function getPath()
|
|
{
|
|
return $this->data['path'];
|
|
}
|
|
|
|
/**
|
|
* Get querystring from url
|
|
* @return string|null
|
|
*/
|
|
public function getQueryString()
|
|
{
|
|
return $this->data['query'];
|
|
}
|
|
|
|
/**
|
|
* Get fragment from url (everything after #)
|
|
* @return string|null
|
|
*/
|
|
public function getFragment()
|
|
{
|
|
return $this->data['fragment'];
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getOriginalUrl()
|
|
{
|
|
return $this->originalUrl;
|
|
}
|
|
|
|
/**
|
|
* UTF-8 aware parse_url() replacement.
|
|
* @param string $url
|
|
* @param int $component
|
|
* @throws \InvalidArgumentException
|
|
* @return array
|
|
*/
|
|
public function parseUrl($url, $component = -1)
|
|
{
|
|
$parts = parse_url(urlencode($url), $component);
|
|
|
|
if ($parts === false) {
|
|
throw new \InvalidArgumentException('Malformed URL: ' . $url);
|
|
}
|
|
|
|
foreach ((array)$parts as $name => $value) {
|
|
$parts[$name] = urldecode($value);
|
|
}
|
|
|
|
return $parts;
|
|
}
|
|
|
|
/**
|
|
* Returns data array with information about the url
|
|
* @return array
|
|
*/
|
|
public function getData()
|
|
{
|
|
return $this->data;
|
|
}
|
|
|
|
public function __toString()
|
|
{
|
|
return $this->getOriginalUrl();
|
|
}
|
|
|
|
} |