Match urifactory of javascript impl.

This commit is contained in:
Dennis Eichhorn 2016-09-15 14:22:53 +02:00
parent 42c403a60c
commit 15c4eca1f9

View File

@ -87,6 +87,33 @@ class UriFactory
return false;
}
private static function unique(string $url) : string
{
$parts = explode('?', url);
if (count($parts) >= 2) {
$full = $parts[1];
$pars = explode('&', $full),
$comps = [],
$spl = null,
$length = count($pars);
for ($i = 0; $i < $length; $i++) {
$spl = explode('=', $pars[i]);
$comps[$spl[0]] = $spl[1];
}
$pars = [];
foreach($comps as $key => $value) {
$pars[] = $key . '=' . $value;
}
$url = $parts[0] . '?' . implode('&', $pars);
}
return $url;
}
/**
* Build uri.
*
@ -109,10 +136,19 @@ class UriFactory
*/
public static function build(string $uri, array $toMatch = [])
{
return preg_replace_callback('(\{[\/#\?@\.\$][a-zA-Z0-9\-]*\})', function ($match) use ($toMatch) {
$parsed = preg_replace_callback('(\{[\/#\?@\.\$][a-zA-Z0-9\-]*\})', function ($match) use ($toMatch) {
$match = substr($match[0], 1, strlen($match[0]) - 2);
return $toMatch[$match] ?? self::$uri[$match] ?? $match;
}, $uri);
// todo: maybe don't do this and adjust unique?!
if(strpos($parsed, '?')) {
str_replace('&', '?', $parsed);
}
$parsed = self::uniqid($parsed);
return $parsed;
}
}