Ergo\Http\Url::getUrlForScheme PHP Method

getUrlForScheme() public method

Builds a URL with a different scheme
public getUrlForScheme ( $scheme ) : Url
return Url
    public function getUrlForScheme($scheme)
    {
        $newUrl = clone $this;
        $wasDefaultPort = $newUrl->hasScheme() && $newUrl->isPortDefault();
        $newUrl->_fragments['scheme'] = $scheme;
        if ($wasDefaultPort) {
            $newUrl->_fragments['port'] = $newUrl->getDefaultPort();
        }
        $newUrl->_inputString = $newUrl->__toString();
        return $newUrl;
    }

Usage Example

Example #1
0
 public function testGetUrlForScheme()
 {
     // implicit default port
     $url = new Http\Url('http://example.org');
     $this->assertEquals('https://example.org/', (string) $url->getUrlForScheme('https'));
     // explicit default port
     $url = new Http\Url('http://example.org:80');
     $this->assertEquals('https://example.org/', (string) $url->getUrlForScheme('https'));
     // explicit non standard port
     $url = new Http\Url('http://example.org:123');
     $this->assertEquals('https://example.org:123/', (string) $url->getUrlForScheme('https'));
 }