Skip to content

Commit 053de60

Browse files
committed
Move extra string representation fron Uri to Modifier class
1 parent 851d237 commit 053de60

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

UriString.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Stringable;
2121
use Throwable;
2222

23+
use function array_map;
2324
use function array_merge;
2425
use function array_pop;
2526
use function array_reduce;
@@ -33,6 +34,7 @@
3334
use function preg_match;
3435
use function rawurldecode;
3536
use function sprintf;
37+
use function str_replace;
3638
use function strpos;
3739
use function strtolower;
3840
use function substr;
@@ -194,6 +196,45 @@ final class UriString
194196
*/
195197
private const MAXIMUM_HOST_CACHED = 100;
196198

199+
/**
200+
* Generate an IRI string representation (RFC3987) from its parsed representation
201+
* returned by League\UriString::parse() or PHP's parse_url.
202+
*
203+
* If you supply your own array, you are responsible for providing
204+
* valid components without their URI delimiters.
205+
*
206+
* @link https://tools.ietf.org/html/rfc3986#section-5.3
207+
* @link https://tools.ietf.org/html/rfc3986#section-7.5
208+
*/
209+
public static function toIriString(Stringable|string $uri): string
210+
{
211+
$components = UriString::parse($uri);
212+
$port = null;
213+
if (isset($components['port'])) {
214+
$port = (int) $components['port'];
215+
unset($components['port']);
216+
}
217+
218+
if (null !== $components['host']) {
219+
$components['host'] = IdnaConverter::toUnicode($components['host'])->domain();
220+
}
221+
222+
$components['path'] = Encoder::decodePath($components['path']);
223+
$components['user'] = Encoder::decodeNecessary($components['user']);
224+
$components['pass'] = Encoder::decodeNecessary($components['pass']);
225+
$components['query'] = Encoder::decodeQuery($components['query']);
226+
$components['fragment'] = Encoder::decodeFragment($components['fragment']);
227+
228+
return self::build([
229+
...array_map(fn (?string $value) => match (true) {
230+
null === $value,
231+
!str_contains($value, '%20') => $value,
232+
default => str_replace('%20', ' ', $value),
233+
}, $components),
234+
...['port' => $port],
235+
]);
236+
}
237+
197238
/**
198239
* Generate a URI string representation from its parsed representation
199240
* returned by League\UriString::parse() or PHP's parse_url.

0 commit comments

Comments
 (0)