Skip to content

neoncitylights/php-media-type

Repository files navigation

MediaType

Packagist Version GitHub Build Status Code coverage

MediaType is a PHP library for parsing and serializing MIME types, also known as IANA media types.

This library is compliant to the WHATWG Mime Sniffing Standard.

Install

System requirements:

composer require neoncitylights/media-type

Usage

Parsing

<?php

use Neoncitylights\MediaType\MediaType;
use Neoncitylights\MediaType\MediaTypeParser;

$parser = new MediaTypeParser();
$mediaType = $parser->parseOrNull( 'text/plain;charset=UTF-8' );

print( $mediaType->type ); // 'text'
print( $mediaType->subType ); // 'plain'
print( $mediaType->getEssence() ); // 'text/plain'
print( $mediaType->getParameterValue( 'charset' ) ); // 'UTF-8'

Serializing

<?php

use Neoncitylights\MediaType\MediaType;

$mediaType1 = new MediaType( 'text', 'plain', [ 'charset' => 'UT-8' ] );
$mediaType1->toString(); // 'text/plain;charset=UTF-8'

$mediaType2 = new MediaType( 'application', 'json', [] );
$mediaType2->toString(); // 'application/json'

Matching

<?php

use Neoncitylights\MediaType\MediaType;
use Neoncitylights\MediaType\MediaTypeParser;

$parser = new MediaTypeParser();

$parser->parseOrNull( 'audio/midi' )->isAudioOrVideo(); // true
$parser->parseOrNull( 'audio/ogg' )->isAudioOrVideo(); // true
$parser->parseOrNull( 'application/ogg' )->isAudioOrVideo(); // true

License

This software is licensed under the MIT license (LICENSE-MIT or https://opensource.org/license/mit/).

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the MIT license, shall be licensed as above, without any additional terms or conditions.