public function determinePeferredFormat($acceptHeader, $allowedFormats = ['json', 'xml', 'html'], $default = 'json')
    {
        if (empty($acceptHeader)) {
            return $default;
        }

        $negotiator = new Negotiator();
        try {
            $elements = $negotiator->getOrderedElements($acceptHeader);
        } catch (InvalidMediaType $e) {
            return $default;
        }

        foreach ($elements as $element) {
            $subpart = $element->getSubPart();
            foreach ($allowedFormats as $format) {
                if (stripos($subpart, $format) !== false) {
                    return $format;
                }
            }
        }

        return $default;
    }
public function determinePeferredFormat($acceptHeader, $allowedFormats = ['json', 'xml', 'html'], $default = 'json')
    {
        if (empty($acceptHeader)) {
            return $default;
        }

        $negotiator = new Negotiator();
        try {
            $elements = $negotiator->getOrderedElements($acceptHeader);
        } catch (InvalidMediaType $e) {
            return $default;
        }

        foreach ($elements as $element) {
            $subpart = $element->getSubPart();
            foreach ($allowedFormats as $format) {
                if (stripos($subpart, $format) !== false) {
                    return $format;
                }
            }
        }

        return $default;
    }
public function determinePeferredFormat ( $ acceptHeader , $ allowedFormats = [ 'json' , 'xml' , 'html' ] , $ default = 'json' ) { if ( empty ( $ acceptHeader ) ) { return $ default ; } $ negotiator = new Negotiator ( ) ; try { $ elements = $ negotiator -> getOrderedElements ( $ acceptHeader ) ; } catch ( InvalidMediaType $ e ) { return $ default ; } foreach ( $ elements as $ element ) { $ subpart = $ element -> getSubPart ( ) ; foreach ( $ allowedFormats as $ format ) { if ( stripos ( $ subpart , $ format ) !== false ) { return $ format ; } } } return $ default ; }
Read the accept header and work out which format is preferred

@param  string $acceptHeader Accept header from request
@param  array  $allowedFormats Array of formats that are preferred
@param  string $default Default format to return if no allowedFormats are found
@return string
Read the accept header and work out which format is preferred