axy/mime

Mime-type matching

1.0.4 2017-12-31 14:23 UTC

This package is auto-updated.

Last update: 2024-10-08 02:08:43 UTC


README

Mime-type matching.

Latest Stable Version Minimum PHP Version Build Status Coverage Status License

  • The library does not require any dependencies.
  • Install: composer require axy/mime.
  • License: MIT.

For PHP 5.4+ support see branch php54 in this repo or version 0.x of the composer package.

Documentation

Mime-type has the follow format: type/subtype. For example: image/png. It is can be case-insensitive: Image/PNG.

Pattern can be in the follow formats:

  • image/png
  • image/png,image/jpeg, image/gif - a list of allowed types (comma-separated)
  • image/* - all subtype of a type
  • image/*,text/plain - a list contains a mask

Matching

use axy\mime\MimeType;

$type = 'image/png';
$pattern = 'image/*';

MimeType::match($type, $pattern); // TRUE

Type and pattern can be strings or MimeType and MimePattern instances.

MimeType class

use axy\mime\MimeType;

$type = new MimeType('Image/PNG');

echo $type->getMimeType(); // image/png
echo $type->getType(); // image
echo $type->getSubtype(); // png
echo $type->isType('image'); // TRUE
echo $type->isType(MimeType::AUDIO); // FALSE

The class has the constants list for common types: APPLICATION, AUDIO, EXAMPLE, IMAGE, MESSAGE, MODEL, MULTIPART, TEXT and VIDEO.

Matching:

$type = new MimeType('image/png');

$type->match('image/jpeg'); // FALSE
$type->match('image/*); // TRUE

$type('image/png'); // __invoke()

$type($instanceOfMimePattern); // see MimePattern

MimePattern class

use axy\mime\MimePattern;

$pattern = new MimePattern('IMAGE/*');

$pattern->getPattern(); // image/*

$pattern->match('image/png'); // TRUE
$pattern('image/jpeg'); // __invoke
$pattern($instanceOfMimeType);