https://github.com/chubbyphp/chubbyphp-negotiation
A simple negotiation library.
https://github.com/chubbyphp/chubbyphp-negotiation
accept accept-language content-negotiation content-type-based-versioning negotiation
Last synced: 22 days ago
JSON representation
A simple negotiation library.
- Host: GitHub
- URL: https://github.com/chubbyphp/chubbyphp-negotiation
- Owner: chubbyphp
- License: mit
- Created: 2017-09-03T12:11:55.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2025-03-12T14:09:19.000Z (4 months ago)
- Last Synced: 2025-05-25T04:07:06.421Z (about 1 month ago)
- Topics: accept, accept-language, content-negotiation, content-type-based-versioning, negotiation
- Language: PHP
- Homepage:
- Size: 135 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# chubbyphp-negotiation
[](https://github.com/chubbyphp/chubbyphp-negotiation/actions/workflows/ci.yml)
[](https://coveralls.io/github/chubbyphp/chubbyphp-negotiation?branch=master)
[](https://dashboard.stryker-mutator.io/reports/github.com/chubbyphp/chubbyphp-negotiation/master)
[](https://packagist.org/packages/chubbyphp/chubbyphp-negotiation)
[](https://packagist.org/packages/chubbyphp/chubbyphp-negotiation)
[](https://packagist.org/packages/chubbyphp/chubbyphp-negotiation)[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)
[](https://sonarcloud.io/dashboard?id=chubbyphp_chubbyphp-negotiation)## Description
A simple negotiation library.
## Requirements
* php: ^8.2
* psr/http-message: ^1.1|^2.0## Suggest
* chubbyphp/chubbyphp-container: ^2.2
* chubbyphp/chubbyphp-http-exception: ^1.1
* chubbyphp/chubbyphp-laminas-config-factory: ^1.3
* pimple/pimple: ^3.5
* psr/http-server-middleware: ^1.0.2## Installation
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-negotiation][1].
```sh
composer require chubbyphp/chubbyphp-negotiation "^2.2"
```## Usage
### AcceptLanguageNegotiator
```php
withHeader('Accept-Language', 'de,en-US;q=0.7,en;q=0.3');$negotiator = new AcceptLanguageNegotiator(['en', 'de']);
$value = $negotiator->negotiate($request); // NegotiatedValue
$value->getValue(); // de
$value->getAttributes(); // ['q' => '1.0']
```### AcceptLanguageMiddleware
```php
withHeader('Accept-Language', 'de,en-US;q=0.7,en;q=0.3');$middleware = new AcceptLanguageMiddleware($acceptLanguageNegotiator);
$response = $negotiator->process($request, $handler);
```### AcceptNegotiator
```php
withHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q =0.8');$negotiator = new AcceptNegotiator(['application/json', 'application/xml', 'application/x-yaml']);
$value = $negotiator->negotiate($request); // NegotiatedValue
$value->getValue(); // application/xml
$value->getAttributes(); // ['q' => '0.9']
```### AcceptMiddleware
```php
withHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q =0.8');$middleware = new AcceptMiddleware($acceptNegotiator);
$response = $negotiator->process($request, $handler);
```### ContentTypeNegotiator
```php
withHeader('Content-Type', 'application/xml; charset=UTF-8');$negotiator = new ContentTypeNegotiator(['application/json', 'application/xml', 'application/x-yaml']);
$value = $negotiator->negotiate($request); // NegotiatedValue
$value->getValue(); // application/xml
$value->getAttributes(); // ['charset' => 'UTF-8']
```### ContentTypeMiddleware
```php
withHeader('Content-Type', 'application/xml; charset=UTF-8');$middleware = new ContentTypeMiddleware($contentTypeNegotiator);
$response = $negotiator->process($request, $handler);
```### NegotiationServiceFactory
```php
factories((new NegotiationServiceFactory())());$request = ...;
$container->get('negotiator.acceptNegotiator')
->negotiate($request);$container->get('negotiator.acceptMiddleware')
->process($request, $handler);$container->get('negotiator.acceptLanguageNegotiator')
->negotiate($request);$container->get('negotiator.acceptLanguageMiddleware')
->process($request, $handler);$container->get('negotiator.contentTypeNegotiator')
->negotiate($request);$container->get('negotiator.contentTypeMiddleware')
->process($request, $handler);
```### NegotiationServiceProvider
```php
register(new NegotiationServiceProvider);$request = ...;
$container['negotiator.acceptNegotiator']
->negotiate($request);$container['negotiator.acceptMiddleware']
->process($request, $handler);$container['negotiator.acceptLanguageNegotiator']
->negotiate($request);$container['negotiator.acceptLanguageMiddleware']
->process($request, $handler);$container['negotiator.contentTypeNegotiator']
->negotiate($request);$container['negotiator.contentTypeMiddleware']
->process($request, $handler);
```### ServiceFactory
* [AcceptLanguageMiddlewareFactory][2]
* [AcceptLanguageNegotiatorFactory][3]
* [AcceptMiddlewareFactory][4]
* [AcceptNegotiatorFactory][5]
* [ContentTypeMiddlewareFactory][6]
* [ContentTypeNegotiatorFactory][7]## Copyright
2025 Dominik Zogg
[1]: https://packagist.org/packages/chubbyphp/chubbyphp-negotiation
[2]: doc/ServiceFactory/AcceptLanguageMiddlewareFactory.md
[3]: doc/ServiceFactory/AcceptLanguageNegotiatorFactory.md
[4]: doc/ServiceFactory/AcceptMiddlewareFactory.md
[5]: doc/ServiceFactory/AcceptNegotiatorFactory.md
[6]: doc/ServiceFactory/ContentTypeMiddlewareFactory.md
[7]: doc/ServiceFactory/ContentTypeNegotiatorFactory.md