public static function getPreferredFormat(ServerRequestInterface $request)
    {
        $acceptTypes = $request->getHeader('accept');

        if (count($acceptTypes) > 0) {
            $acceptType = $acceptTypes[0];

            // As many formats may match for a given Accept header, let's try to find the one that fits the best
            $counters = [];
            foreach (self::$formats as $format => $values) {
                foreach ($values as $value) {
                    $counters[$format] = isset($counters[$format]) ? $counters[$format] : 0;
                    $counters[$format] += intval(strpos($acceptType, $value) !== false);
                }
            }

            // Sort the array to retrieve the format that best matches the Accept header
            asort($counters);
            end($counters);
            return key($counters);
        }

        return 'html';
    }