url = $this->normalizeUrl($url); $this->arrUrl=parse_url($this->url); } /** * Normalize url: removes './', 'a/../b' becomes 'b' * * @param string $path * @param string $separator * @return string normalized path */ function normalizeUrl($path, $separator = '\\/') { // Remove any kind of unicode whitespace $normalized = preg_replace('#\p{C}+|^\./#u', '', $path); // Path remove self referring paths ("/./"). $normalized = preg_replace('#/\.(?=/)|^\./|\./$#', '', $normalized); // Regex for resolving relative paths $regex = '#\/*[^/\.]+/\.\.#Uu'; while (preg_match($regex, $normalized)) { $normalized = preg_replace($regex, '', $normalized); } if (preg_match('#/\.{2}|\.{2}/#', $normalized)) { throw new LogicException('Path is outside of the defined root, path: [' . $path . '], resolved: [' . $normalized . ']'); } return trim($normalized, $separator); } /** gets protocol / scheme part of url * * @return string the protocol / scheme part */ public function getProtocol() { $this->protocol=$this->arrUrl['scheme']; return $this->protocol; } /** * gets host of url * * @return string host */ public function getHost() { $this->host=$this->arrUrl['host']; return $this->host; } /** * gets port of url * * @return integer port */ public function getPort() { $this->port=$this->arrUrl['port']; return $this->port; } /** * gets user of url * * @return string user */ public function getUser() { $this->user = $this->arrUrl['user']; return $this->user; } /** * gets pass of url * * @return string password */ public function getPass() { $this->pass = $this->arrUrl['pass']; return $this->pass; } /** * gets path of url * * @return string path */ public function getPath() { $this->path=$this->arrUrl['path']; return $this->path; } /** * gets query of url * * @return string query */ public function getQuery() { $this->query=$this->arrUrl['query']; return $this->query; } /** * gets fragment of url * * @return string fragment */ public function getFragment() { $this->fragment=$this->arrUrl['fragment']; return $this->fragment; } /** * defines how this class will react when it is cast to a string * * @return string url */ public function __toString() { return $this->url; } } url='http://username:password@hostname:9090/removeMe/.././path?arg=value#anchor'; } public function tearDown(){ } public function testProtocolIsHttp() { // test to ensure that the protocol equals "http" $url=new Url($this->url); $this->assertTrue('http'===$url->getProtocol()); } public function testHostIsHostname() { // test to ensure that the host equals "hostname" $url=new Url($this->url); $this->assertTrue('hostname'===$url->getHost()); } public function testPortIs9090() { // test to ensure that the port equals "9090" $url=new Url($this->url); $this->assertTrue('9090'==$url->getPort()); } public function testUserIsUsername() { // test to ensure that the user equals "username" $url=new Url($this->url); $this->assertTrue('username'===$url->getUser()); } public function testPassIsPassword() { // test to ensure that the password equals "password" $url=new Url($this->url); $this->assertTrue('password'===$url->getPass()); } public function testPathIsPath() { // test to ensure that the path equals "/path" $url=new Url($this->url); $this->assertTrue('/path'===$url->getPath()); } public function testQueryIsArgValue() { // test to ensure that the query equals "arg=value" $url=new Url($this->url); $this->assertTrue('arg=value'===$url->getQuery()); } public function testgetFragmentIsAnchor() { // test to ensure that the fragment equals "anchor" $url=new Url($this->url); $this->assertTrue('anchor'===$url->getFragment()); } public function testToStringIsUrl() { // test to ensure casting the url object equals "http://username:password@hostname:9090/path?arg=value#anchor" $url=new Url($this->url); $this->assertTrue('http://username:password@hostname:9090/path?arg=value#anchor'===(string)$url); } public function testNormalizeUrlIsB() { // test to ensure that 'a/../b' equals "b" after normalisation $url=new Url(''); $this->assertTrue('b'===$url->normalizeUrl('a/../b')); } }