dreadlabs / media-type-decoding

媒体类型推断的简单API。

1.0 2018-07-16 21:26 UTC

This package is auto-updated.

Last update: 2024-09-23 23:02:12 UTC


README

描述

这个库提供了一个简单的API,用于媒体类型解码。

它可以用来从如 application/prs.acme.user-created+json; version=1.0 这样的媒体类型字符串字面量中推断PHP类名。

它基于RFC 6838规范。

它是DreadLabs/media-type-encoding的配套库,提供了媒体类型字符串字面量的解码功能。

安装

composer install dreadlabs/media-type-decoding:~1.0

用法

示例1

从位于顶层类型为 Application 和子类型树为 Vendor 的媒体类型字符串字面量中,我想要解析一个完全限定的PHP类名。媒体类型字符串字面量还包含一个 version 参数,没有定义 Suffix

$mediaType = new Application(RegistrationTree::vendor(new UpperCamelCasedFromHyphened(new Imploded('\\'))));
$withParameter = $mediaType->withParameter(new Parameter('version'));
$withSuffix = $withParameter->withSuffix(Suffix::none());

echo (string)$withSuffix->inferred('application/vnd.acme.customer-api.domain.event.item-added-to-cart; version=1.0');

> 'Acme\\CustomerApi\\Domain\\Event\\ItemAddedToCart'

示例2

这个例子是对示例1的增强。媒体类型字符串字面量没有携带所有必要的数据来获取完全限定的PHP类名。因此,我们使用了 Prefixed 子类型推断。

$prefix = ['acme', 'customer-api', 'domain'];
$subtype = new Prefixed(new UpperCamelCasedFromHyphened(new Imploded('\\')), $prefix);
$mediaType = new Application(RegistrationTree::personal($subtype));

echo (string)$mediaType->inferred('application/prs.event.item-removed-from-cart')

> 'Acme\\CustomerApi\\Domain\\Event\\ItemRemovedFromCart'

开发

要求

请阅读贡献指南,并确保您有一个可工作的Docker环境。

设置

按照贡献指南中的说明进行Fork和克隆此仓库。

打开终端并运行设置脚本

script/setup

运行测试

script/console run composer test:unit
script/console run composer test:integration
script/console run composer test:acceptance:fail-fast

链接

许可证

MIT