Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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([]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can add this to a let method, it's like setUp in phpunit. If you use beConstructedWith again, it will override the on in the let method.

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we use ::class constants wherever possible. phpspec 3.0 generates specs like that by default.

}

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
53 changes: 53 additions & 0 deletions src/Plugin/QueryDefaultsPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 should not be url encoded.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"should" is too weak. i would say "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);
}
}