ContentType
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.
Cases
Methods
Creates a ContentType instance from a full Content-Type header string.
Extracts the charset parameter from a Content-Type header string.
Determines whether the content type represents JSON data.
Determines whether the content type represents multipart data.
Determines whether the content type represents text-based content.
Determines whether the content type represents XML data.
Returns the Content-Type header value with an appended charset parameter.
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
Description
the derived ContentType case or null if unsupported
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
Description
the charset value or null if absent
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
Description
true if JSON type, false otherwise
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
Description
true if multipart type, false otherwise
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
Description
true if text-based type, false otherwise
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
Description
true if XML type, false otherwise
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
Description
the resulting Content-Type header value