Last active
January 28, 2022 08:14
-
-
Save jesseschalken/44bab8ad767ebc4cc44d3259833944b1 to your computer and use it in GitHub Desktop.
Simple implementation of \Psr\Http\Message\UriInterface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Uri implements \Psr\Http\Message\UriInterface { | |
const PASS = 'pass'; | |
const USER = 'user'; | |
const PORT = 'port'; | |
const SCHEME = 'scheme'; | |
const QUERY = 'query'; | |
const FRAGMENT = 'fragment'; | |
const HOST = 'host'; | |
const PATH = 'path'; | |
/** @var array */ | |
private $parsed = array( | |
self::PASS => null, | |
self::USER => '', | |
self::PORT => null, | |
self::SCHEME => '', | |
self::QUERY => '', | |
self::FRAGMENT => '', | |
self::HOST => '', | |
self::PATH => '', | |
); | |
/** | |
* @param string $url | |
*/ | |
public function __construct($url = '') { | |
$this->parsed = array_replace($this->parsed, parse_url($url)); | |
} | |
public function getScheme() { | |
return $this->get(self::SCHEME); | |
} | |
public function getAuthority() { | |
$user = $this->getUserInfo(); | |
$port = $this->getPort(); | |
return ( | |
(strlen($user) ? "$user@" : '') . | |
$this->getHost() . | |
($port !== null ? ":$port" : '') | |
); | |
} | |
public function getUserInfo() { | |
$pass = $this->get(self::PASS); | |
$user = $this->get(self::USER); | |
return $pass !== null ? "$user:$pass" : "$user"; | |
} | |
public function getHost() { | |
return $this->get(self::HOST); | |
} | |
public function getPort() { | |
return $this->get(self::PORT); | |
} | |
public function getPath() { | |
return $this->get(self::PATH); | |
} | |
public function getQuery() { | |
return $this->get(self::QUERY); | |
} | |
public function getFragment() { | |
return $this->get(self::FRAGMENT); | |
} | |
public function withScheme($scheme) { | |
return $this->with(array(self::SCHEME => "$scheme")); | |
} | |
public function withUserInfo($user, $password = null) { | |
return $this->with(array(self::USER => "$user", self::PASS => $password === null ? null : "$password")); | |
} | |
public function withHost($host) { | |
return $this->with(array(self::HOST => "$host")); | |
} | |
public function withPort($port) { | |
return $this->with(array(self::PORT => $port === null ? null : (int)$port)); | |
} | |
public function withPath($path) { | |
return $this->with(array(self::PATH => "$path")); | |
} | |
public function withQuery($query) { | |
return $this->with(array(self::QUERY => "$query")); | |
} | |
public function withFragment($fragment) { | |
return $this->with(array(self::FRAGMENT => "$fragment")); | |
} | |
public function __toString() { | |
$authority = $this->getAuthority(); | |
$scheme = $this->getScheme(); | |
$query = $this->getQuery(); | |
$fragment = $this->getFragment(); | |
$path = $this->getPath(); | |
if (strlen($authority)) { | |
$path = '/' . ltrim($path, '/'); | |
} else if ($path && $path[0] !== '/') { | |
$path = '/' . $path; | |
} | |
return ( | |
(strlen($scheme) ? "$scheme:" : '') . | |
(strlen($authority) ? "//$authority" : '') . | |
$path . | |
(strlen($query) ? "?$query" : '') . | |
(strlen($fragment) ? "#$fragment" : '') | |
); | |
} | |
private function with(array $changes) { | |
$self = clone $this; | |
$self->parsed = array_replace($self->parsed, $changes); | |
return $self; | |
} | |
private function get($part) { | |
return $this->parsed[$part]; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$password === null ? null : $password ))