private function filterFragment(string $fragment): string
    {
        if (trim($fragment) == '') {
            return '';
        }

        if ($fragment[0] == '#') {
            $fragment = substr($fragment, 1);
        }

        return preg_replace_callback(
            '/(?:[^'.self::UNRESERVED.self::SUB_DELIMS.':@%~\/\[\]\?]+|%(?![A-Fa-f0-9]{2}))/',
            function (array $match) {
                return rawurlencode($match[0]);
            },
            $fragment
        );
    }