Example #1
0
echo $uri->setAuth('j2'), PHP_EOL;
echo $uri->setPort(80), PHP_EOL;
echo $uri->setPort(7070), PHP_EOL;
echo $uri->setHost('foobar.baz'), PHP_EOL;
echo $uri->setPath('/wow'), PHP_EOL;
echo $uri->setQueryParameter('ran', 't'), PHP_EOL;
$uri2 = new Uri('/foo/bar');
var_dump('protocol=' . $uri2->getScheme());
var_dump('username='******'password='******'host=' . $uri2->getHost());
var_dump('port=' . $uri2->getPort());
var_dump('path=' . $uri2->getPath());
var_dump('query=' . $uri2->getQuery());
var_dump('query params=', $uri2->getQueryParameters());
var_dump('fragment=' . $uri2->getFragment());
$tryCatch = function (string $func, ...$params) use(&$uri2) {
    try {
        $uri2->{$func}(...$params);
    } catch (UriException $e) {
        echo $e->getMessage(), PHP_EOL;
    }
};
$tryCatch('getAbsolute');
// this is a relative URL, no absolute parts are defined, so this will throw an exception
$tryCatch('setHost', '');
// can't have an empty host; if you want to get the relative part of a URL, use getRelative()
$tryCatch('setHost', 'domain/tld');
// slashes in host not allowed (leading/trailing slashes are ok, they're trimmed)
$tryCatch('setAuth', '', 'nonemptypassword');
// can't have password without username