Use jrosset/collections 2.*

master 1.1.0
Julien Rosset 3 years ago
parent 8c5a2190dc
commit 8e23b983da

@ -6,7 +6,7 @@
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": "^7.4 || ^8.0", "php": "^7.4 || ^8.0",
"jrosset/arrayclasses": "^1.0" "jrosset/collections": "^2.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

@ -2,7 +2,8 @@
namespace jrosset\UrlInfo; namespace jrosset\UrlInfo;
use jrosset\ArrayClasses\ArrayClass; use jrosset\Collections\Collection;
use jrosset\Collections\ICollection;
/** /**
* An utils class about URL parts * An utils class about URL parts
@ -33,9 +34,9 @@ class UrlInfo {
*/ */
private ?string $path; 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 * @var string|null The URL anchor
*/ */
@ -104,33 +105,41 @@ class UrlInfo {
*/ */
public function build (): string { public function build (): string {
$url = ''; $url = '';
if ($this->protocol !== null) { if ($this->hasProtocol()) {
$url .= $this->protocol . '://'; $url .= $this->getProtocol() . '://';
} }
if ($this->user !== null) { if ($this->hasUser()) {
$url .= $this->user; $url .= $this->getUser();
} }
if ($this->pass !== null) { if ($this->hasPass()) {
$url .= ':' . $this->pass; $url .= ':' . $this->getPass();
} }
if ($this->host !== null) { if ($this->hasHost()) {
$url .= ($this->user !== null || $this->pass !== null ? '@' : '') . $this->host; $url .= ($this->hasUser() || $this->hasPass() ? '@' : '') . $this->getHost();
} }
if ($this->port !== null) { if ($this->hasPort()) {
$url .= ':' . $this->port; $url .= ':' . $this->getPort();
} }
if ($this->path !== null) { if ($this->hasPath()) {
$url .= ($url === '' || mb_substr($this->path, 0, 1) === '/' ? '' : '/') . $this->path; $url .= ($url === '' || !$this->isPathAbsolute() ? '' : '/') . $this->getPath();
} }
if ($this->arguments->count()) { if ($this->hasArguments()) {
$url .= '?' . $this->getArgumentsAsString(); $url .= '?' . $this->getArgumentsAsString();
} }
if ($this->anchor !== null) { if ($this->hasAnchor()) {
$url .= '#' . $this->anchor; $url .= '#' . $this->getAnchor();
} }
return $url; 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 * The URL protocol
* *
@ -151,6 +160,14 @@ class UrlInfo {
return $this; 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 * The URL host
* *
@ -171,6 +188,14 @@ class UrlInfo {
return $this; 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 * The URL port
* *
@ -191,6 +216,14 @@ class UrlInfo {
return $this; 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 * The URL username
* *
@ -211,6 +244,14 @@ class UrlInfo {
return $this; 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 * The URL password
* *
@ -231,6 +272,22 @@ class UrlInfo {
return $this; 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 * The URL path
* *
@ -251,12 +308,20 @@ class UrlInfo {
return $this; 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 * The URL arguments
* *
* @return ArrayClass The URL arguments * @return ICollection The URL arguments
*/ */
public function getArguments (): ArrayClass { public function getArguments (): ICollection {
return $this->arguments; return $this->arguments;
} }
/** /**
@ -272,12 +337,12 @@ class UrlInfo {
/** /**
* Set the URL arguments * Set the URL arguments
* *
* @param ArrayClass|null $arguments The new URL arguments * @param ICollection|null $arguments The new URL arguments
* *
* @return $this * @return $this
*/ */
public function setArguments (?ArrayClass $arguments = null): self { public function setArguments (?ICollection $arguments = null): self {
$this->arguments = $arguments ?? new ArrayClass(); $this->arguments = $arguments ?? new Collection();
return $this; return $this;
} }
/** /**
@ -292,9 +357,17 @@ class UrlInfo {
if ($arguments !== null && $arguments !== '') { if ($arguments !== null && $arguments !== '') {
parse_str($arguments, $argumentsArray); 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 * The URL anchor
* *

Loading…
Cancel
Save