Symfony\Component\HttpFoundation\Request::getFormat PHP Method

getFormat() public method

Gets the format associated with the mime type.
public getFormat ( string $mimeType ) : string | null
$mimeType string The associated mime type
return string | null The format (null if not found)
    public function getFormat($mimeType)
    {
        $canonicalMimeType = null;
        if (false !== $pos = strpos($mimeType, ';')) {
            $canonicalMimeType = substr($mimeType, 0, $pos);
        }

        if (null === static::$formats) {
            static::initializeFormats();
        }

        foreach (static::$formats as $format => $mimeTypes) {
            if (in_array($mimeType, (array) $mimeTypes)) {
                return $format;
            }
            if (null !== $canonicalMimeType && in_array($canonicalMimeType, (array) $mimeTypes)) {
                return $format;
            }
        }
    }

Usage Example

 /**
  * Gets the normalized type of a request.
  *
  * The normalized type is a short, lowercase version of the format, such as
  * 'html', 'json' or 'atom'.
  *
  * @param \Symfony\Component\HttpFoundation\Request $request
  *   The request object from which to extract the content type.
  *
  * @return string
  *   The normalized type of a given request.
  */
 public function getContentType(Request $request)
 {
     // AJAX iframe uploads need special handling, because they contain a JSON
     // response wrapped in <textarea>.
     if ($request->get('ajax_iframe_upload', FALSE)) {
         return 'iframeupload';
     }
     // Check all formats, if priority format is found return it.
     $first_found_format = FALSE;
     $priority = array('html', 'drupal_ajax', 'drupal_modal', 'drupal_dialog');
     foreach ($request->getAcceptableContentTypes() as $mime_type) {
         $format = $request->getFormat($mime_type);
         if (in_array($format, $priority, TRUE)) {
             return $format;
         }
         if (!is_null($format) && !$first_found_format) {
             $first_found_format = $format;
         }
     }
     // No HTML found, return first found.
     if ($first_found_format) {
         return $first_found_format;
     }
     if ($request->isXmlHttpRequest()) {
         return 'ajax';
     }
     // Do HTML last so that it always wins.
     return 'html';
 }
All Usage Examples Of Symfony\Component\HttpFoundation\Request::getFormat