protected function toRoute(RouteContract $route, array $parameters, int $referenceType): string
    {
        $path = $this->prepareRoutePath($route, $parameters);
        $uri  = $this->uriFactory->createUri('/' . \ltrim($path, '/'));

        if (($host = $route->getDomain()) !== null) {
            $uri = $uri->withHost($host);
        } else {
            $uri = $uri->withHost($this->request->getUri()->getHost());
        }

        $requiredSchemes = $this->isSchemeRequired($route);

        if ($referenceType === self::ABSOLUTE_URL || $requiredSchemes || $referenceType === self::NETWORK_PATH) {
            $uri = $this->addPortAndSchemeToUri($uri, $route);
        }

        if ($referenceType === self::ABSOLUTE_URL || $requiredSchemes) {
            return (string) $uri;
        }

        if ($referenceType === self::NETWORK_PATH) {
            $uri = $uri->withScheme('');

            return (string) $uri;
        }

        return '/' . self::getRelativePath('//' . $uri->getHost() . '/', (string) $uri);
    }