|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the bitbucket-api package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Alexandru G. <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bitbucket\API\Http\Plugin; |
|
13
|
|
|
|
|
14
|
|
|
use Http\Client\Common\Plugin; |
|
15
|
|
|
use Http\Discovery\MessageFactoryDiscovery; |
|
16
|
|
|
use Http\Message\RequestFactory; |
|
17
|
|
|
use Http\Promise\Promise; |
|
18
|
|
|
use Psr\Http\Message\RequestInterface; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Transform PHP array to API array |
|
22
|
|
|
* |
|
23
|
|
|
* PHP array square brackets does not play nice with remote API, |
|
24
|
|
|
* which expects just the key name, without brackets. |
|
25
|
|
|
* |
|
26
|
|
|
* Transforms: foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy" |
|
27
|
|
|
* |
|
28
|
|
|
* @author Alexandru G. <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
class NormalizeArrayPlugin implements Plugin |
|
31
|
|
|
{ |
|
32
|
|
|
/** @var \Http\Message\RequestFactory */ |
|
33
|
|
|
private $requestFactory; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(RequestFactory $requestFactory = null) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->requestFactory = $requestFactory ?: MessageFactoryDiscovery::find(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritDoc} |
|
42
|
|
|
*/ |
|
43
|
|
|
public function handleRequest(RequestInterface $request, callable $next, callable $first) |
|
44
|
|
|
{ |
|
45
|
|
|
if (in_array('application/x-www-form-urlencoded', $request->getHeader('Content-Type'), true)) { |
|
46
|
|
|
return $next($request); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
// Transform: "foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy" |
|
50
|
|
|
$request = $this->requestFactory->createRequest( |
|
51
|
|
|
$request->getMethod(), |
|
52
|
|
|
$request->getUri(), |
|
53
|
|
|
$request->getHeaders(), |
|
54
|
|
|
preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getBody()->getContents()), |
|
55
|
|
|
$request->getProtocolVersion() |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
// Transform: "foo[0]=xxx&foo[1]=yyy" to "foo=xxx&foo=yyy" |
|
59
|
|
|
$request = $request->withUri( |
|
60
|
|
|
$request->getUri()->withQuery( |
|
61
|
|
|
preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '=', $request->getUri()->getQuery()) |
|
62
|
|
|
) |
|
63
|
|
|
); |
|
64
|
|
|
|
|
65
|
|
|
return $next($request); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|