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 1
1
# Change Log
2
2
3
+ ## Unreleased
4
+
5
+ ### Added
6
+
7
+ - ` QueryDefaultsPlugin ` to add default query parameters.
3
8
4
9
## 1.4.2 - 2017-03-18
5
10
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 12
12
*
13
13
* This only makes sense for headers that can have multiple values like 'Forwarded'
14
14
*
15
- * @link https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
15
+ * @see https://en.wikipedia.org/wiki/List_of_HTTP_header_fields
16
16
*
17
17
* @author Soufiane Ghzal <sghzal@gmail.com>
18
18
*/
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