From 8e23b983da00dc15d165f4412c4a5a69e06dea9c Mon Sep 17 00:00:00 2001 From: Julien Rosset Date: Tue, 8 Nov 2022 18:24:58 +0100 Subject: [PATCH] Use jrosset/collections 2.* --- composer.json | 2 +- src/UrlInfo/UrlInfo.php | 121 ++++++++++++++++++++++++++++++++-------- 2 files changed, 98 insertions(+), 25 deletions(-) diff --git a/composer.json b/composer.json index e83c04b..6de82b8 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "minimum-stability": "stable", "require": { "php": "^7.4 || ^8.0", - "jrosset/arrayclasses": "^1.0" + "jrosset/collections": "^2.0" }, "autoload": { "psr-4": { diff --git a/src/UrlInfo/UrlInfo.php b/src/UrlInfo/UrlInfo.php index 9782649..c4841a5 100644 --- a/src/UrlInfo/UrlInfo.php +++ b/src/UrlInfo/UrlInfo.php @@ -2,7 +2,8 @@ namespace jrosset\UrlInfo; -use jrosset\ArrayClasses\ArrayClass; +use jrosset\Collections\Collection; +use jrosset\Collections\ICollection; /** * An utils class about URL parts @@ -33,9 +34,9 @@ class UrlInfo { */ private ?string $path; /** - * @var ArrayClass The URL arguments + * @var ICollection The URL arguments */ - private ArrayClass $arguments; + private ICollection $arguments; /** * @var string|null The URL anchor */ @@ -104,33 +105,41 @@ class UrlInfo { */ public function build (): string { $url = ''; - if ($this->protocol !== null) { - $url .= $this->protocol . '://'; + if ($this->hasProtocol()) { + $url .= $this->getProtocol() . '://'; } - if ($this->user !== null) { - $url .= $this->user; + if ($this->hasUser()) { + $url .= $this->getUser(); } - if ($this->pass !== null) { - $url .= ':' . $this->pass; + if ($this->hasPass()) { + $url .= ':' . $this->getPass(); } - if ($this->host !== null) { - $url .= ($this->user !== null || $this->pass !== null ? '@' : '') . $this->host; + if ($this->hasHost()) { + $url .= ($this->hasUser() || $this->hasPass() ? '@' : '') . $this->getHost(); } - if ($this->port !== null) { - $url .= ':' . $this->port; + if ($this->hasPort()) { + $url .= ':' . $this->getPort(); } - if ($this->path !== null) { - $url .= ($url === '' || mb_substr($this->path, 0, 1) === '/' ? '' : '/') . $this->path; + if ($this->hasPath()) { + $url .= ($url === '' || !$this->isPathAbsolute() ? '' : '/') . $this->getPath(); } - if ($this->arguments->count()) { + if ($this->hasArguments()) { $url .= '?' . $this->getArgumentsAsString(); } - if ($this->anchor !== null) { - $url .= '#' . $this->anchor; + if ($this->hasAnchor()) { + $url .= '#' . $this->getAnchor(); } return $url; } + /** + * Is the URL protocol set ? + * + * @return bool Is the URL protocol set ? + */ + public function hasProtocol (): bool { + return $this->protocol !== null; + } /** * The URL protocol * @@ -151,6 +160,14 @@ class UrlInfo { return $this; } + /** + * Is the URL host set ? + * + * @return bool Is the URL host set ? + */ + public function hasHost (): bool { + return $this->host !== null; + } /** * The URL host * @@ -171,6 +188,14 @@ class UrlInfo { return $this; } + /** + * Is the URL port set ? + * + * @return bool Is the URL port set ? + */ + public function hasPort (): bool { + return $this->port !== null; + } /** * The URL port * @@ -191,6 +216,14 @@ class UrlInfo { return $this; } + /** + * Is the URL username set ? + * + * @return bool Is the URL username set ? + */ + public function hasUser (): bool { + return $this->user !== null; + } /** * The URL username * @@ -211,6 +244,14 @@ class UrlInfo { return $this; } + /** + * Is the URL password set ? + * + * @return bool Is the URL password set ? + */ + public function hasPass (): bool { + return $this->pass !== null; + } /** * The URL password * @@ -231,6 +272,22 @@ class UrlInfo { return $this; } + /** + * Is the URL path set ? + * + * @return bool Is the URL path set ? + */ + public function hasPath (): bool { + return $this->path !== null; + } + /** + * Is the URL path absolute ? + * + * @return bool Is the URL path absolute ? + */ + public function isPathAbsolute (): bool { + return mb_substr($this->getPath(), 0, 1) === '/'; + } /** * The URL path * @@ -251,12 +308,20 @@ class UrlInfo { return $this; } + /** + * Is the URL arguments set ? + * + * @return bool Is the URL arguments set ? + */ + public function hasArguments (): bool { + return count($this->arguments) > 0; + } /** * The URL arguments * - * @return ArrayClass The URL arguments + * @return ICollection The URL arguments */ - public function getArguments (): ArrayClass { + public function getArguments (): ICollection { return $this->arguments; } /** @@ -272,12 +337,12 @@ class UrlInfo { /** * Set the URL arguments * - * @param ArrayClass|null $arguments The new URL arguments + * @param ICollection|null $arguments The new URL arguments * * @return $this */ - public function setArguments (?ArrayClass $arguments = null): self { - $this->arguments = $arguments ?? new ArrayClass(); + public function setArguments (?ICollection $arguments = null): self { + $this->arguments = $arguments ?? new Collection(); return $this; } /** @@ -292,9 +357,17 @@ class UrlInfo { if ($arguments !== null && $arguments !== '') { parse_str($arguments, $argumentsArray); } - return $this->setArguments(new ArrayClass($argumentsArray)); + return $this->setArguments(new Collection($argumentsArray)); } + /** + * Is the URL anchor set ? + * + * @return bool Is the URL anchor set ? + */ + public function hasAnchor (): bool { + return $this->anchor !== null; + } /** * The URL anchor *