From 15c4eca1f9ae1fd72fc9e3769abbd686dbb1cb9f Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 15 Sep 2016 14:22:53 +0200 Subject: [PATCH] Match urifactory of javascript impl. --- Uri/UriFactory.php | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/Uri/UriFactory.php b/Uri/UriFactory.php index 751b1ec1b..7e234fcdd 100644 --- a/Uri/UriFactory.php +++ b/Uri/UriFactory.php @@ -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; } }