keven/media-type
最新稳定版本:1.0.1
Composer 安装命令:
composer require keven/media-type
包简介
MediaType manipulation and validation
README 文档
README
Simple RFC 6838 media type manipulation and validation library.
This module will parse a given media type into its component parts, like type, subtype, and suffix. A formatter is also provided to put them back together and the two can be combined to normalize media types into a canonical form.
Installation
$ composer install keven/media-type
API
use Keven\MediaType\MediaType;
Parsing
$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"
Parse a media type string. This will return an object with the following
properties (examples are shown for the string 'image/vnd.svg+xml; charset=utf-8'):
-
type: The type of the media type (always lower case). Example:'image' -
subtype: The subtype of the media type (always lower case). Example:'svg' -
tree: The tree (~vendor) of the media type (always lower case). Example:'vnd' -
suffix: The suffix of the media type (always lower case). Example:'xml' -
parameters: The parameters added to the end of the media type. Exemple:['charset' => 'utf-8'] -
If the given type string is invalid, then a
InvalidMediaTypeExceptionis thrown.
Formatting
$mediaType = new MediaType('image', 'svg', 'vnd', 'xml'); echo (string) $mediaType; // "image/vnd.svg+xml"
Format an object into a media type string. This will return a string of the mime type for the given object.
Validation
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
Validate a media type string. This will return true if the string is a well-
formatted media type, or false otherwise.
You can also assert if the type is valid by passing a second parameter.
License
统计信息
- 总下载量: 459
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-10-16