PHP code example of chubbyphp / chubbyphp-negotiation

1. Go to this page and download the library: Download chubbyphp/chubbyphp-negotiation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

chubbyphp / chubbyphp-negotiation example snippets




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']



use Chubbyphp\Negotiation\Middleware\AcceptLanguageMiddleware;

$request = ...;
$request->withHeader('Accept-Language', 'de,en-US;q=0.7,en;q=0.3');

$middleware = new AcceptLanguageMiddleware($acceptLanguageNegotiator);
$response = $negotiator->process($request, $handler);



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']



use Chubbyphp\Negotiation\Middleware\AcceptMiddleware;

$request = ...;
$request->withHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q =0.8');

$middleware = new AcceptMiddleware($acceptNegotiator);
$response = $negotiator->process($request, $handler);



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']



use Chubbyphp\Negotiation\Middleware\ContentTypeMiddleware;

$request = ...;
$request->withHeader('Content-Type', 'application/xml; charset=UTF-8');

$middleware = new ContentTypeMiddleware($contentTypeNegotiator);
$response = $negotiator->process($request, $handler);



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.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);



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.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);