Skip to content

Commit

Permalink
Fixed encodeComponent, parseQueryString
Browse files Browse the repository at this point in the history
Added decodeComponent
  • Loading branch information
sorinsarca committed May 22, 2021
1 parent 92816ad commit 0f3ca49
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -811,10 +811,10 @@ public static function parseQueryString(?string $query): array
foreach (explode('&', $query) as $name) {
$value = null;
if (($pos = strpos($name, '=')) !== false) {
$value = self::encodeComponent(substr($name, $pos + 1));
$name = self::encodeComponent(substr($name, 0, $pos));
$value = self::decodeComponent(substr($name, $pos + 1));
$name = self::decodeComponent(substr($name, 0, $pos));
} else {
$name = self::encodeComponent($name);
$name = self::decodeComponent($name);
}
$list[$name] = $value;
}
Expand Down Expand Up @@ -890,8 +890,17 @@ public static function normalizeQueryString(string $query): string
return static::buildQueryString(self::parseQueryString($query), null, '&', true);
}

public static function decodeComponent(string $component): string
{
return rawurldecode($component);
}

public static function encodeComponent(string $component, ?array $skip = null): string
{
if (!$skip) {
return rawurlencode($component);
}

$str = '';

foreach (UnicodeString::walkString($component) as [$cp, $chars]) {
Expand All @@ -901,7 +910,7 @@ public static function encodeComponent(string $component, ?array $skip = null):
($cp >= 0x41 && $cp <= 0x5A) ||
($cp >= 0x61 && $cp <= 0x7A) ||
($cp >= 0x30 && $cp <= 0x39) ||
($skip && in_array($cp, $skip, true))
in_array($cp, $skip, true)
) {
$str .= chr($cp);
} else {
Expand All @@ -910,7 +919,7 @@ public static function encodeComponent(string $component, ?array $skip = null):
} else {
$i = 0;
while (isset($chars[$i])) {
$str .= '%' . strtoupper(dechex(ord($chars[$i++])));
$str .= '%' . strtoupper(dechex($chars[$i++]));
}
}
}
Expand Down

0 comments on commit 0f3ca49

Please sign in to comment.