willdurand/negotiation
Composer 安装命令:
composer require willdurand/negotiation
包简介
Content Negotiation tools for PHP provided as a standalone library.
README 文档
README
Negotiation is a standalone library without any dependencies that allows you to implement content negotiation in your application, whatever framework you use. This library is based on RFC 7231. Negotiation is easy to use, and extensively unit tested!
Important: You are browsing the documentation of Negotiation 3.x+.
Documentation for version 1.x is available here: Negotiation 1.x documentation.
Documentation for version 2.x is available here: Negotiation 2.x documentation.
Installation
The recommended way to install Negotiation is through Composer:
$ composer require willdurand/negotiation
Usage Examples
Media Type 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'
The Negotiator returns an instance of Accept, or null if negotiating the
best media type has failed.
Language Negotiation
<?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
The LanguageNegotiator returns an instance of AcceptLanguage.
Encoding Negotiation
<?php $negotiator = new \Negotiation\EncodingNegotiator(); $encoding = $negotiator->getBest($acceptHeader, $priorities);
The EncodingNegotiator returns an instance of AcceptEncoding.
Charset Negotiation
<?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
The CharsetNegotiator returns an instance of AcceptCharset.
Accept* Classes
Accept and Accept* classes share common methods such as:
getValue()returns the accept value (e.g.text/html; z=y; a=b; c=d)getNormalizedValue()returns the value with parameters sorted (e.g.text/html; a=b; c=d; z=y)getQuality()returns the quality if available (qparameter)getType()returns the accept type (e.g.text/html)getParameters()returns the set of parameters (excluding theqparameter if provided)getParameter()allows to retrieve a given parameter by its name. Fallback to a$default(nullable) value otherwise.hasParameter()indicates whether a parameter exists.
Versioning
Negotiation follows Semantic Versioning.
End Of Life
1.x
As of October 2016, branch
1.x is not supported
anymore, meaning major version 1 reached end of life. Last version is:
1.5.0.
2.x
As of November 2020, branch
2.x is not supported
anymore, meaning major version 2 reached end of life. Last version is:
2.3.1.
Stable Version
3.x (and dev-master)
Negotiation 3.0
has been released on November 26th, 2020. This is the current stable version
and it is in sync with the main branch (a.k.a. master).
Unit Tests
Setup the test suite using Composer:
$ composer install --dev
Run it using PHPUnit:
$ phpunit
Contributing
See CONTRIBUTING file.
Credits
-
Some parts of this library are inspired by:
- Symfony framework;
- FOSRest;
- PEAR HTTP2.
License
Negotiation is released under the MIT License. See the bundled LICENSE file for details.
willdurand/negotiation 适用场景与选型建议
willdurand/negotiation 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 128.75M 次下载、GitHub Stars 达 1.42k, 最近一次更新时间为 2013 年 01 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「format」 「header」 「content」 「accept」 「negotiation」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 willdurand/negotiation 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 willdurand/negotiation 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 willdurand/negotiation 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides a simple decimal/roman number converter
Softonic OpenApi validation formats extension
A block that displays featured content - large image, title, description and link.
Tools to convert between .NET and PHP date formats
Adds the EDTF data type to Wikibase
TYPO3 CMS extension to create gallery content element with preset crop ratios and pagination
统计信息
- 总下载量: 128.75M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1434
- 点击次数: 30
- 依赖项目数: 182
- 推荐数: 3
其他信息
- 授权协议: MIT
- 更新时间: 2013-01-18