private static function myValidateFragment(?string $fragment, ?string &$error): bool
    {
        if ($fragment === null) {
            return true;
        }

        if (preg_match('/[^0-9a-zA-Z._~!\$&\'()*\+,;=:@\[\]\/\?%-]/', $fragment, $matches)) {
            $error = 'Fragment "' . $fragment . '" contains invalid character "' . $matches[0] . '".';

            return false;
        }

        return true;
    }