定制 keven/media-type 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

keven/media-type

最新稳定版本:1.0.1

Composer 安装命令:

composer require keven/media-type

包简介

MediaType manipulation and validation

README 文档

README

Latest Stable Version Latest Unstable Version Total Downloads CI License

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 InvalidMediaTypeException is 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

MIT

统计信息

  • 总下载量: 459
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-10-16