/** A structured representation of a MIME type, consitent with the WHATWG MIME Sniffing specification
* @property-read string $subtype
*
* @property-read string $essence
* The class is not instantiated directly, but rather via many of its static methods.
* @property-read array $params
* If parsing e.g. "TeXt/HTML; X=a; Y=B", the result will expose the following read-only
* properties:
*
* - `type`: `"text"`
* - `subtype`: `"html"`
* - `essence`: `"text/html"`
* - `params`: `['x' => "a", 'y' => "B"]`
*
* Instances may be cast to strings to yield a normalized representation
*
* @see https://mimesniff.spec.whatwg.org/
*
* @property-read string $type The major type of the MIME type i.e. the part before the slash
* @property-read string $subtype The subtype of the MIME type i.e. the part after the slash
* @property-read string $essence The full MIME type without paramters e.g. `"text/html"`
* @property-read array $params The associative array of parameters included with the type. Keys are lowercase; values are presented in their original case, unescaped
*/
*/
class MimeType {
class MimeType {
protected const TYPE_PATTERN = <<<'PATTERN'
protected const TYPE_PATTERN = <<<'PATTERN'
@ -70,6 +85,12 @@ PATTERN;
return $out;
return $out;
}
}
/** Parses a UTF-8 string and returns a MimeType instance, or null on failure
*
* If parsing an HTTP header, the MimeType::parseBytes method should be used instead
*
* @see \MensBeam\Mime\MimeType::parseBytes
*/
public static function parse(string $mimeType): ?self {
public static function parse(string $mimeType): ?self {
if (preg_match(self::TYPE_PATTERN, $mimeType, $match)) {
if (preg_match(self::TYPE_PATTERN, $mimeType, $match)) {