69 $parts = parse_url($uri);
70 if ($parts ===
false) {
71 throw new \InvalidArgumentException(
"Unable to parse URI: $uri");
79 return self::composeComponents(
152 return $uri->
getPort() === null
244 if (
$base !== null) {
285 $rel =
new self($rel);
309 $decodedKey = rawurldecode(
$key);
310 $result = array_filter(explode(
'&',
$current),
function ($part) use ($decodedKey) {
311 return rawurldecode(explode(
'=', $part)[0]) !== $decodedKey;
339 $decodedKey = rawurldecode(
$key);
340 $result = array_filter(explode(
'&',
$current),
function ($part) use ($decodedKey) {
341 return rawurldecode(explode(
'=', $part)[0]) !== $decodedKey;
348 $key = strtr($key, self::$replaceQuery);
350 if ($value !== null) {
351 $result[] = $key .
'=' . strtr($value, self::$replaceQuery);
372 $uri->applyParts($parts);
373 $uri->validateState();
386 if ($this->userInfo !==
'') {
390 if ($this->port !== null) {
431 if ($this->scheme ===
$scheme) {
437 $new->removeDefaultPort();
438 $new->validateState();
450 if ($this->userInfo ===
$info) {
455 $new->userInfo =
$info;
456 $new->validateState();
465 if ($this->host ===
$host) {
471 $new->validateState();
480 if ($this->port ===
$port) {
486 $new->removeDefaultPort();
487 $new->validateState();
496 if ($this->path ===
$path) {
502 $new->validateState();
511 if ($this->query ===
$query) {
542 $this->scheme = isset($parts[
'scheme'])
545 $this->userInfo = isset($parts[
'user']) ? $parts[
'user'] :
'';
546 $this->host = isset($parts[
'host'])
549 $this->port = isset($parts[
'port'])
552 $this->path = isset($parts[
'path'])
555 $this->query = isset($parts[
'query'])
558 $this->fragment = isset($parts[
'fragment'])
561 if (isset($parts[
'pass'])) {
562 $this->userInfo .=
':' . $parts[
'pass'];
578 throw new \InvalidArgumentException(
'Scheme must be a string');
593 if (!is_string(
$host)) {
594 throw new \InvalidArgumentException(
'Host must be a string');
597 return strtolower(
$host);
609 if (
$port === null) {
615 throw new \InvalidArgumentException(
616 sprintf(
'Invalid port: %d. Must be between 1 and 65535',
$port)
625 if ($this->port !== null && self::isDefaultPort($this)) {
641 if (!is_string(
$path)) {
642 throw new \InvalidArgumentException(
'Path must be a string');
645 return preg_replace_callback(
646 '/(?:[^' . self::$charUnreserved . self::$charSubDelims .
'%:@\/]++|%(?![A-Fa-f0-9]{2}))/',
647 [$this,
'rawurlencodeMatchZero'],
663 if (!is_string($str)) {
664 throw new \InvalidArgumentException(
'Query and fragment must be a string');
667 return preg_replace_callback(
668 '/(?:[^' . self::$charUnreserved . self::$charSubDelims .
'%:@\/\?]++|%(?![A-Fa-f0-9]{2}))/',
669 [$this,
'rawurlencodeMatchZero'],
676 return rawurlencode($match[0]);
681 if ($this->host ===
'' && ($this->scheme ===
'http' || $this->scheme ===
'https')) {
682 $this->host = self::HTTP_DEFAULT_HOST;
686 if (0 === strpos($this->path,
'//')) {
687 throw new \InvalidArgumentException(
'The path of a URI without an authority must not start with two slashes "//"');
689 if ($this->scheme ===
'' &&
false !== strpos(explode(
'/', $this->path, 2)[0],
':')) {
690 throw new \InvalidArgumentException(
'A relative URI must not have a path beginning with a segment containing a colon');
692 } elseif (isset($this->path[0]) && $this->path[0] !==
'/') {
694 'The path of a URI with an authority must start with a slash "/" or be empty. Automagically fixing the URI ' .
695 'by adding a leading slash to the path is deprecated since version 1.4 and will throw an exception instead.',
withUserInfo($user, $password=null)
Return an instance with the specified user information.
static isSameDocumentReference(UriInterface $uri, UriInterface $base=null)
Whether the URI is a same-document reference.
static removeDotSegments($path)
Removes dot segments from a path and returns the new path.
getUserInfo()
Retrieve the user information component of the URI.
static isAbsolute(UriInterface $uri)
Whether the URI is absolute, i.e.
static removeDotSegments($path)
Removes dot segments from a path and returns the new path.
withQuery($query)
Return an instance with the specified query string.
static static $charUnreserved
withPort($port)
Return an instance with the specified port.
withPath($path)
Return an instance with the specified path.
Value object representing a URI.
static resolve(UriInterface $base, UriInterface $rel)
Converts the relative URI into a new URI that is resolved against the base URI.
withScheme($scheme)
Return an instance with the specified scheme.
withQuery($query)
Return an instance with the specified query string.
withFragment($fragment)
Return an instance with the specified URI fragment.
PSR-7 URI implementation.
getScheme()
Retrieve the scheme component of the URI.
static isNetworkPathReference(UriInterface $uri)
Whether the URI is a network-path reference.
getPort()
Retrieve the port component of the URI.
static isRelativePathReference(UriInterface $uri)
Whether the URI is a relative-path reference.
filterPath($path)
Filters the path of a URI.
static withQueryValue(UriInterface $uri, $key, $value)
Creates a new URI with a specific query string value.
static fromParts(array $parts)
Creates a URI from a hash of parse_url components.
filterQueryAndFragment($str)
Filters the query string or fragment of a URI.
applyParts(array $parts)
Apply parse_url parts to a URI.
getAuthority()
Retrieve the authority component of the URI.
const HTTP_DEFAULT_HOST
Absolute http and https URIs require a host per RFC 7230 Section 2.7 but in generic URIs the host can...
getScheme()
Retrieve the scheme component of the URI.
getPath()
Retrieve the path component of the URI.
static isAbsolutePathReference(UriInterface $uri)
Whether the URI is a absolute-path reference.
getPort()
Retrieve the port component of the URI.
static resolve(UriInterface $base, $rel)
Converts the relative URI into a new URI that is resolved against the base URI.
getQuery()
Retrieve the query string of the URI.
getHost()
Retrieve the host component of the URI.
withHost($host)
Return an instance with the specified host.
static composeComponents($scheme, $authority, $path, $query, $fragment)
Composes a URI reference string from its various components.
rawurlencodeMatchZero(array $match)
getAuthority()
Retrieve the authority component of the URI.
__toString()
Return the string representation as a URI reference.
static withoutQueryValue(UriInterface $uri, $key)
Creates a new URI with a specific query string value removed.
static isDefaultPort(UriInterface $uri)
Whether the URI has the default port of the current scheme.
getQuery()
Retrieve the query string of the URI.
getFragment()
Retrieve the fragment component of the URI.
getPath()
Retrieve the path component of the URI.