Skip to content

Commit

Permalink
Url append helper (#1981)
Browse files Browse the repository at this point in the history
* url append helper

* add tests, rename to append

* add changelog

* add tests, rename
  • Loading branch information
nadar authored Dec 17, 2019
1 parent a9782f0 commit 172d593
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ In order to read more about upgrading and BC breaks have a look at the [UPGRADE

## 1.0.25

+ [#1981](https://github.com/luyadev/luya/pull/1981) Add `appendQuery()` and `appendQueryTourl()` function to Url Helper. The append method will add a given key value query param to the current url or any url.

## 1.0.24 (9. December 2019)

+ [#1980](https://github.com/luyadev/luya/pull/1980) Added `ArrayHelper::search()` keys option to search only in certain array keys.
Expand Down
47 changes: 47 additions & 0 deletions core/helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,51 @@ public static function domain($url)
{
return self::cleanHost(parse_url(Url::ensureHttp($url), PHP_URL_HOST));
}

/**
* Append a query to the current url.
*
* See {{luya\helpers\Url::appendToUrl()}}
*
* @param string|array $append A string with url fragments or an array which will be processed by http_build_query.
* @param boolean $scheme Add full path schema to the url, by default false. Otherwise absolute paths are used (including domain).
* @return string
* @since 1.0.25
*/
public static function appendQuery($append, $scheme = false)
{
$url = $scheme ? Yii::$app->request->absoluteUrl : Yii::$app->request->url;

return self::appendQueryToUrl($url, $append);
}

/**
* Append an url part to an url
*
* @param string $url The url where the data should be appended.
* @param string|array $append The query param to append, if an array is given http_build_query() will taken to build the query string.
* @return string Returns the url with appended query string
* @since 1.0.25
*/
public static function appendQueryToUrl($url, $append)
{
if (is_array($append)) {
$append = http_build_query($append);
}
// remove starting & and ? chars
$append = ltrim($append, '&?');
// use &: Do we have already a ? in the url
if (StringHelper::contains('?', $url)) {
// seperator already existing
if (StringHelper::endsWith($url, '&') || StringHelper::endsWith($url, '?')) {
return $url . $append;
}

// add seperator
return $url . '&' . $append;
}

// use ?
return $url . '?' . $append;
}
}
15 changes: 15 additions & 0 deletions tests/core/helpers/UrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,19 @@ public function testDomain()
$this->assertSame('luya.io', Url::domain('https://www.luya.io/barfoo'));
$this->assertSame('beta.luya.io', Url::domain('https://beta.luya.io'));
}

public function testUrlAppend()
{
$this->assertSame('/luya/envs/dev/public_html/?foo=bar', Url::appendQuery('?foo=bar'));
$this->assertSame('http://localhost/luya/envs/dev/public_html/?foo=bar', Url::appendQuery('?foo=bar', true));
$this->assertSame('/luya/envs/dev/public_html/?foo=bar', Url::appendQuery(['foo' => 'bar']));
$this->assertSame('/luya/envs/dev/public_html/?foo=bar', Url::appendQuery('&foo=bar'));
$this->assertSame('/luya/envs/dev/public_html/?foo=bar', Url::appendQuery('?&foo=bar'));
}

public function testAppendToUrl()
{
$this->assertSame('http://luya.io?x=z&foo=bar', Url::appendQueryToUrl('http://luya.io?x=z', '?foo=bar'));
$this->assertSame('http://luya.io?foo=bar', Url::appendQueryToUrl('http://luya.io?', '?foo=bar'));
}
}

0 comments on commit 172d593

Please sign in to comment.