abstract class MediaTypes (View source)

Utility class for converting Internet Media Types to file extensions and vice versa.

"Internet Media Types" are registered through the IANA and were formerly known as "MIME types". Although "MIME" still exists in the context of emails, the term is outdated when speaking about media types for the web.

The static properties containing the actual mappings is derived from a freely available list provided by the Apache HTTPD project. It is generated by a script which can be found in the Scripts folder of the Neos.Utility.MediaTypes package.

Constants

PATTERN_SPLITMEDIARANGE

PATTERN_SPLITMEDIATYPE

Methods

static string
getMediaTypeFromFilename(string $filename)

Returns a Media Type based on the filename extension

static string
getMediaTypeFromFileContent(string $fileContent)

Returns a Media Type based on the file content

static string
getFilenameExtensionFromMediaType(string $mediaType)

Returns the primary filename extension based on the given Media Type.

static array
getFilenameExtensionsFromMediaType(string $mediaType)

Returns all possible filename extensions based on the given Media Type.

static array
parseMediaType(string $rawMediaType)

Parses a RFC 2616 Media Type and returns its parts in an associative array.

static bool
mediaRangeMatches(string $mediaRange, string $mediaType)

Checks if the given media range and the media type match.

static string
trimMediaType(string $rawMediaType)

Strips off any parameters from the given media type and returns just the type and subtype in the format "type/subtype".

Details

static string getMediaTypeFromFilename(string $filename)

Returns a Media Type based on the filename extension

Parameters

string $filename

Filename to determine the media type for, for example "index.html"

Return Value

string

The IANA Internet Media Type

static string getMediaTypeFromFileContent(string $fileContent)

Returns a Media Type based on the file content

Parameters

string $fileContent

The file content do determine the media type from

Return Value

string

The IANA Internet Media Type

static string getFilenameExtensionFromMediaType(string $mediaType)

Returns the primary filename extension based on the given Media Type.

Parameters

string $mediaType

The IANA Internet Media Type, for example "text/html"

Return Value

string

The corresponding filename extension, for example "html"

static array getFilenameExtensionsFromMediaType(string $mediaType)

Returns all possible filename extensions based on the given Media Type.

Parameters

string $mediaType

The IANA Internet Media Type, for example "text/html"

Return Value

array

The corresponding filename extensions, for example ("html", "htm")

static array parseMediaType(string $rawMediaType)

Parses a RFC 2616 Media Type and returns its parts in an associative array.

media-type = type "/" subtype *( ";" parameter)

The media type "text/html; charset=UTF-8" would be parsed and returned with the following array keys and values:

"type" => the type as a string, "text" "subtype" => the subtype as a string, "html" "parameters" => an array of parameter names and values, array("charset" => "UTF-8")

Parameters

string $rawMediaType

The raw media type, for example "application/json; charset=UTF-8"

Return Value

array

An associative array with parsed information

static bool mediaRangeMatches(string $mediaRange, string $mediaType)

Checks if the given media range and the media type match.

The pattern used by this function splits each into media type and subtype and ignores possible additional parameters or extensions. The prepared types and subtypes are then compared and wildcards are considered in this process.

Media ranges are explained in RFC 2616, section 14.1. "Accept".

Parameters

string $mediaRange

The media range, for example "text/*"

string $mediaType

The media type to match against, for example "text/html"

Return Value

bool

true if both match, false if they don't match or either of them is invalid

static string trimMediaType(string $rawMediaType)

Strips off any parameters from the given media type and returns just the type and subtype in the format "type/subtype".

Parameters

string $rawMediaType

The full media type, for example "application/json; charset=UTF-8"

Return Value

string

Just the type and subtype, for example "application/json"