Skip to content

Added QueryDefaultsPlugin #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Mar 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## Unreleased

### Added

- `QueryDefaultsPlugin` to add default query parameters.

## 1.4.2 - 2017-03-18

Expand Down
42 changes: 42 additions & 0 deletions spec/Plugin/QueryDefaultsPluginSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace spec\Http\Client\Common\Plugin;

use Http\Client\Common\Plugin;
use Psr\Http\Message\RequestInterface;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\UriInterface;

class QueryDefaultsPluginSpec extends ObjectBehavior
{
public function let()
{
$this->beConstructedWith([]);
}

public function it_is_initializable()
{
$this->shouldHaveType('Http\Client\Common\Plugin\QueryDefaultsPlugin');
}

public function it_is_a_plugin()
{
$this->shouldImplement('Http\Client\Common\Plugin');
}

public function it_sets_the_default_header(RequestInterface $request, UriInterface $uri)
{
$this->beConstructedWith([
'foo' => 'bar',
]);

$request->getUri()->shouldBeCalled()->willReturn($uri);
$uri->getQuery()->shouldBeCalled()->willReturn('test=true');
$uri->withQuery('test=true&foo=bar')->shouldBeCalled()->willReturn($uri);
$request->withUri($uri)->shouldBeCalled()->willReturn($request);

$this->handleRequest($request, function () {
}, function () {
});
}
}
2 changes: 1 addition & 1 deletion src/Plugin/HeaderAppendPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*
* This only makes sense for headers that can have multiple values like 'Forwarded'
*
* @link https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
* @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
*
* @author Soufiane Ghzal <[email protected]>
*/
Expand Down
54 changes: 54 additions & 0 deletions src/Plugin/QueryDefaultsPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Http\Client\Common\Plugin;

use Http\Client\Common\Plugin;
use Psr\Http\Message\RequestInterface;

/**
* Set query to default value if it does not exist.
*
* If a given query parameter already exists the value wont be replaced and the request wont be changed.
*
* @author Tobias Nyholm <[email protected]>
*/
final class QueryDefaultsPlugin implements Plugin
{
/**
* @var array
*/
private $queryParams = [];

/**
* @param array $queryParams Hashmap of query name to query value. Names and values must not be url encoded as
* this plugin will encode them
*/
public function __construct(array $queryParams)
{
$this->queryParams = $queryParams;
}

/**
* {@inheritdoc}
*/
public function handleRequest(RequestInterface $request, callable $next, callable $first)
{
foreach ($this->queryParams as $name => $value) {
$uri = $request->getUri();
$array = [];
parse_str($uri->getQuery(), $array);

// If query value is not found
if (!isset($array[$name])) {
$array[$name] = $value;

// Create a new request with the new URI with the added query param
$request = $request->withUri(
$uri->withQuery(http_build_query($array))
);
}
}

return $next($request);
}
}