2017 © Pedro Peláez
 

project php-negotiator

Minimal PHP content negotiation library

image

43081j/php-negotiator

Minimal PHP content negotiation library

  • Monday, October 21, 2013
  • by 43081j
  • Repository
  • 2 Watchers
  • 4 Stars
  • 304 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

PHP Negotiator

A minimal PHP content negotiation library to process common headers such as media types, encodings, charsets and so on., (*1)

Quite a few concepts are taken from Negotiator., (*2)

Accept (Media Types)

require 'vendor/autoload.php';

$negotiator = new Negotiator\Parser([
    'accept-charset' => 'utf-8, iso-8859-1;q=0.8, utf-7;q=0.2',
    'accept' => 'text/html, application/*;q=0.2, image/jpeg;q=0.8',
    'accept-language' => 'en;q=0.8, es, pt',
    'accept-encoding' => 'gzip, compress;q=0.2, identity;q=0.5',
]);

$available = ['text/html', 'text/plain', 'application/json'];

$negotiator->preferredMediaTypes();
// ['text/html', 'image/jpeg', 'application/*']

$negotiator->preferredMediaTypes($available);
// ['text/html', 'application/json']

$negotiator->preferredMediaType($available);
// 'text/html'

Do note that you must retrieve the headers yourself and standardise the keys to lowercase representations (e.g. 'accept' vs. 'Accept')., (*3)

Methods

preferredMediaTypes($available), (*4)

Returns an array of preferred media types ordered by priority and optionally selected from a set of available types., (*5)

preferredMediaType($available), (*6)

Returns a string of the highest priority media type preferred, optionally selected from a set of available types., (*7)

preferredLanguages($available), (*8)

Returns an array of preferred languages ordered by priority and optionally selected from a set of available languages., (*9)

preferredLanguage($available), (*10)

Returns a string of the highest priority language preferred, optionally selected from a set of available languages., (*11)

preferredCharsets($available), (*12)

Returns an array of preferred character sets ordered by priority and optionally selected from a set of available character sets., (*13)

preferredCharset($available), (*14)

Returns a string of the highest priority character set preferred, optionally selected from a set of available character sets., (*15)

preferredEncodings($available), (*16)

Returns an array of preferred encodings ordered by priority and optionally selected from a set of available encodings., (*17)

preferredEncoding($available), (*18)

Returns a string of the highest priority encoding preferred, optionally selected from a set of available encodings., (*19)

The Versions

21/10 2013

dev-master

9999999-dev

Minimal PHP content negotiation library

  Sources   Download

Apache-2.0

The Requires

  • php >=5.3.3

 

content accept negotiation