PHP code example of keven / media-type

1. Go to this page and download the library: Download keven/media-type library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

keven / media-type example snippets


use Keven\MediaType\MediaType;

$mediaType = MediaType::create('application/api+json');
$mediaType->getType(); // "application"
$mediaType->getTree(); // "vnd"
$mediaType->getSubtype(); // "api"
$mediaType->getsuffix(); // "json"
$mediaType->getParameters(); // ["charset" => "utf-8"]
$mediaType->hasParameter('charset'); // true
$mediaType->getParameter('charset'); // "utf-8"

$mediaType = new MediaType('image', 'svg', 'vnd', 'xml');
echo (string) $mediaType; // "image/vnd.svg+xml"

MediaType::isValid('app/vnd.api+json; charset=utf-8'); // true (it's well formatted...)
MediaType::isValid('app/vnd.api+json; charset=utf-8', true); // false (...but the type is not a valid IANA type)
MediaType::create('unvalid media type'); // throws InvalidMediaTypeException