vendor/codefog/contao-haste/library/Haste/Util/Url.php line 126

Open in your IDE?
  1. <?php
  2. /**
  3.  * Haste utilities for Contao Open Source CMS
  4.  *
  5.  * Copyright (C) 2012-2013 Codefog & terminal42 gmbh
  6.  *
  7.  * @package    Haste
  8.  * @link       http://github.com/codefog/contao-haste/
  9.  * @license    http://opensource.org/licenses/lgpl-3.0.html LGPL
  10.  */
  11. namespace Haste\Util;
  12. use Contao\Controller;
  13. use Contao\Environment;
  14. use Contao\PageModel;
  15. class Url
  16. {
  17.     /**
  18.      * Add a query string to the given URI string or page ID
  19.      *
  20.      * @param string $strQuery
  21.      * @param mixed  $varUrl
  22.      *
  23.      * @return string
  24.      * @throws \InvalidArgumentException
  25.      */
  26.     public static function addQueryString($strQuery$varUrl null)
  27.     {
  28.         $strUrl = static::prepareUrl($varUrl);
  29.         $strQuery trim(ampersand($strQueryfalse), '&');
  30.         list($strScript$strQueryString) = explode('?'$strUrl2);
  31.         parse_str($strQueryString$queries);
  32.         $queries array_filter($queries);
  33.         unset($queries['language']);
  34.         $href '';
  35.         if (!empty($queries)) {
  36.             parse_str($strQuery$new);
  37.             $href '?' http_build_query(array_merge($queries$new), '''&');
  38.         } elseif (!empty($strQuery)) {
  39.             $href '?' $strQuery;
  40.         }
  41.         return $strScript $href;
  42.     }
  43.     /**
  44.      * Remove query parameters from the current URL
  45.      *
  46.      * @param array           $arrParams
  47.      * @param string|int|null $varUrl
  48.      *
  49.      * @return string
  50.      */
  51.     public static function removeQueryString(array $arrParams$varUrl=null)
  52.     {
  53.         $strUrl = static::prepareUrl($varUrl);
  54.         if (empty($arrParams)) {
  55.             return $strUrl;
  56.         }
  57.         list($strScript$strQueryString) = explode('?'$strUrl2);
  58.         parse_str($strQueryString$queries);
  59.         $queries array_filter($queries);
  60.         $queries array_diff_key($queriesarray_flip($arrParams));
  61.         $href '';
  62.         if (!empty($queries)) {
  63.             $href .= '?' http_build_query($queries'''&');
  64.         }
  65.         return $strScript $href;
  66.     }
  67.     /**
  68.      * Remove query parameters from the current URL using a callback method.
  69.      *
  70.      * @param callable        $callback
  71.      * @param string|int|null $varUrl
  72.      *
  73.      * @return string
  74.      */
  75.     public static function removeQueryStringCallback(callable $callback$varUrl=null)
  76.     {
  77.         $strUrl = static::prepareUrl($varUrl);
  78.         list($strScript$strQueryString) = explode('?'$strUrl2);
  79.         parse_str($strQueryString$queries);
  80.         // Cannot use array_filter because flags ARRAY_FILTER_USE_BOTH is only supported in PHP 5.6
  81.         foreach ($queries as $k => $v) {
  82.             if (true !== call_user_func($callback$v$k)) {
  83.                 unset($queries[$k]);
  84.             }
  85.         }
  86.         $href '';
  87.         if (!empty($queries)) {
  88.             $href .= '?' http_build_query($queries'''&');
  89.         }
  90.         return $strScript $href;
  91.     }
  92.     /**
  93.      * Prepare URL from ID and keep query string from current string
  94.      *
  95.      * @param string|int|null
  96.      *
  97.      * @return string
  98.      */
  99.     protected static function prepareUrl($varUrl)
  100.     {
  101.         if ($varUrl === null) {
  102.             $varUrl Environment::get('request');
  103.         } elseif (is_numeric($varUrl)) {
  104.             if (($objJump PageModel::findByPk($varUrl)) === null) {
  105.                 throw new \InvalidArgumentException('Given page id does not exist.');
  106.             }
  107.             $varUrl Controller::generateFrontendUrl($objJump->row());
  108.             list(, $strQueryString) = explode('?'Environment::get('request'), 2);
  109.             if ($strQueryString != '') {
  110.                 $varUrl .= '?' $strQueryString;
  111.             }
  112.         }
  113.         $varUrl ampersand($varUrlfalse);
  114.         return $varUrl;
  115.     }
  116. }