2017 © Pedro Peláez
 

library psr7-conneg

PSR7 content negotiation

image

ptlis/psr7-conneg

PSR7 content negotiation

  • Tuesday, June 16, 2015
  • by ptlis
  • Repository
  • 1 Watchers
  • 15 Stars
  • 26 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

PSR-7 ConNeg

A content negotiation middleware that uses the PSR-7 interfaces., (*1)

Configurable component that decorate processed requests with preferred type information., (*2)

Built upon ptlis/ConNeg., (*3)

Build Status Code Coverage Scrutinizer Code Quality License Latest Stable Version, (*4)

Install

Either from the console:, (*5)

    $ composer require ptlis/psr7-conneg:~1.2

Or by Editing composer.json:, (*6)

    {
        "require": {
            ...
            "ptlis/conneg-psr7-conneg": "~1.2",
            ...
        }
    }

Followed by a composer update:, (*7)

    $ composer update

Usage

The package ships with a single class to provide negotiation., (*8)

    use ptlis\Psr7ConNeg\Negotiator;

    $negotiator = new Negotiator();

To opt-in to negotiation on a field use the appropriate with* method (note these methods return a new instance in the manner of the PSR-7 interfaces)., (*9)

To negotiate the preferred mime-type use the withMime method, providing it with a list of your application's type preference:, (*10)

    $negotiator = $negotiator->withMime('application/json;q=1.0,text/xml;q=0.7');

With your negotiator configured you can now perform negotiation:, (*11)

    $request = $negotiator->negotiate($request);

This adds attributes to the request containing the preferred type. These can be accessed with the appropriate getters, in the above example of negotiation on the Accept field this looks like:, (*12)

    $mime = $newRequest->getAttribute(Negotiator::MIME_BEST);

If the Accept field of the request contained application/json,text/xml then the value returned from this lookup would be application/json., (*13)

With Zend-Stratigility

To use the zend-stratigility component, configure your negotiator instance as described above and pass it to the Stratigility negotiator., (*14)

    $stratigilityNegotiator = new StratigilityNegotiator($negotiator);

To perform negotiation on all routes, add it with an empty route:, (*15)

    $app->pipe('', $stratigilityNegotiator);

For further information please refer to the Stratigility documentation., (*16)

With Relay

To use the relay component, configure your negotiator instance as described above and pass it to the Relay negotiator., (*17)

    $queue[] = new RelayNegotiator($negotiator);

Note: The negotiator must be earlier in the queue than middleware components that depend upon negotiation data., (*18)

Integration

Middlewares integration is shipped for the following packages:, (*19)

Contributing

You can contribute by submitting an Issue to the issue tracker or submitting a pull request., (*20)

The Versions

16/06 2015

dev-master

9999999-dev

PSR7 content negotiation

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 mime language types http psr7 content accept negotiation field parse encoding header http-message charset accept-language accept-charset accept-encoding content-type

16/06 2015

v1.2.0

1.2.0.0

PSR7 content negotiation

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 mime language types http psr7 content accept negotiation field parse encoding header http-message charset accept-language accept-charset accept-encoding content-type

16/06 2015

v1.1.0

1.1.0.0

PSR7 content negotiation

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 mime language types http psr7 content accept negotiation field parse encoding header http-message charset accept-language accept-charset accept-encoding content-type

21/05 2015

v1.0.0

1.0.0.0

PSR7 content negotiation

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 mime language types http psr7 content accept negotiation field parse encoding header http-message charset accept-language accept-charset accept-encoding content-type

12/05 2015

v0.1.0

0.1.0.0

PSR7 content negotiation

  Sources   Download

MIT

The Requires

 

The Development Requires

psr-7 mime language types http psr7 content accept negotiation field parse encoding header http-message charset accept-language accept-charset accept-encoding content-type