datingvip/negotiation

PHP独立库提供的内容协商工具。

3.0.1 2022-01-24 09:46 UTC

README

Build Status Build status Total Downloads Latest Stable Version

Negotiation是一个无任何依赖的独立库,允许您在应用程序中实现内容协商,无论您使用什么框架。此库基于RFC 7231。Negotiation易于使用,并且经过了广泛的单元测试!

重要:您正在浏览Negotiation 3.x+版本的文档。

1.x版本的文档在此处可用:Negotiation 1.x文档

2.x版本的文档在此处可用:Negotiation 2.x文档

安装

安装Negotiation的推荐方式是通过Composer

$ composer require willdurand/negotiation

使用示例

媒体类型协商

$negotiator = new \Negotiation\Negotiator();

$acceptHeader = 'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8';
$priorities   = array('text/html; charset=UTF-8', 'application/json', 'application/xml;q=0.5');

$mediaType = $negotiator->getBest($acceptHeader, $priorities);

$value = $mediaType->getValue();
// $value == 'text/html; charset=UTF-8'

Negotiator返回一个Accept实例,或者在协商最佳媒体类型失败时返回null

语言协商

<?php

$negotiator = new \Negotiation\LanguageNegotiator();

$acceptLanguageHeader = 'en; q=0.1, fr; q=0.4, fu; q=0.9, de; q=0.2';
$priorities          = array('de', 'fu', 'en');

$bestLanguage = $negotiator->getBest($acceptLanguageHeader, $priorities);

$type = $bestLanguage->getType();
// $type == 'fu';

$quality = $bestLanguage->getQuality();
// $quality == 0.9

LanguageNegotiator返回一个AcceptLanguage实例。

编码协商

<?php

$negotiator = new \Negotiation\EncodingNegotiator();
$encoding   = $negotiator->getBest($acceptHeader, $priorities);

EncodingNegotiator返回一个AcceptEncoding实例。

字符集协商

<?php

$negotiator = new \Negotiation\CharsetNegotiator();

$acceptCharsetHeader = 'ISO-8859-1, UTF-8; q=0.9';
$priorities          = array('iso-8859-1;q=0.3', 'utf-8;q=0.9', 'utf-16;q=1.0');

$bestCharset = $negotiator->getBest($acceptCharsetHeader, $priorities);

$type = $bestCharset->getType();
// $type == 'utf-8';

$quality = $bestCharset->getQuality();
// $quality == 0.81

CharsetNegotiator返回一个AcceptCharset实例。

Accept*

AcceptAccept*类共享一些常用方法,例如

  • getValue()返回接受值(例如text/html; z=y; a=b; c=d
  • getNormalizedValue()返回排序后的值(例如text/html; a=b; c=d; z=y
  • getQuality()如果可用则返回质量(q参数)
  • getType()返回接受类型(例如text/html
  • getParameters()返回参数集(如果提供了q参数,则不包括该参数)
  • getParameter()允许通过其名称检索给定参数。否则回退到$default(可空)值。
  • hasParameter()指示是否存在参数。

版本控制

Negotiation遵循语义版本控制

生命周期结束

1.x

截至2016年10月,分支1.x不再受支持,这意味着主要版本1已达到生命周期结束。最后版本是:1.5.0

2.x

截至2020年11月,分支2.x不再受支持,这意味着主要版本2已达到生命周期结束。最后版本是:2.3.1

稳定版本

3.x(和dev-master

Negotiation 3.0已于2020年11月26日发布。这是当前稳定版本,它与主分支(即master)同步。

单元测试

使用Composer设置测试套件

$ composer install --dev

使用PHPUnit运行它

$ phpunit

贡献

查看CONTRIBUTING文件。

致谢

许可证

协商根据MIT许可证发布。有关详细信息,请参阅附带的LICENSE文件。