Newer
Older
Klaranouba7
a validé
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Negotiation
===========
[](http://travis-ci.org/willdurand/Negotiation)
[](https://ci.appveyor.com/project/willdurand/negotiation)
[](https://packagist.org/packages/willdurand/Negotiation)
[](https://packagist.org/packages/willdurand/Negotiation)
**Negotiation** is a standalone library without any dependencies that allows you
to implement [content
negotiation](https://tools.ietf.org/html/rfc7231#section-5.3) in your
application, whatever framework you use. This library is based on [RFC
7231](https://tools.ietf.org/html/rfc7231). Negotiation is easy to use, and
extensively unit tested!
> **Important:** You are browsing the documentation of Negotiation **2.x**+.
Documentation for version **1.x** is available here: [Negotiation 1.x
documentation](https://github.com/willdurand/Negotiation/blob/1.x/README.md#usage).
You might also be interested in this: [**What's new in Negotiation 2?**](https://github.com/willdurand/Negotiation/releases/tag/v2.0.0-alpha1)
Installation
------------
The recommended way to install Negotiation is through
[Composer](http://getcomposer.org/):
```bash
$ composer require willdurand/negotiation
```
Usage Examples
--------------
### Media Type Negotiation
``` php
$negotiator = new \Negotiation\Negotiator();
$acceptHeader = 'text/html, application/xhtml+xml, application/xml;q=0.9, */*;q=0.8';
$priorities = array('text/html; charset=UTF-8', 'application/json', 'application/xml;q=0.5');
$mediaType = $negotiator->getBest($acceptHeader, $priorities);
$value = $mediaType->getValue();
// $value == 'text/html; charset=UTF-8'
```
The `Negotiator` returns an instance of `Accept`, or `null` if negotiating the
best media type has failed.
### Language Negotiation
``` php
<?php
$negotiator = new \Negotiation\LanguageNegotiator();
$acceptLanguageHeader = 'en; q=0.1, fr; q=0.4, fu; q=0.9, de; q=0.2';
$priorities = array('de', 'fu', 'en');
$bestLanguage = $negotiator->getBest($acceptLanguageHeader, $priorities);
$type = $bestLanguage->getType();
// $type == 'fu';
$quality = $bestLanguage->getQuality();
// $quality == 0.9
```
The `LanguageNegotiator` returns an instance of `AcceptLanguage`.
### Encoding Negotiation
``` php
<?php
$negotiator = new \Negotiation\EncodingNegotiator();
$encoding = $negotiator->getBest($acceptHeader, $priorities);
```
The `EncodingNegotiator` returns an instance of `AcceptEncoding`.
### Charset Negotiation
``` php
<?php
$negotiator = new \Negotiation\CharsetNegotiator();
$acceptCharsetHeader = 'ISO-8859-1, UTF-8; q=0.9';
$priorities = array('iso-8859-1;q=0.3', 'utf-8;q=0.9', 'utf-16;q=1.0');
$bestCharset = $negotiator->getBest($acceptCharsetHeader, $priorities);
$type = $bestCharset->getType();
// $type == 'utf-8';
$quality = $bestCharset->getQuality();
// $quality == 0.81
```
The `CharsetNegotiator` returns an instance of `AcceptCharset`.
### `Accept*` Classes
`Accept` and `Accept*` classes share common methods such as:
* `getValue()` returns the accept value (e.g. `text/html; z=y; a=b; c=d`)
* `getNormalizedValue()` returns the value with parameters sorted (e.g.
`text/html; a=b; c=d; z=y`)
* `getQuality()` returns the quality if available (`q` parameter)
* `getType()` returns the accept type (e.g. `text/html`)
* `getParameters()` returns the set of parameters (excluding the `q` parameter
if provided)
* `getParameter()` allows to retrieve a given parameter by its name. Fallback to
a `$default` (nullable) value otherwise.
* `hasParameter()` indicates whether a parameter exists.
Versioning
----------
Negotiation follows [Semantic Versioning](http://semver.org/).
### End Of Life
#### 1.x
As of October 2016, [branch
`1.x`](https://github.com/willdurand/Negotiation/tree/1.x) is not supported
anymore, meaning major version `1` reached end of life. Last version is:
[1.5.0](https://github.com/willdurand/Negotiation/releases/tag/1.5.0).
### Stable Version
#### 2.x
Negotiation [2.0](https://github.com/willdurand/Negotiation/releases/tag/v2.0.0)
has been released on October 1st, 2015. It is the **current stable version**.
The [`2.x` branch](https://github.com/willdurand/Negotiation/tree/2.x) is used
to maintain this version.
### `dev-master`
#### 3.x
Version `3.x` is the next major version of Negotiation. This version lives in
the `master` branch, and should not be used in production yet (even if we try
to keep its state as stable as we can).
Unit Tests
----------
Setup the test suite using Composer:
$ composer install --dev
Run it using PHPUnit:
$ phpunit
Contributing
------------
See [CONTRIBUTING](CONTRIBUTING.md) file.
Credits
-------
* Some parts of this library are inspired by:
* [Symfony](http://github.com/symfony/symfony) framework;
* [FOSRest](http://github.com/FriendsOfSymfony/FOSRest);
* [PEAR HTTP2](https://github.com/pear/HTTP2).
* William Durand <will+git@drnd.me>
* [@neural-wetware](https://github.com/neural-wetware)
License
-------
Negotiation is released under the MIT License. See the bundled LICENSE file for
details.