An implementation of PSR-7 URI interface and the WHATWG URL specification
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

125 lines
2.9 KiB

<?php
/** @license MIT
* Copyright 2020 J. King et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace MensBeam\Url;
use Psr\Http\Message\UriInterface;
/** Normalized URI representation, compatible with the PSR-7 URI interface
*
* The following features are implemented:
*
* - The full PSR-7 `UriInterface` interface
* - Correct handling of both URLs and URNs
* - Relative URL resolution
* - Encoding normalization
* - Scheme normalization
* - IDNA normalization
* - IPv6 address normalization
* - Empty query and fragment removal
*
* Some things this class does not do:
*
* - Handle non-standard schemes (e.g. ed2k)
* - Collapse paths
* - Drop default ports
*
* This class should not be used with XML namespace URIs,
* as the normalizations performed will change the values
* of some namespaces.
*/
class Uri extends AbstractUri implements UriInterface {
public function getAuthority() {
$host = $this->getHost();
if (strlen($host) > 0) {
$userInfo = $this->getUserInfo();
$port = $this->getPort();
return (strlen($userInfo) ? $userInfo."@" : "").$host.(!is_null($port) ? ":".$port : "");
}
return "";
}
public function getFragment() {
return $this->fragment ?? "";
}
public function getHost() {
return $this->host ?? "";
}
public function getPath() {
return $this->path ?? "";
}
public function getPort() {
return $this->port;
}
public function getQuery() {
return $this->query ?? "";
}
public function getScheme() {
return $this->scheme ?? "";
}
public function getUserInfo() {
if (strlen($this->user ?? "")) {
return $this->user.(strlen($this->pass ?? "") ? ":".$this->pass : "");
}
return "";
}
public function withFragment($fragment) {
$out = clone $this;
$out->set("fragment", $fragment);
return $out;
}
public function withHost($host) {
if ($host === "") {
$host = null;
}
$out = clone $this;
$out->set("host", $host);
return $out;
}
public function withPath($path) {
$out = clone $this;
$out->set("path", $path);
return $out;
}
public function withPort($port) {
$out = clone $this;
$out->set("port", $port);
return $out;
}
public function withQuery($query) {
$out = clone $this;
$out->set("query", $query);
return $out;
}
public function withScheme($scheme) {
$out = clone $this;
$out->set("scheme", $scheme);
return $out;
}
public function withUserInfo($user, $password = null) {
$out = clone $this;
$out->set("user", $user);
$out->set("pass", $password);
return $out;
}
public function __get(string $name) {
return $this->$name;
}
}