public function getBestEncodingMatch(
        IMediaTypeFormatter $formatter,
        array $acceptCharsetHeaders,
        ?MediaTypeHeaderValue $mediaTypeHeader
    ): ?string {
        $rankedAcceptCharsetHeaders = $this->rankAcceptCharsetHeaders($acceptCharsetHeaders);

        foreach ($rankedAcceptCharsetHeaders as $acceptCharsetHeader) {
            foreach ($formatter->getSupportedEncodings() as $supportedEncoding) {
                $charset = $acceptCharsetHeader->getCharset();

                if ($charset === '*' || strcasecmp($charset, $supportedEncoding) === 0) {
                    return $supportedEncoding;
                }
            }
        }

        if ($mediaTypeHeader === null || $mediaTypeHeader->getCharset() === null) {
            return null;
        }

        // Fall back to the charset in the media type header
        foreach ($formatter->getSupportedEncodings() as $supportedEncoding) {
            $charset = $mediaTypeHeader->getCharset();

            if ($charset === '*' || strcasecmp($charset, $supportedEncoding) === 0) {
                return $supportedEncoding;
            }
        }

        return null;
    }