|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Risan\OAuth1\Signature; |
|
4
|
|
|
|
|
5
|
|
|
use Risan\OAuth1\Request\UriParserInterface; |
|
6
|
|
|
|
|
7
|
|
|
class BaseStringBuilder implements BaseStringBuilderInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* The UriParserInterface instance. |
|
11
|
|
|
* |
|
12
|
|
|
* @var \Risan\OAuth1\Request\UriParserInterface |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $uriParser; |
|
15
|
|
|
|
|
16
|
15 |
|
public function __construct(UriParserInterface $uriParser) |
|
17
|
|
|
{ |
|
18
|
15 |
|
$this->uriParser = $uriParser; |
|
19
|
15 |
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* {@inheritdoc} |
|
23
|
|
|
*/ |
|
24
|
1 |
|
public function getUriParser() |
|
25
|
|
|
{ |
|
26
|
1 |
|
return $this->uriParser; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public function build($httpMethod, $uri, array $parameters = []) |
|
33
|
|
|
{ |
|
34
|
4 |
|
$uri = $this->uriParser->toPsrUri($uri); |
|
35
|
|
|
|
|
36
|
4 |
|
$components = []; |
|
37
|
|
|
|
|
38
|
4 |
|
$components[] = rawurlencode($this->buildMethodComponent($httpMethod)); |
|
39
|
|
|
|
|
40
|
4 |
|
$components[] = rawurlencode($this->buildUriComponent($uri)); |
|
41
|
|
|
|
|
42
|
4 |
|
parse_str($uri->getQuery(), $queryParameters); |
|
43
|
|
|
|
|
44
|
4 |
|
$components[] = rawurlencode($this->buildParametersComponent(array_merge($queryParameters, $parameters))); |
|
45
|
|
|
|
|
46
|
4 |
|
return implode('&', $components); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
5 |
|
public function buildMethodComponent($httpMethod) |
|
53
|
|
|
{ |
|
54
|
5 |
|
return strtoupper($httpMethod); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function buildUriComponent($uri) |
|
61
|
|
|
{ |
|
62
|
6 |
|
$uri = $this->uriParser->toPsrUri($uri); |
|
63
|
|
|
|
|
64
|
6 |
|
return $this->uriParser->buildFromParts([ |
|
65
|
6 |
|
'scheme' => $uri->getScheme(), |
|
66
|
6 |
|
'host' => $uri->getHost(), |
|
67
|
6 |
|
'port' => $uri->getPort(), |
|
68
|
6 |
|
'path' => $uri->getPath(), |
|
69
|
|
|
]); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
5 |
|
public function buildParametersComponent(array $parameters) |
|
76
|
|
|
{ |
|
77
|
5 |
|
$parameters = $this->normalizeParameters($parameters); |
|
78
|
|
|
|
|
79
|
5 |
|
return $this->buildQueryString($parameters); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Normalize the given request parameters. |
|
84
|
|
|
* |
|
85
|
|
|
* @param array $parameters |
|
86
|
|
|
* |
|
87
|
|
|
* @return array |
|
88
|
|
|
*/ |
|
89
|
7 |
|
public function normalizeParameters(array $parameters) |
|
90
|
|
|
{ |
|
91
|
7 |
|
$normalized = []; |
|
92
|
|
|
|
|
93
|
|
|
// [1] Encode both the keys and values. |
|
94
|
|
|
// Decode it frist, in case the given data is already encoded. |
|
95
|
7 |
|
foreach ($parameters as $key => $value) { |
|
96
|
7 |
|
$key = rawurlencode(rawurldecode($key)); |
|
97
|
|
|
|
|
98
|
7 |
|
if (is_array($value)) { |
|
99
|
1 |
|
$normalized[$key] = $this->normalizeParameters($value); |
|
100
|
|
|
} else { |
|
101
|
7 |
|
$normalized[$key] = rawurlencode(rawurldecode($value)); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// [2] Sort by the encoded key. |
|
106
|
7 |
|
ksort($normalized); |
|
107
|
|
|
|
|
108
|
7 |
|
return $normalized; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Build query string from the given parameters. |
|
113
|
|
|
* |
|
114
|
|
|
* @param array $parameters |
|
115
|
|
|
* @param array $initialQueryParameters |
|
116
|
|
|
* @param string $previousKey |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
7 |
|
public function buildQueryString(array $parameters, array $initialQueryParameters = [], $previousKey = null) |
|
121
|
|
|
{ |
|
122
|
7 |
|
$queryParameters = $initialQueryParameters; |
|
123
|
|
|
|
|
124
|
7 |
|
foreach ($parameters as $key => $value) { |
|
125
|
7 |
|
if (null !== $previousKey) { |
|
126
|
1 |
|
$key = "{$previousKey}[{$key}]"; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
7 |
|
if (is_array($value)) { |
|
130
|
1 |
|
$queryParameters = $this->buildQueryString($value, $queryParameters, $key); |
|
131
|
|
|
} else { |
|
132
|
7 |
|
$queryParameters[] = "{$key}={$value}"; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
7 |
|
return null !== $previousKey ? $queryParameters : implode('&', $queryParameters); |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|