@@ -33,6 +33,11 @@ final class Request
33
33
*/
34
34
private $ body ;
35
35
36
+ /**
37
+ * @var string|null
38
+ */
39
+ private $ queryString ;
40
+
36
41
/**
37
42
* @var array<string, string>
38
43
*/
@@ -43,6 +48,11 @@ final class Request
43
48
*/
44
49
private $ endpoint ;
45
50
51
+ /**
52
+ * @var string
53
+ */
54
+ private $ hostPrefix ;
55
+
46
56
/**
47
57
* @var array{scheme: string, host: string, port: int|null}|null
48
58
*/
@@ -52,7 +62,7 @@ final class Request
52
62
* @param array<string, string> $query
53
63
* @param array<string, string> $headers
54
64
*/
55
- public function __construct (string $ method , string $ uri , array $ query , array $ headers , RequestStream $ body )
65
+ public function __construct (string $ method , string $ uri , array $ query , array $ headers , RequestStream $ body, string $ hostPrefix = '' )
56
66
{
57
67
$ this ->method = $ method ;
58
68
$ this ->uri = $ uri ;
@@ -62,6 +72,7 @@ public function __construct(string $method, string $uri, array $query, array $he
62
72
}
63
73
$ this ->body = $ body ;
64
74
$ this ->query = $ query ;
75
+ $ this ->hostPrefix = $ hostPrefix ;
65
76
$ this ->endpoint = '' ;
66
77
}
67
78
@@ -126,12 +137,14 @@ public function hasQueryAttribute(string $name): bool
126
137
public function removeQueryAttribute (string $ name ): void
127
138
{
128
139
unset($ this ->query [$ name ]);
140
+ $ this ->queryString = null ;
129
141
$ this ->endpoint = '' ;
130
142
}
131
143
132
144
public function setQueryAttribute (string $ name , string $ value ): void
133
145
{
134
146
$ this ->query [$ name ] = $ value ;
147
+ $ this ->queryString = null ;
135
148
$ this ->endpoint = '' ;
136
149
}
137
150
@@ -148,35 +161,55 @@ public function getQuery(): array
148
161
return $ this ->query ;
149
162
}
150
163
164
+ public function getHostPrefix (): string
165
+ {
166
+ return $ this ->hostPrefix ;
167
+ }
168
+
169
+ public function setHostPrefix (string $ hostPrefix ): void
170
+ {
171
+ $ this ->hostPrefix = $ hostPrefix ;
172
+ $ this ->endpoint = '' ;
173
+ }
174
+
151
175
public function getEndpoint (): string
152
176
{
153
177
if (empty ($ this ->endpoint )) {
154
178
if (null === $ this ->parsed ) {
155
179
throw new LogicException ('Request::$endpoint must be set before using it. ' );
156
180
}
157
181
158
- $ this ->endpoint = $ this ->parsed ['scheme ' ] . ':// ' . $ this ->parsed ['host ' ] . (isset ($ this ->parsed ['port ' ]) ? ': ' . $ this ->parsed ['port ' ] : '' ) . $ this ->uri . ($ this ->query ? (false === strpos ($ this ->uri , '? ' ) ? '? ' : '& ' ) . http_build_query ( $ this ->query , '' , ' & ' , \ PHP_QUERY_RFC3986 ) : '' );
182
+ $ this ->endpoint = $ this ->parsed ['scheme ' ] . ':// ' . $ this ->hostPrefix . $ this -> parsed ['host ' ] . (isset ($ this ->parsed ['port ' ]) ? ': ' . $ this ->parsed ['port ' ] : '' ) . $ this ->uri . ($ this ->query ? (false === strpos ($ this ->uri , '? ' ) ? '? ' : '& ' ) . $ this ->getQueryString ( ) : '' );
159
183
}
160
184
161
185
return $ this ->endpoint ;
162
186
}
163
187
164
188
public function setEndpoint (string $ endpoint ): void
165
189
{
166
- if (! empty ( $ this ->endpoint ) ) {
190
+ if (null !== $ this ->parsed ) {
167
191
throw new LogicException ('Request::$endpoint cannot be changed after it has a value. ' );
168
192
}
169
193
170
- $ this ->endpoint = $ endpoint ;
171
- $ parsed = parse_url ($ this ->endpoint );
194
+ $ parsed = parse_url ($ endpoint );
172
195
173
196
if (false === $ parsed || !isset ($ parsed ['scheme ' ], $ parsed ['host ' ])) {
174
197
throw new InvalidArgument (sprintf ('The endpoint "%s" is invalid. ' , $ endpoint ));
175
198
}
176
199
177
200
$ this ->parsed = ['scheme ' => $ parsed ['scheme ' ], 'host ' => $ parsed ['host ' ], 'port ' => $ parsed ['port ' ] ?? null ];
178
201
202
+ $ this ->queryString = $ parsed ['query ' ] ?? '' ;
179
203
parse_str ($ parsed ['query ' ] ?? '' , $ this ->query );
180
204
$ this ->uri = $ parsed ['path ' ] ?? '/ ' ;
181
205
}
206
+
207
+ private function getQueryString (): string
208
+ {
209
+ if (null === $ this ->queryString ) {
210
+ $ this ->queryString = http_build_query ($ this ->query , '' , '& ' , \PHP_QUERY_RFC3986 );
211
+ }
212
+
213
+ return $ this ->queryString ;
214
+ }
182
215
}
0 commit comments