chubbyphp / chubbyphp-negotiation
Chubbyphp Negotiation
Installs: 77 801
Dependents: 2
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 3
Open Issues: 0
Requires
- php: ^8.0
- psr/http-message: ^1.0
Requires (Dev)
- chubbyphp/chubbyphp-container: ^2.1
- chubbyphp/chubbyphp-dev-helper: dev-master
- chubbyphp/chubbyphp-laminas-config-factory: ^1.2
- chubbyphp/chubbyphp-mock: ^1.6.1
- infection/infection: ^0.26.5
- php-coveralls/php-coveralls: ^2.5.2
- phploc/phploc: ^7.0.2
- phpstan/extension-installer: ^1.1
- phpstan/phpstan: ^1.4.8
- phpunit/phpunit: ^9.5.17
- pimple/pimple: ^3.5
README
Description
A simple negotiation library.
Requirements
- php: ^7.4|^8.0
- psr/http-message: ^1.0
Suggest
- chubbyphp/chubbyphp-container: ^2.1
- pimple/pimple: ^3.5
Installation
Through Composer as chubbyphp/chubbyphp-negotiation.
composer require chubbyphp/chubbyphp-negotiation "^1.9"
Usage
AcceptLanguageNegotiator
<?php use Chubbyphp\Negotiation\AcceptLanguageNegotiator; $request = ...; $request->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']
AcceptNegotiator
<?php use Chubbyphp\Negotiation\AcceptNegotiator; $request = ...; $request->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']
ContentTypeNegotiator
<?php use Chubbyphp\Negotiation\ContentTypeNegotiator; $request = ...; $request->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']
NegotiationServiceFactory
<?php use Chubbyphp\Container\Container; use Chubbyphp\Negotiation\ServiceFactory\NegotiationServiceFactory; use Psr\Http\Message\ServerRequestInterface; $container = new Container(); $container->factories((new NegotiationServiceFactory())()); $request = ...; $container->get('negotiator.acceptNegotiator') ->negotiate($request); $container->get('negotiator.acceptNegotiator') ->negotiate($request); $container->get('negotiator.contentTypeNegotiator') ->negotiate($request);
NegotiationServiceProvider
<?php use Chubbyphp\Negotiation\ServiceProvider\NegotiationServiceProvider; use Pimple\Container; use Psr\Http\Message\ServerRequestInterface; $container = new Container(); $container->register(new NegotiationServiceProvider); $request = ...; $container['negotiator.acceptNegotiator'] ->negotiate($request); $container['negotiator.acceptNegotiator'] ->negotiate($request); $container['negotiator.contentTypeNegotiator'] ->negotiate($request);
ServiceFactory
Copyright
2023 Dominik Zogg