public static function parse_url(string $url, int $component = -1)
    {
        // the host and port must be used as is, to allow IPv6 syntax, e.g.: [3b00:f59:1008::212:183:20]:8080
        // thus we parse here, before url-encoding
        $componentsFromUrl = parse_url($url);
        if ($componentsFromUrl === false) {
            return false;
        }

        $encodedUrl = preg_replace_callback('%[^:@/?#&=\.]+%usD', function ($matches) {
            return urlencode($matches[0]);
        }, $url);
        $components = parse_url($encodedUrl);

        if ($components === false) {
            return false;
        }

        foreach ($components as &$currentComponent) {
            $currentComponent = urldecode($currentComponent);
        }

        // the host and port must be used as is, to allow IPv6 syntax, e.g.: [3b00:f59:1008::212:183:20]:8080
        if (array_key_exists('host', $componentsFromUrl)) {
            $components['host'] = $componentsFromUrl['host'];
        }
        if (array_key_exists('port', $componentsFromUrl)) {
            $components['port'] = (integer)$componentsFromUrl['port'];
        } else {
            unset($components['port']);
        }

        switch ($component) {
            case -1:
                return $components;
            case PHP_URL_SCHEME:
                return $components['scheme'];
            case PHP_URL_HOST:
                return $components['host'];
            case PHP_URL_PORT:
                return $components['port'];
            case PHP_URL_USER:
                return $components['user'];
            case PHP_URL_PASS:
                return $components['pass'];
            case PHP_URL_PATH:
                return $components['path'];
            case PHP_URL_QUERY:
                return $components['query'];
            case PHP_URL_FRAGMENT:
                return $components['fragment'];
            default:
                throw new \InvalidArgumentException('Invalid component requested for URL parsing.', 1406280743);
        }
    }