File tree Expand file tree Collapse file tree 4 files changed +102
-1
lines changed Expand file tree Collapse file tree 4 files changed +102
-1
lines changed Original file line number Diff line number Diff line change 11# Change Log
22
3+ ## Unreleased
4+
5+ ### Added
6+
7+ - ` QueryDefaultsPlugin ` to add default query parameters.
38
49## 1.4.2 - 2017-03-18
510
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace spec \Http \Client \Common \Plugin ;
4+
5+ use Http \Client \Common \Plugin ;
6+ use Psr \Http \Message \RequestInterface ;
7+ use PhpSpec \ObjectBehavior ;
8+ use Psr \Http \Message \UriInterface ;
9+
10+ class QueryDefaultsPluginSpec extends ObjectBehavior
11+ {
12+ public function let ()
13+ {
14+ $ this ->beConstructedWith ([]);
15+ }
16+
17+ public function it_is_initializable ()
18+ {
19+ $ this ->shouldHaveType ('Http\Client\Common\Plugin\QueryDefaultsPlugin ' );
20+ }
21+
22+ public function it_is_a_plugin ()
23+ {
24+ $ this ->shouldImplement ('Http\Client\Common\Plugin ' );
25+ }
26+
27+ public function it_sets_the_default_header (RequestInterface $ request , UriInterface $ uri )
28+ {
29+ $ this ->beConstructedWith ([
30+ 'foo ' => 'bar ' ,
31+ ]);
32+
33+ $ request ->getUri ()->shouldBeCalled ()->willReturn ($ uri );
34+ $ uri ->getQuery ()->shouldBeCalled ()->willReturn ('test=true ' );
35+ $ uri ->withQuery ('test=true&foo=bar ' )->shouldBeCalled ()->willReturn ($ uri );
36+ $ request ->withUri ($ uri )->shouldBeCalled ()->willReturn ($ request );
37+
38+ $ this ->handleRequest ($ request , function () {
39+ }, function () {
40+ });
41+ }
42+ }
Original file line number Diff line number Diff line change 1212 *
1313 * This only makes sense for headers that can have multiple values like 'Forwarded'
1414 *
15- * @link https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
15+ * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
1616 *
1717 * @author Soufiane Ghzal <sghzal@gmail.com>
1818 */
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Http \Client \Common \Plugin ;
4+
5+ use Http \Client \Common \Plugin ;
6+ use Psr \Http \Message \RequestInterface ;
7+
8+ /**
9+ * Set query to default value if it does not exist.
10+ *
11+ * If a given query parameter already exists the value wont be replaced and the request wont be changed.
12+ *
13+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
14+ */
15+ final class QueryDefaultsPlugin implements Plugin
16+ {
17+ /**
18+ * @var array
19+ */
20+ private $ queryParams = [];
21+
22+ /**
23+ * @param array $queryParams Hashmap of query name to query value. Names and values must not be url encoded as
24+ * this plugin will encode them
25+ */
26+ public function __construct (array $ queryParams )
27+ {
28+ $ this ->queryParams = $ queryParams ;
29+ }
30+
31+ /**
32+ * {@inheritdoc}
33+ */
34+ public function handleRequest (RequestInterface $ request , callable $ next , callable $ first )
35+ {
36+ foreach ($ this ->queryParams as $ name => $ value ) {
37+ $ uri = $ request ->getUri ();
38+ $ array = [];
39+ parse_str ($ uri ->getQuery (), $ array );
40+
41+ // If query value is not found
42+ if (!isset ($ array [$ name ])) {
43+ $ array [$ name ] = $ value ;
44+
45+ // Create a new request with the new URI with the added query param
46+ $ request = $ request ->withUri (
47+ $ uri ->withQuery (http_build_query ($ array ))
48+ );
49+ }
50+ }
51+
52+ return $ next ($ request );
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments