File size: 2.75Kb
<?php
/**
This software is furnished under a license and may be used and copied
only in accordance with the terms of such license and with the
inclusion of the above copyright notice. This software or any other
copies thereof may not be provided or otherwise made available to any
other person. No title to and ownership of the software is hereby
transferred.
You may not reverse engineer, decompile, defeat license encryption
mechanisms, or disassemble this software product or software product
license. AddonsLab may terminate this license if you don't comply with
any of these terms and conditions. In such event, licensee agrees
to return licensor or destroy all copies of software upon termination
of the license.
*/
namespace AddonsLab\Tools;
class UrlModifier
{
protected $url;
public function __construct($url)
{
$this->url = $url;
}
public function addArgument($argumentName, $argumentValue)
{
$url = $this->url;
$query = parse_url($url, PHP_URL_QUERY);
if (!$query) {
$queryArray = array();
} else {
$queryArray = $this->parse_str($query);
}
$queryArray[$argumentName] = $argumentValue;
if (!$query) {
return $url .= '?' . http_build_query($queryArray);
}
return str_replace('?' . $query, '?' . $this->http_build_query($queryArray), $url);
}
public function removeArgument($argumentName)
{
$url = $this->url;
$query = parse_url($url, PHP_URL_QUERY);
if (!$query) {
$queryArray = array();
} else {
parse_str($query, $queryArray);
}
unset($queryArray[$argumentName]);
return str_replace('?' . $query, '?' . $this->http_build_query($queryArray), $url);
}
public function parse_str($queryString)
{
$args = array();
$queryString = explode('&', $queryString);
foreach ($queryString AS $item) {
if (!$item) {
continue;
}
$item = explode('=', $item);
if (empty($item[1])) {
$item[1] = '';
}
$args[$item[0]] = urldecode($item[1]);
}
return $args;
}
public function http_build_query($args)
{
$query = array();
foreach ($args AS $argName => $argValue) {
if (
(is_array($argValue) && empty($argValue))
|| (is_scalar($argValue) && strlen($argValue) === 0)
) {
unset($args[$argName]);
}
}
return http_build_query($args);
}
}