2013-06-27 12:45:18 +02:00
|
|
|
<?php
|
2013-10-23 15:10:33 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-27 12:45:18 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web;
|
|
|
|
|
|
|
|
use Icinga\Application\Icinga;
|
2014-09-05 11:40:43 +02:00
|
|
|
use Icinga\Cli\FakeRequest;
|
2014-04-25 14:25:13 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2014-06-17 12:07:19 +02:00
|
|
|
use Icinga\Web\UrlParams;
|
2013-06-27 12:45:18 +02:00
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
2014-04-25 14:25:13 +02:00
|
|
|
* Url class that provides convenient access to parameters, allows to modify query parameters and
|
|
|
|
* returns Urls reflecting all changes made to the url and to the parameters.
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* Direct instantiation is prohibited and should be done either with @see Url::fromRequest() or
|
|
|
|
* @see Url::fromUrlString()
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* Currently, protocol, host and port are ignored and will be implemented when required
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2013-06-27 12:45:18 +02:00
|
|
|
class Url
|
|
|
|
{
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* An array of all parameters stored in this Url
|
|
|
|
*
|
2014-07-16 15:16:05 +02:00
|
|
|
* @var UrlParams
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2014-06-17 12:07:19 +02:00
|
|
|
protected $params;
|
2013-06-27 12:45:18 +02:00
|
|
|
|
2013-10-20 16:28:53 +02:00
|
|
|
/**
|
|
|
|
* The site anchor after the '#'
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-04-25 14:25:13 +02:00
|
|
|
protected $anchor = '';
|
2013-10-20 16:28:53 +02:00
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* The relative path of this Url, without query parameters
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-04-25 14:25:13 +02:00
|
|
|
protected $path = '';
|
2013-07-31 10:59:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The baseUrl that will be appended to @see Url::$path in order to
|
|
|
|
* create an absolute Url
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-04-25 14:25:13 +02:00
|
|
|
protected $baseUrl = '/';
|
2013-07-31 10:59:34 +02:00
|
|
|
|
2014-04-25 14:25:13 +02:00
|
|
|
protected function __construct()
|
2013-07-31 10:59:34 +02:00
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
$this->params = UrlParams::fromQueryString(''); // TODO: ::create()
|
2013-07-31 10:59:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Url class representing the current request
|
|
|
|
*
|
|
|
|
* If $params are given, those will be added to the request's parameters
|
|
|
|
* and overwrite any existing parameters
|
|
|
|
*
|
2014-06-17 12:07:19 +02:00
|
|
|
* @param UrlParams|array $params Parameters that should additionally be considered for the url
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param Zend_Request $request A request to use instead of the default one
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return Url
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2014-06-17 12:07:19 +02:00
|
|
|
public static function fromRequest($params = array(), $request = null)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2013-07-12 12:06:30 +02:00
|
|
|
if ($request === null) {
|
2013-08-08 16:22:22 +02:00
|
|
|
$request = self::getRequest();
|
2013-07-12 12:06:30 +02:00
|
|
|
}
|
2013-07-31 10:59:34 +02:00
|
|
|
|
2014-06-17 12:07:19 +02:00
|
|
|
$url = new Url();
|
|
|
|
$url->setPath($request->getPathInfo());
|
|
|
|
|
|
|
|
// $urlParams = UrlParams::fromQueryString($request->getQuery());
|
|
|
|
if (isset($_SERVER['QUERY_STRING'])) {
|
|
|
|
$urlParams = UrlParams::fromQueryString($_SERVER['QUERY_STRING']);
|
|
|
|
} else {
|
|
|
|
$urlParams = UrlParams::fromQueryString('');
|
|
|
|
foreach ($request->getQuery() as $k => $v) {
|
|
|
|
$urlParams->set($k, $v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($params as $k => $v) {
|
|
|
|
$urlParams->set($k, $v);
|
|
|
|
}
|
|
|
|
$url->setParams($urlParams);
|
|
|
|
$url->setBaseUrl($request->getBaseUrl());
|
|
|
|
return $url;
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-08-08 16:22:22 +02:00
|
|
|
/**
|
|
|
|
* Return a request object that should be used for determining the URL
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return Zend_Abstract_Request
|
2013-08-08 16:22:22 +02:00
|
|
|
*/
|
2014-04-25 14:25:13 +02:00
|
|
|
protected static function getRequest()
|
2013-08-08 16:22:22 +02:00
|
|
|
{
|
2014-09-05 11:40:43 +02:00
|
|
|
$app = Icinga::app();
|
|
|
|
if ($app->isCli()) {
|
|
|
|
return new FakeRequest();
|
|
|
|
} else {
|
|
|
|
return $app->getFrontController()->getRequest();
|
|
|
|
}
|
2013-08-08 16:22:22 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Create a new Url class representing the given url
|
|
|
|
*
|
|
|
|
* If $params are given, those will be added to the urls parameters
|
|
|
|
* and overwrite any existing parameters
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param string $url The string representation of the url to parse
|
|
|
|
* @param array $params An array of parameters that should additionally be considered for the url
|
|
|
|
* @param Zend_Request $request A request to use instead of the default one
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return Url
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
|
|
|
public static function fromPath($url, array $params = array(), $request = null)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2013-07-31 10:59:34 +02:00
|
|
|
if ($request === null) {
|
2013-08-08 16:22:22 +02:00
|
|
|
$request = self::getRequest();
|
2013-07-31 10:59:34 +02:00
|
|
|
}
|
2014-04-25 14:25:13 +02:00
|
|
|
|
|
|
|
if (!is_string($url)) {
|
2014-08-26 11:15:19 +02:00
|
|
|
throw new ProgrammingError(
|
|
|
|
'url "%s" is not a string',
|
|
|
|
$url
|
|
|
|
);
|
2014-04-25 14:25:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$urlObject = new Url();
|
2014-04-29 14:27:19 +02:00
|
|
|
$baseUrl = $request->getBaseUrl();
|
|
|
|
$urlObject->setBaseUrl($baseUrl);
|
2013-07-31 10:59:34 +02:00
|
|
|
|
|
|
|
$urlParts = parse_url($url);
|
2014-04-29 14:27:19 +02:00
|
|
|
if (isset($urlParts['path'])) {
|
|
|
|
if ($baseUrl !== '' && strpos($urlParts['path'], $baseUrl) === 0) {
|
|
|
|
$urlObject->setPath(substr($urlParts['path'], strlen($baseUrl)));
|
2014-04-25 14:25:13 +02:00
|
|
|
} else {
|
2014-04-29 14:27:19 +02:00
|
|
|
$urlObject->setPath($urlParts['path']);
|
2014-04-25 14:25:13 +02:00
|
|
|
}
|
2013-07-31 10:59:34 +02:00
|
|
|
}
|
2014-06-17 12:07:19 +02:00
|
|
|
// TODO: This has been used by former filter implementation, remove it:
|
2014-04-29 14:27:19 +02:00
|
|
|
if (isset($urlParts['query'])) {
|
2014-06-20 15:50:55 +02:00
|
|
|
$params = UrlParams::fromQueryString($urlParts['query'])->mergeValues($params);
|
2013-07-31 10:59:34 +02:00
|
|
|
}
|
2014-04-25 14:25:13 +02:00
|
|
|
|
2014-08-19 09:36:15 +02:00
|
|
|
if (isset($urlParts['fragment'])) {
|
|
|
|
$urlObject->setAnchor($urlParts['fragment']);
|
2013-10-20 16:28:53 +02:00
|
|
|
}
|
2013-07-31 10:59:34 +02:00
|
|
|
|
|
|
|
$urlObject->setParams($params);
|
|
|
|
return $urlObject;
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
2014-04-25 14:25:13 +02:00
|
|
|
* Overwrite the baseUrl
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
|
|
|
* If an empty Url is given '/' is used as the base
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param string $baseUrl The url path to use as the Url Base
|
|
|
|
*
|
|
|
|
* @return self
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2013-06-27 12:45:18 +02:00
|
|
|
public function setBaseUrl($baseUrl)
|
|
|
|
{
|
2014-04-25 14:25:13 +02:00
|
|
|
if (($baseUrl = rtrim($baseUrl, '/ ')) === '') {
|
2013-07-31 10:59:34 +02:00
|
|
|
$baseUrl = '/';
|
|
|
|
}
|
2014-04-25 14:25:13 +02:00
|
|
|
|
2013-06-27 12:45:18 +02:00
|
|
|
$this->baseUrl = $baseUrl;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
/**
|
2014-04-25 14:25:13 +02:00
|
|
|
* Return the baseUrl set for this url
|
2013-08-06 11:53:42 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return string
|
2013-08-06 11:53:42 +02:00
|
|
|
*/
|
|
|
|
public function getBaseUrl()
|
|
|
|
{
|
|
|
|
return $this->baseUrl;
|
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Set the relative path of this url, without query parameters
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param string $path The path to set
|
|
|
|
*
|
|
|
|
* @return self
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
|
|
|
public function setPath($path)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-04-25 14:25:13 +02:00
|
|
|
$this->path = ltrim($path, '/');
|
|
|
|
return $this;
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
2014-04-25 14:25:13 +02:00
|
|
|
* Return the relative path of this url, without query parameters
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
|
|
|
* If you want the relative path with query parameters use getRelativeUrl
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return string
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2013-07-12 12:06:30 +02:00
|
|
|
public function getPath()
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2013-07-12 12:06:30 +02:00
|
|
|
return $this->path;
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Return the relative url with query parameters as a string
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return string
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2014-08-19 09:42:16 +02:00
|
|
|
public function getRelativeUrl($separator = '&')
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
if ($this->params->isEmpty()) {
|
2014-04-25 14:25:13 +02:00
|
|
|
return $this->path . $this->anchor;
|
2014-06-17 12:07:19 +02:00
|
|
|
} else {
|
2014-08-19 09:42:16 +02:00
|
|
|
return $this->path . '?' . $this->params->toString($separator) . $this->anchor;
|
2013-08-01 15:54:38 +02:00
|
|
|
}
|
2013-07-31 10:59:34 +02:00
|
|
|
}
|
|
|
|
|
2014-06-20 12:41:16 +02:00
|
|
|
public function setQueryString($queryString)
|
|
|
|
{
|
|
|
|
$this->params = UrlParams::fromQueryString($queryString);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-06-05 05:44:12 +02:00
|
|
|
public function getQueryString()
|
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
return (string) $this->params;
|
2014-06-05 05:44:12 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Return the absolute url with query parameters as a string
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return string
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2014-08-19 09:42:16 +02:00
|
|
|
public function getAbsoluteUrl($separator = '&')
|
2013-07-31 10:59:34 +02:00
|
|
|
{
|
2014-08-19 09:42:16 +02:00
|
|
|
return $this->baseUrl . ($this->baseUrl !== '/' ? '/' : '') . $this->getRelativeUrl($separator);
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
2013-08-01 15:54:38 +02:00
|
|
|
* Add a set of parameters to the query part if the keys don't exist yet
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param array $params The parameters to add
|
|
|
|
*
|
|
|
|
* @return self
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
|
|
|
public function addParams(array $params)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
foreach ($params as $k => $v) {
|
|
|
|
$this->params->add($k, $v);
|
|
|
|
}
|
|
|
|
|
2013-06-27 12:45:18 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-14 16:00:19 +02:00
|
|
|
/**
|
|
|
|
* Set and overwrite the given params if one if the same key already exists
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param array $params The parameters to set
|
|
|
|
*
|
|
|
|
* @return self
|
2013-08-14 16:00:19 +02:00
|
|
|
*/
|
|
|
|
public function overwriteParams(array $params)
|
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
foreach ($params as $k => $v) {
|
|
|
|
$this->params->set($k, $v);
|
|
|
|
}
|
|
|
|
|
2013-08-14 16:00:19 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Overwrite the parameters used in the query part
|
|
|
|
*
|
2014-06-17 12:07:19 +02:00
|
|
|
* @param UrlParams|array $params The new parameters to use for the query part
|
2014-04-25 14:25:13 +02:00
|
|
|
*
|
|
|
|
* @return self
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2014-06-17 12:07:19 +02:00
|
|
|
public function setParams($params)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
if ($params instanceof UrlParams) {
|
|
|
|
$this->params = $params;
|
|
|
|
} elseif (is_array($params)) {
|
|
|
|
$urlParams = UrlParams::fromQueryString('');
|
|
|
|
foreach ($params as $k => $v) {
|
|
|
|
$urlParams->set($k, $v);
|
|
|
|
}
|
|
|
|
$this->params = $urlParams;
|
|
|
|
} else {
|
2014-06-20 15:50:55 +02:00
|
|
|
throw new ProgrammingError(
|
|
|
|
'Url params needs to be either an array or an UrlParams instance'
|
|
|
|
);
|
2014-06-17 12:07:19 +02:00
|
|
|
}
|
2013-06-27 12:45:18 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Return all parameters that will be used in the query part
|
|
|
|
*
|
2014-07-21 09:19:44 +02:00
|
|
|
* @return UrlParams An instance of UrlParam containing all parameters
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2013-07-12 12:06:30 +02:00
|
|
|
public function getParams()
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-07-21 09:19:44 +02:00
|
|
|
return $this->params;
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
2014-04-25 14:25:13 +02:00
|
|
|
* Return true if a urls' query parameter exists, otherwise false
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param string $param The url parameter name to check
|
|
|
|
*
|
|
|
|
* @return bool
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2014-04-25 14:25:13 +02:00
|
|
|
public function hasParam($param)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
return $this->params->has($param);
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
2014-04-25 14:25:13 +02:00
|
|
|
* Return a url's query parameter if it exists, otherwise $default
|
|
|
|
*
|
|
|
|
* @param string $param A query parameter name to return if existing
|
|
|
|
* @param mixed $default A value to return when the parameter doesn't exist
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return mixed
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2014-04-25 14:25:13 +02:00
|
|
|
public function getParam($param, $default = null)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
return $this->params->get($param, $default);
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
2014-04-25 14:25:13 +02:00
|
|
|
* Set a single parameter, overwriting any existing one with the same name
|
2013-07-31 10:59:34 +02:00
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param string $param The query parameter name
|
|
|
|
* @param array|string $value An array or string to set as the parameter value
|
|
|
|
*
|
|
|
|
* @return self
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2014-06-22 16:18:23 +02:00
|
|
|
public function setParam($param, $value = true)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
$this->params->set($param, $value);
|
2013-07-12 12:06:30 +02:00
|
|
|
return $this;
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
|
2013-10-20 16:28:53 +02:00
|
|
|
/**
|
|
|
|
* Set the url anchor-part
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param string $anchor The site's anchor string without the '#'
|
2014-02-11 15:27:42 +01:00
|
|
|
*
|
|
|
|
* @return self
|
2013-10-20 16:28:53 +02:00
|
|
|
*/
|
|
|
|
public function setAnchor($anchor)
|
|
|
|
{
|
|
|
|
$this->anchor = '#' . $anchor;
|
2014-02-11 15:27:42 +01:00
|
|
|
return $this;
|
2013-10-20 16:28:53 +02:00
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Remove provided key (if string) or keys (if array of string) from the query parameter array
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param string|array $keyOrArrayOfKeys An array of strings or a string representing the key(s)
|
2013-07-31 10:59:34 +02:00
|
|
|
* of the parameters to be removed
|
2014-04-25 14:25:13 +02:00
|
|
|
* @return self
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
|
|
|
public function remove($keyOrArrayOfKeys)
|
2013-06-27 12:45:18 +02:00
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
$this->params->remove($keyOrArrayOfKeys);
|
2013-07-31 10:59:34 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-05-20 15:49:35 +02:00
|
|
|
/**
|
|
|
|
* Shift a query parameter from this URL if it exists, otherwise $default
|
|
|
|
*
|
|
|
|
* @param string $param Parameter name
|
|
|
|
* @param mixed $default Default value in case $param does not exist
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function shift($param, $default = null)
|
|
|
|
{
|
2014-06-17 12:07:19 +02:00
|
|
|
return $this->params->shift($param, $default);
|
2014-05-20 15:49:35 +02:00
|
|
|
}
|
|
|
|
|
2014-09-04 18:46:58 +02:00
|
|
|
/**
|
|
|
|
* Whether the given URL matches this URL object
|
|
|
|
*
|
|
|
|
* This does an exact match, parameters MUST be in the same order
|
|
|
|
*
|
|
|
|
* @param Url|string $url the URL to compare against
|
|
|
|
*
|
|
|
|
* @return bool whether the URL matches
|
|
|
|
*/
|
|
|
|
public function matches($url)
|
|
|
|
{
|
|
|
|
if (! $url instanceof Url) {
|
|
|
|
$url = Url::fromPath($url);
|
|
|
|
}
|
|
|
|
return (string) $url === (string) $this;
|
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Return a copy of this url without the parameter given
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* The argument can be either a single query parameter name or an array of parameter names to
|
2013-07-31 10:59:34 +02:00
|
|
|
* remove from the query list
|
|
|
|
*
|
2014-04-25 14:25:13 +02:00
|
|
|
* @param string|array $keyOrArrayOfKeys A single string or an array containing parameter names
|
|
|
|
*
|
|
|
|
* @return Url
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
|
|
|
public function getUrlWithout($keyOrArrayOfKeys)
|
2014-06-05 05:44:12 +02:00
|
|
|
{
|
|
|
|
return $this->without($keyOrArrayOfKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function without($keyOrArrayOfKeys)
|
2013-07-31 10:59:34 +02:00
|
|
|
{
|
|
|
|
$url = clone($this);
|
|
|
|
$url->remove($keyOrArrayOfKeys);
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2014-09-05 11:40:43 +02:00
|
|
|
/**
|
|
|
|
* Return a copy of this url with the given parameter(s)
|
|
|
|
*
|
|
|
|
* The argument can be either a single query parameter name or an array of parameter names to
|
|
|
|
* remove from the query list
|
|
|
|
*
|
|
|
|
* @param string|array $param A single string or an array containing parameter names
|
|
|
|
* @param array $values an optional values array
|
|
|
|
*
|
|
|
|
* @return Url
|
|
|
|
*/
|
2014-09-05 11:21:10 +02:00
|
|
|
public function with($param, $values = null)
|
|
|
|
{
|
|
|
|
$url = clone($this);
|
|
|
|
$url->params->mergeValues($param, $values);
|
|
|
|
return $url;
|
|
|
|
}
|
|
|
|
|
2014-06-17 12:07:19 +02:00
|
|
|
public function __clone()
|
|
|
|
{
|
|
|
|
$this->params = clone $this->params;
|
|
|
|
}
|
|
|
|
|
2013-07-31 10:59:34 +02:00
|
|
|
/**
|
|
|
|
* Alias for @see Url::getAbsoluteUrl()
|
2014-04-25 14:25:13 +02:00
|
|
|
*
|
|
|
|
* @return string
|
2013-07-31 10:59:34 +02:00
|
|
|
*/
|
2013-08-14 16:00:19 +02:00
|
|
|
public function __toString()
|
|
|
|
{
|
2014-08-19 09:42:16 +02:00
|
|
|
return $this->getAbsoluteUrl('&');
|
2013-06-27 12:45:18 +02:00
|
|
|
}
|
|
|
|
}
|