Skip to content
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.

Commit

Permalink
fix bug: transposing a parsed uri forgets any parameters changed in q…
Browse files Browse the repository at this point in the history
…uery string
  • Loading branch information
mikey179 committed Oct 3, 2014
1 parent 1c00b61 commit c894c45
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
5.1.1 (2014-10-03)
------------------

* fixed bug: transposing a parsed uri forgot any parameters changed in query string


5.1.0 (2014-09-29)
------------------

Expand Down
21 changes: 14 additions & 7 deletions src/main/php/peer/ParsedUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ class ParsedUri
/**
* constructor
*
* @param string $uri
* Passing a query string will omit any query string already present in $uri.
*
* @param string $uri uri to parse
* @param \stubbles\peer\QueryString $queryString optional parameters when not in uri
* @throws \stubbles\peer\MalformedUriException
*/
public function __construct($uri)
public function __construct($uri, QueryString $queryString = null)
{
$this->uri = ((!is_array($uri)) ? (parse_url($uri)): ($uri));
if (!is_array($this->uri)) {
Expand All @@ -46,10 +49,14 @@ public function __construct($uri)
$this->uri['host'] = strtolower($this->uri['host']);
}

try {
$this->queryString = new QueryString((isset($this->uri['query'])) ? ($this->uri['query']) : (null));
} catch (\InvalidArgumentException $iae) {
throw new MalformedUriException($iae->getMessage(), $iae);
if (null !== $queryString) {
$this->queryString = $queryString;
} else {
try {
$this->queryString = new QueryString((isset($this->uri['query'])) ? ($this->uri['query']) : (null));
} catch (\InvalidArgumentException $iae) {
throw new MalformedUriException($iae->getMessage(), $iae);
}
}

// bugfix for a PHP issue: ftp://user:@auxiliary.kl-s.com/
Expand All @@ -75,7 +82,7 @@ public function __construct($uri)
*/
public function transpose(array $changedUri)
{
return new self(array_merge($this->uri, $changedUri));
return new self(array_merge($this->uri, $changedUri), $this->queryString);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions src/test/php/peer/ParsedUriTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* This file is part of stubbles.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package stubbles
*/
namespace stubbles\peer;
/**
* Test for stubbles\peer\ParsedUri.
*
* @group peer
* @since 5.1.1
*/
class ParsedUriTest extends \PHPUnit_Framework_TestCase
{
/**
* @test
*/
public function transposeKeepsChangedParameters()
{
$parsedUri = new ParsedUri('http://example.com/?foo=bar&baz=303');
$parsedUri->queryString()->addParam('baz', '313');
$parsedUri->queryString()->addParam('dummy', 'example');
$this->assertEquals(
'https://example.com/?foo=bar&baz=313&dummy=example',
$parsedUri->transpose(['scheme' => 'https'])->asStringWithoutPort()
);
}
}

0 comments on commit c894c45

Please sign in to comment.