borschphp/mimetype

MimeType and MediaType implementation.

1.0.0 2021-11-26 12:04 UTC

This package is auto-updated.

Last update: 2024-10-26 18:54:48 UTC


README

Latest Stable Version License PHP Version Require build Code Coverage Scrutinizer Code Quality

MimeType and MediaType implementation.

Install

This project requires PHP 7.2 or higher.
Via Composer, simply run:

composer require borschphp/mimetype

Basic Usage

Easily create Mime Type for your requests:

use Borsch\MimeType\MimeType;
use Laminas\Diactoros\Request;
use Laminas\Diactoros\Uri;

$mime_type = new MimeType('application', 'json', ['charset' => 'UTF-8']);

$request = (new Request())
    ->withUri(new Uri('http://example.com'))
    ->withMethod('GET')
    ->withAddedHeader('Content-Type', (string)$mime_type);

Or Media Type:

use Borsch\MimeType\MediaType;
use Laminas\Diactoros\Request;
use Laminas\Diactoros\Uri;

$request = (new Request())
    ->withUri(new Uri('http://example.com'))
    ->withMethod('GET')
    ->withAddedHeader('Content-Type', MediaType::APPLICATION_JSON);

Parse Mime Types and get useful data:

use Borsch\MimeType\MimeType;
use Borsch\MimeType\MediaType;

$mime_type = MimeType::createFromString(
    'application/atom+xml;charset=utf-8;boundary=3d6b6a416f9b5;name=some_file'
);

$mime_type->getType(); // application
$mime_type->getSubtype(); // atom+xml
$mime_type->getSubtypeSuffix() // xml
$mime_type->getCharset(); // utf-8
$mime_type->getParameters(); // ['charset' => 'utf-8', 'boundary' => '3d6b6a416f9b5', 'name' => 'some_file']
$mime_type->getParameter('boundary'); // 3d6b6a416f9b5

$media_type = new MediaType('image', 'png', ['q' => 0.8]);
$media_type->getQualityValue(); // 0.8
$media_type->removeQualityValue();
$media_type->getQualityValue(); // null

Contributing

Please see CONTRIBUTING.md for details.

Testing

Made with PHPUnit, simply run:

./vendor/bin/phpunit tests

License

This project is licensed under the MIT license.
See the LICENSE.md file for more details.