ContentType

Enum
backed by string

Enum ContentType.

Description

Represents a comprehensive set of HTTP Content-Type header values. Each enum case describes a MIME type that MAY be used when constructing or parsing HTTP messages. Implementations interacting with this enum SHOULD ensure appropriate handling based on RFC 2119 requirement levels.

This enum MUST be used when normalizing, validating, or comparing Content-Type header values in a strict and type-safe manner. It SHALL provide helper methods for extracting metadata, ensuring consistent behavior across HTTP message handling.

Table of Contents

Cases

 = 'application/x-www-form-urlencoded'
 = 'application/javascript'
 = 'application/json'
 = 'application/octet-stream'
 = 'application/pdf'
 = 'application/xml'
 = 'image/gif'
 = 'image/jpeg'
 = 'image/png'
 = 'image/svg+xml'
 = 'multipart/form-data'
 = 'text/css'
 = 'text/csv'
 = 'text/html'
 = 'text/plain'
 = 'text/xml'

Methods

fromHeaderString()

Creates a ContentType instance from a full Content-Type header string.

 : self|null
getCharset()

Extracts the charset parameter from a Content-Type header string.

 : string|null
isJson()

Determines whether the content type represents JSON data.

 : bool
isMultipart()

Determines whether the content type represents multipart data.

 : bool
isText()

Determines whether the content type represents text-based content.

 : bool
isXml()

Determines whether the content type represents XML data.

 : bool
withCharset()

Returns the Content-Type header value with an appended charset parameter.

 : string
Cases

Cases

Methods

fromHeaderString()

Public Static

Creates a ContentType instance from a full Content-Type header string.

public static fromHeaderString( string  $header) : self|null

Description

This method SHALL parse header values that include parameters such as charsets or boundary markers. Only the primary MIME type SHALL be used for determining the enum case. If the base MIME type does not match any known ContentType, this method MUST return null.

Parameters
$header : string

Description

the full Content-Type header string

Return values
self|null

Description

the derived ContentType case or null if unsupported

getCharset()

Public Static

Extracts the charset parameter from a Content-Type header string.

public static getCharset( string  $contentTypeHeader) : string|null

Description

This method SHOULD be used when charset negotiation or validation is required. If no charset is present, this method MUST return null. The extracted charset value SHALL be trimmed of surrounding whitespace.

Parameters
$contentTypeHeader : string

Description

the full Content-Type header value

Return values
string|null

Description

the charset value or null if absent

isJson()

Public

Determines whether the content type represents JSON data.

public isJson() : bool

Description

This method SHALL evaluate strictly via enum identity comparison. It MUST return true only for application/json.

Return values
bool

Description

true if JSON type, false otherwise

isMultipart()

Public

Determines whether the content type represents multipart data.

public isMultipart() : bool

Description

Multipart types are typically used for form uploads and MUST begin with "multipart/". This method SHALL match MIME type prefixes accordingly.

Return values
bool

Description

true if multipart type, false otherwise

isText()

Public

Determines whether the content type represents text-based content.

public isText() : bool

Description

Any MIME type beginning with "text/" SHALL be treated as text content. This method MUST use string prefix evaluation according to the enum value.

Return values
bool

Description

true if text-based type, false otherwise

isXml()

Public

Determines whether the content type represents XML data.

public isXml() : bool

Description

This method SHALL consider both application/xml and text/xml as valid XML content types. It MUST return true for either enumeration case.

Return values
bool

Description

true if XML type, false otherwise

withCharset()

Public

Returns the Content-Type header value with an appended charset parameter.

public withCharset( string  $charset) : string

Description

The returned string MUST follow the standard "type/subtype; charset=X" format. Implementations SHOULD ensure the provided charset is valid according to application requirements.

Parameters
$charset : string

Description

the charset to append to the Content-Type

Return values
string

Description

the resulting Content-Type header value