chubbyphp/chubbyphp-negotiation

Chubbyphp Negotiation

1.2.4 2018-05-03 07:29 UTC

README

Build Status Scrutinizer Code Quality Code Coverage Total Downloads Monthly Downloads Latest Stable Version Latest Unstable Version

Description

A simple negotiation library.

Requirements

  • php: ~7.0
  • psr/http-message: ~1.0

Suggest

  • pimple/pimple: ~3.0

Installation

Through Composer as chubbyphp/chubbyphp-negotiation.

composer require chubbyphp/chubbyphp-negotiation "~1.2"

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

NegotiationProvider

<?php

use Chubbyphp\Negotiation\Provider\NegotiationProvider;
use Pimple\Container;
use Psr\Http\Message\ServerRequestInterface;

$container = new Container();
$container->register(new NegotiationProvider);

$request = ...;

$container['negotiator.acceptNegotiator']
    ->negotiate($request);

$container['negotiator.acceptNegotiator']
    ->negotiate($request);

$container['negotiator.contentTypeNegotiator']
    ->negotiate($request);

Copyright

Dominik Zogg 2017