Skip to content

Commit 3c5aba9

Browse files
committed
Utility/Uri::withParsedValues() - handle null values
1 parent ca844ba commit 3c5aba9

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

src/HttpMessage/Utility/Uri.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public static function fromGlobals(): BdkUri
5959
*/
6060
public static function fromParsed(array $parsed): BdkUri
6161
{
62-
$uri = new BdkUri();
63-
return self::withParsedValues($uri, $parsed);
62+
return self::withParsedValues(new BdkUri(), $parsed);
6463
}
6564

6665
/**
@@ -128,8 +127,7 @@ public static function resolve(UriInterface $base, UriInterface $rel): UriInterf
128127
if ($rel->getScheme() !== '') {
129128
// rel specified scheme
130129
// return rel (with path cleaned up)
131-
return $rel
132-
->withPath(self::pathRemoveDots($rel->getPath()));
130+
return $rel->withPath(self::pathRemoveDots($rel->getPath()));
133131
}
134132
$targetValues = array(
135133
'fragment' => $rel->getFragment(),
@@ -158,27 +156,27 @@ public static function resolve(UriInterface $base, UriInterface $rel): UriInterf
158156
* Apply component values to a Uri
159157
*
160158
* @param UriInterface $uri UriInterface instance
161-
* @param array $parsed Component values
159+
* @param array $values Component values
162160
*
163161
* @return UriInterface
164162
*
165163
* @since x3.4
166164
*/
167165
public static function withParsedValues(UriInterface $uri, array $values): UriInterface
168166
{
169-
$uriKeys = ['fragment', 'host', 'path', 'port', 'query', 'scheme', 'userInfo'];
170-
$values = \array_intersect_key(self::parsedPartsPrep($values), \array_flip($uriKeys));
171-
if (\array_key_exists('path', $values) && $values['path'] === null) {
172-
$values['path'] = '';
173-
}
174-
foreach ($values as $key => $value) {
167+
$keys = ['fragment', 'host', 'path', 'port', 'query', 'scheme', 'userInfo'];
168+
$nullToStringKeys = ['fragment', 'host', 'path', 'query', 'scheme'];
169+
$values = \array_intersect_key(self::parsedPartsPrep($values), \array_flip($keys));
170+
\array_walk($values, static function ($value, $key) use ($nullToStringKeys, &$uri) {
171+
if ($value === null && \in_array($key, $nullToStringKeys, true)) {
172+
$value = '';
173+
}
175174
$method = 'with' . \ucfirst($key);
176175
// using call_user_func_array... some methods (withUserInfo) accept multiple arguments
177-
$args = $value === null
176+
$uri = \call_user_func_array([$uri, $method], $value === null
178177
? array(null)
179-
: (array) $value;
180-
$uri = \call_user_func_array([$uri, $method], $args);
181-
}
178+
: (array) $value);
179+
});
182180
return $uri;
183181
}
184182

tests/HttpMessage/Utility/UriTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ public static function providerFromParsed()
181181
),
182182
'http://0:0@example.com:1234/0?0#0'
183183
),
184+
'nullValues' => array(
185+
array(
186+
'fragment' => null,
187+
'host' => null,
188+
'username' => null,
189+
'password' => null,
190+
'path' => null,
191+
'port' => null,
192+
'query' => null,
193+
'scheme' => null,
194+
),
195+
'',
196+
),
184197
);
185198
}
186199

0 commit comments

Comments
 (0)