neoncitylights / media-type
Allows working with IANA media types as entities in PHP
Fund package maintenance!
neoncitylights
Installs: 9 566
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 2
Requires
- php: >=8.2
- ext-intl: *
- wikimedia/assert: ^0.5.1
Requires (Dev)
- mediawiki/mediawiki-codesniffer: 43.0.0
- mediawiki/minus-x: 1.1.1
- php-parallel-lint/php-console-highlighter: 1.0.0
- php-parallel-lint/php-parallel-lint: 1.4.0
- phpunit/phpunit: 11.0.8
This package is auto-updated.
Last update: 2024-10-22 08:46:31 UTC
README
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:
- Minimum PHP version: 8.2
intl
PHP extension (installation instructions)
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.