noresources / mediatype
Media Type parsing and manipulation library
v2.0.0
2024-11-06 19:24 UTC
Requires
- php: >= 7.1
- noresources/http-core: ^2.0
Requires (Dev)
- ext-dom: *
- phpunit/phpunit: >= 5.0
Replaces
This package is auto-updated.
Last update: 2025-04-16 18:05:22 UTC
README
RFC 6838 Media Type (MIME type) parsing and comparison.
Installation
composer require noresources/mediatype
Usage
use NoreSources\MediaType\MediaType; use NoreSources\MediaType\MediaTypeFactory; use NoreSources\MediaType\MediaRange; $factory = MediaTypeFactory::getInstance(); $mediaType = $factory->createFromString('text/vnd.noresources.incredibly.flexible+xml'); var_dump($mediaType->getMainType()); // "text" var_dump($mediaType->getStructuredSyntax()); // "xml" $subType = $mediaType->getSubType(); var_dump(\strval($subType)); // "vnd.noresources.incredibly.flexible+xml" var_dump($subType->getFacets()); // [ "vnd", "noresources", "incredibly", "flexible" ] // From a file or a stream $mediaType = $factory->createFromMedia('path/to/filename.html'); var_dump(\strval($mediaType)); // "text/html" // Media range is also recognized $range = $factory->createFromString('image/*'); // Comparing $html = $factory->createFromString('text/html'); $anyText = $factory->createFromString('text/*'); $any = $factory->createFromString('*/*'); var_dump([ 'text/html vs text/*' => MediaRange::compare($html, $anyText), 'text/* vs */*' => MediaRange::compare($anyText, $any), '*/* vs text/html' => MediaRange::compare($any, $html) ]); /* array(3) { ["text/html vs text/*"]=> int(1) ["text/* vs */*"]=> int(1) ["*/* vs text/html"]=> int(-1) } */