Utilities for parsing, normalizing, merging, and building URLs with fine‑grained control over query strings.
composer require dealnews/urlHigh level URL helper that stores individual URL parts (scheme, host, path, query, etc.) and keeps a QueryString helper in sync with the query property.
Key capabilities:
parse(string $url, bool $reset = true, ?string $separator = null)– split a URL into parts and validate each component.- Magic setters/getters (
$url->host,$url->path,$url->query, …) – allow direct manipulation of parts with validation. build(?string $separator = null)– rebuild the URL, omitting default ports and honoring custom separators.normalize()– lowercases scheme/host, sorts query parameters, and normalizes percent‑encoding.sanitizePathComponent(string $input)– generate safe slug/path fragments.merge(string $url1, string ...$url2)– merge one or more URLs into the current instance without mutating the original data.
Example:
use DealNews\Url\Url;
$url = new Url('https://user:[email protected]:443/products/tv?q=4k#details');
$url->normalize(); // lowercases scheme/host and sorts query parameters
$url->query_string->setParameter('page', '2');
$url->fragment = 'reviews';
echo $url->build(); // https://user:[email protected]/products/tv?q=4k&page=2#reviews
// Merge additional data without losing the existing state
$combined = $url->merge('http://cdn.example.com/media', '?size=large');
// $combined => http://cdn.example.com/media?size=large
// $url remains unchangedLightweight parser/builder for ordered query strings. Allows unnamed entries, custom separators, front insertion, and selective replacement.
Key methods:
parse(string $query_string, string $separator = '&')build(?string $separator = null): stringaddParameter(?string $key, ?string $value, bool $front = false)setParameter(?string $key, ?string $value, bool $front = false)removeParameters(array $keys): voidsortParameters(): voidgetParameters(): array
Example:
use DealNews\Url\QueryString;
$qs = new QueryString('foo=1&bar=2');
$qs->setParameter('foo', '3');
$qs->addParameter(null, 'feature'); // unnamed flag
$qs->sortParameters(); // only sorts when all params are named
echo $qs->build(); // foo=3&bar=2&featureCustom separator example:
$qs = new QueryString(null, ';');
$qs->parse('token;user=john;role=admin', ';');
$qs->addParameter('expires', '1700000000');
echo $qs->build(); // token;user=john;role=admin;expires=1700000000Custom exception extending \Exception. Methods such as Url::__construct() and Url::__set() throw Parse when a URL cannot be parsed or an invalid property is referenced.
use DealNews\Url\Exception\Parse;
use DealNews\Url\Url;
try {
$url = new Url();
$url->invalid_property = 'value';
} catch (Parse $e) {
// Handle invalid assignment
}composer install
./vendor/bin/phpunit