From 8c5a2190dc7e04e64d10dbdfff413184ac3569fa Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Tue, 18 Oct 2022 12:00:58 +0200 Subject: [PATCH] Add UrlInfo --- README.md | 4 +- composer.json | 19 +-- src/UrlInfo/UrlInfo.php | 317 ++++++++++++++++++++++++++++++++++++++++ tests/test.php | 12 ++ 4 files changed, 341 insertions(+), 11 deletions(-) create mode 100644 src/UrlInfo/UrlInfo.php create mode 100644 tests/test.php diff --git a/README.md b/README.md index 904cea3..3f16606 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,3 @@ -# XXX +# PhpUrlInfo -XXX \ No newline at end of file +An utils class about URL parts \ No newline at end of file diff --git a/composer.json b/composer.json index 982738d..e83c04b 100644 --- a/composer.json +++ b/composer.json @@ -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" } -} \ No newline at end of file +} diff --git a/src/UrlInfo/UrlInfo.php b/src/UrlInfo/UrlInfo.php new file mode 100644 index 0000000..9782649 --- /dev/null +++ b/src/UrlInfo/UrlInfo.php @@ -0,0 +1,317 @@ +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; + } +} \ No newline at end of file diff --git a/tests/test.php b/tests/test.php new file mode 100644 index 0000000..17f805d --- /dev/null +++ b/tests/test.php @@ -0,0 +1,12 @@ +getArguments()['hash'] = 'ABCDEF'; + +var_dump((string)$url);