Add UrlInfo

master 1.0.0
Julien Rosset 3 years ago
parent 04cbbaf376
commit 8c5a2190dc

@ -1,3 +1,3 @@
# XXX
# PhpUrlInfo
XXX
An utils class about URL parts

@ -1,11 +1,12 @@
{
"name": "jrosset/xxx",
"description": "XXX",
"name": "jrosset/urlinfo",
"description": "An utils class about URL parts",
"keywords": [ ],
"minimum-stability": "stable",
"require": {
"php": "^7.4 || ^8.0"
"php": "^7.4 || ^8.0",
"jrosset/arrayclasses": "^1.0"
},
"autoload": {
"psr-4": {
@ -15,7 +16,7 @@
},
"readme": "README.md",
"homepage": "https://git.jrosset.ovh/jrosset/XXX",
"homepage": "https://git.jrosset.ovh/jrosset/PhpUrlInfo",
"license": "CC-BY-4.0",
"authors": [
{
@ -25,9 +26,9 @@
],
"support": {
"email": "jul.rosset@gmail.com",
"issues": "https://git.jrosset.ovh/jrosset/XXX/issues",
"wiki": "https://git.jrosset.ovh/jrosset/XXX/wiki",
"docs": "https://git.jrosset.ovh/jrosset/XXX/wiki",
"source": "https://git.jrosset.ovh/jrosset/XXX"
"issues": "https://git.jrosset.ovh/jrosset/PhpUrlInfo/issues",
"wiki": "https://git.jrosset.ovh/jrosset/PhpUrlInfo/wiki",
"docs": "https://git.jrosset.ovh/jrosset/PhpUrlInfo/wiki",
"source": "https://git.jrosset.ovh/jrosset/PhpUrlInfo"
}
}

@ -0,0 +1,317 @@
<?php
namespace jrosset\UrlInfo;
use jrosset\ArrayClasses\ArrayClass;
/**
* An utils class about URL parts
*/
class UrlInfo {
/**
* @var string|null The URL protocol
*/
private ?string $protocol;
/**
* @var string|null The URL host
*/
private ?string $host;
/**
* @var int|null The URL port
*/
private ?int $port;
/**
* @var string|null The URL username
*/
private ?string $user;
/**
* @var string|null The URL password
*/
private ?string $pass;
/**
* @var string|null The URL path
*/
private ?string $path;
/**
* @var ArrayClass The URL arguments
*/
private ArrayClass $arguments;
/**
* @var string|null The URL anchor
*/
private ?string $anchor;
/**
* Initialize the class
*
* @param string|null $url The initial URL to parse
*/
public function __construct (?string $url = null) {
$this->setProtocol();
$this->setHost();
$this->setPort();
$this->setUser();
$this->setPath();
$this->setArguments();
$this->setAnchor();
if ($url !== null && $url !== '') {
$this->parse($url);
}
}
/**
* Build the URL from its components
*
* @return string The URL
*/
public function __toString (): string {
return $this->build();
}
/**
* Ensure the return value is null if the given is an empty string
*
* @param string|null $value The value to check
*
* @return string|null The value checked
*/
protected static final function ensureNull (?string $value): ?string {
return $value === '' ? null : $value;
}
/**
* Parse a URL to its components
*
* @param string $url The URL to parse
*
* @return void
*/
protected function parse (string $url): void {
$info = parse_url($url);
$this->setProtocol($info['scheme'] ?? null);
$this->setHost($info['host'] ?? null);
$this->setPort($info['port'] ?? null);
$this->setUser($info['user'] ?? null);
$this->setPass($info['pass'] ?? null);
$this->setPath($info['path'] ?? null);
$this->setArgumentsFromString($info['query'] ?? null);
$this->setAnchor($info['fragment'] ?? null);
}
/**
* Build the URL from its components
*
* @return string The URL
*/
public function build (): string {
$url = '';
if ($this->protocol !== null) {
$url .= $this->protocol . '://';
}
if ($this->user !== null) {
$url .= $this->user;
}
if ($this->pass !== null) {
$url .= ':' . $this->pass;
}
if ($this->host !== null) {
$url .= ($this->user !== null || $this->pass !== null ? '@' : '') . $this->host;
}
if ($this->port !== null) {
$url .= ':' . $this->port;
}
if ($this->path !== null) {
$url .= ($url === '' || mb_substr($this->path, 0, 1) === '/' ? '' : '/') . $this->path;
}
if ($this->arguments->count()) {
$url .= '?' . $this->getArgumentsAsString();
}
if ($this->anchor !== null) {
$url .= '#' . $this->anchor;
}
return $url;
}
/**
* The URL protocol
*
* @return string|null The URL protocol
*/
public function getProtocol (): ?string {
return $this->protocol;
}
/**
* Set the URL protocol
*
* @param string|null $protocol The new URL protocol
*
* @return $this
*/
public function setProtocol (?string $protocol = null): self {
$this->protocol = self::ensureNull(mb_strtolower($protocol));
return $this;
}
/**
* The URL host
*
* @return string|null The URL host
*/
public function getHost (): ?string {
return $this->host;
}
/**
* Set the URL host
*
* @param string|null $host The new URL host
*
* @return $this
*/
public function setHost (?string $host = null): self {
$this->host = self::ensureNull(mb_strtolower($host));
return $this;
}
/**
* The URL port
*
* @return int|null The URL port
*/
public function getPort (): ?int {
return $this->port;
}
/**
* Set the URL port
*
* @param int|null $port The new URL port
*
* @return $this
*/
public function setPort (?int $port = null): self {
$this->port = $port;
return $this;
}
/**
* The URL username
*
* @return string|null The URL username
*/
public function getUser (): ?string {
return $this->user;
}
/**
* Set the URL username
*
* @param string|null $user The new URL username
*
* @return $this
*/
public function setUser (?string $user = null): self {
$this->user = self::ensureNull($user);
return $this;
}
/**
* The URL password
*
* @return string|null The URL password
*/
public function getPass (): ?string {
return $this->pass;
}
/**
* Set the URL password
*
* @param string|null $pass The new URL password
*
* @return $this
*/
public function setPass (?string $pass = null): self {
$this->pass = self::ensureNull($pass);
return $this;
}
/**
* The URL path
*
* @return string|null The URL path
*/
public function getPath (): ?string {
return $this->path;
}
/**
* Set the URL path
*
* @param string|null $path The new URL path
*
* @return $this
*/
public function setPath (?string $path = null): self {
$this->path = self::ensureNull($path);
return $this;
}
/**
* The URL arguments
*
* @return ArrayClass The URL arguments
*/
public function getArguments (): ArrayClass {
return $this->arguments;
}
/**
* The URL arguments as a string
*
* @param int $encoding_type The ISO-norm to use for encoding
*
* @return string The URL arguments as a string
*/
public function getArgumentsAsString (int $encoding_type = PHP_QUERY_RFC1738): string {
return http_build_query($this->getArguments()->toArray(), '', null, $encoding_type);
}
/**
* Set the URL arguments
*
* @param ArrayClass|null $arguments The new URL arguments
*
* @return $this
*/
public function setArguments (?ArrayClass $arguments = null): self {
$this->arguments = $arguments ?? new ArrayClass();
return $this;
}
/**
* Set the URL arguments from a string
*
* @param string|null $arguments The new URL arguments string
*
* @return $this
*/
public function setArgumentsFromString (?string $arguments = null): self {
$argumentsArray = [];
if ($arguments !== null && $arguments !== '') {
parse_str($arguments, $argumentsArray);
}
return $this->setArguments(new ArrayClass($argumentsArray));
}
/**
* The URL anchor
*
* @return string|null The URL anchor
*/
public function getAnchor (): ?string {
return $this->anchor;
}
/**
* Set the URL anchor
*
* @param string|null $anchor The new URL anchor
*
* @return $this
*/
public function setAnchor (?string $anchor = null): self {
$this->anchor = self::ensureNull($anchor);
return $this;
}
}

@ -0,0 +1,12 @@
<?php
use jrosset\UrlInfo\UrlInfo;
require_once __DIR__ . '/../vendor/autoload.php';
$url = new UrlInfo('js/basesite/auto.js?');
var_dump($url);
$url->getArguments()['hash'] = 'ABCDEF';
var_dump((string)$url);
Loading…
Cancel
Save