protected function marshalPath($path): string
    {
        if ( null === $path ) {
            $path = '/';
        }

        if ( ! is_string($path) ) {
            throw new UriException(
                'Path must be a string or null'
            );
        }

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