Skip to content

Commit

Permalink
Minor update on Uri::removeParams().
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Jun 9, 2020
1 parent 1657bfc commit 79a3217
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
16 changes: 16 additions & 0 deletions spec/EcPhp/CasLib/Utils/UriSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,22 @@ public function it_can_remove_parameters()
$this::removeParams($uri, 'param3', 'param2', 'param1')
->__toString()
->shouldReturn('http://host/path#fragment');

$url = 'http://host/path?param1=param1&param2=param2&param3=param3#fragment';

$uri = new \Nyholm\Psr7\Uri($url);

$this::removeParams($uri, 'param1', 'param2', 'param4')
->__toString()
->shouldReturn('http://host/path?param3=param3#fragment');

$url = 'http://host/path?param1=param1&param2=param2&param3=param3#fragment';

$uri = new \Nyholm\Psr7\Uri($url);

$this::removeParams($uri)
->__toString()
->shouldReturn($url);
}

public function it_can_set_multiple_params_at_the_same_time()
Expand Down
11 changes: 2 additions & 9 deletions src/Utils/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,15 @@ public static function hasParams(UriInterface $uri, string ...$keys): bool
*/
public static function removeParams(UriInterface $uri, string ...$keys): UriInterface
{
foreach ($keys as $key) {
if (false === self::hasParams($uri, $key)) {
continue;
}

$uri = $uri->withQuery(
return $uri
->withQuery(
http_build_query(
array_diff_key(
self::getParams($uri),
array_flip($keys)
)
)
);
}

return $uri;
}

/**
Expand Down

0 comments on commit 79a3217

Please sign in to comment.