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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
|
<?php /** * * This file is part of Aura for PHP. * * @license http://opensource.org/licenses/bsd-license.php BSD * */ namespace Aura\Accept;
use ArrayIterator; use IteratorAggregate;
/** * * Represents a collection of `Accept*` header values, sorted in quality order. * * @package Aura.Accept * */ abstract class AbstractNegotiator implements IteratorAggregate { /** * * An array of objects representing acceptable values from the header. * * @var array * */ protected $acceptable = array();
/** * * The $_SERVER key to use when populating acceptable values. * * @var string * */ protected $server_key;
/** * * A factory to create value objects. * * @var ValueFactory * */ protected $value_factory;
/** * * The type of value object to create using the ValueFactory. * * @var string * */ protected $value_type;
/** * * Constructor. * * @param ValueFactory $value_factory A factory for value objects. * * @param array $server A copy of $_SERVER for finding acceptable values. * */ public function __construct( ValueFactory $value_factory, array $server = array() ) { $this->value_factory = $value_factory; if (isset($server[$this->server_key])) { $this->set($server[$this->server_key]); } }
/** * * Returns a value object by its sorted quality position. * * @param int $key The sorted position. * * @return Value\AbstractValue * */ public function get($key = null) { if ($key === null) { return $this->acceptable; } return $this->acceptable[$key]; }
/** * * Sets the collection to one or more acceptable values, overwriting all * previous values. * * @param string|array $acceptable An `Accept*` string value; e.g., * `text/plain;q=0.5,text/html,text/*;q=0.1`. * * @return null * */ public function set($acceptable = null) { $this->acceptable = array(); $this->add($acceptable); }
/** * * Adds one or more acceptable values to this collection. * * @param string|array $acceptable One or more `Accept*` string values; * e.g., the string `'text/plain;q=0.5,text/html,text/*;q=0.1'` and * `array('text/plain;q=0.5','text/html','text/*;q=0.1')` are * equivalent. * * @return null * * @todo Allow this to take an array so we can parse-and-sort in one pass. * */ public function add($acceptable = null) { foreach ((array) $acceptable as $string) { $this->parse($string); }
$this->sort(); }
/** * * Parses an acceptable string value into the `$acceptable` property. * * @param string $string An `Accept*` string value; e.g., * `text/plain;q=0.5,text/html,text/*;q=0.1`. * * @return array * */ protected function parse($string) { $acceptable = explode(',', $string);
foreach ($acceptable as $value) { $pairs = explode(';', $value); $value = $pairs[0]; unset($pairs[0]);
$parameters = array(); foreach ($pairs as $pair) { $param = array(); preg_match( '/^(?P<name>.+?)=(?P<quoted>"|\')?(?P<value>.*?)(?:\k<quoted>)?$/', $pair, $param ); $parameters[$param['name']] = $param['value']; }
$quality = 1.0; if (isset($parameters['q'])) { $quality = $parameters['q']; unset($parameters['q']); }
$this->acceptable[] = $this->value_factory->newInstance( $this->value_type, trim($value), (float) $quality, $parameters ); } }
/** * * Sorts the `$acceptable` values according to quality levels. * * This is an unusual sort. Normally we'd think a reverse-sort would * order the array by q values from 1 to 0, but the problem is that * an implicit 1.0 on more than one value means that those values will * be reverse from what the header specifies, which seems unexpected * when negotiating later. * */ protected function sort() { // q-value buckets $bucket = array();
// sort into q-value buckets foreach ($this->acceptable as $value) { $bucket[(string)$value->getQuality()][] = $value; }
// reverse-sort the buckets so that q=1 is first and q=0 is last, // but the values in the buckets stay in the original order. krsort($bucket);
// flatten the buckets back into the acceptable array $this->acceptable = array(); foreach ($bucket as $q => $acceptable) { foreach ($acceptable as $value) { $this->acceptable[] = $value; } } }
/** * * IteratorInterface: returns the iterator for this object. * * @return ArrayIterator * */ public function getIterator() { return new ArrayIterator($this->acceptable); }
/** * * Negotiates between acceptable and available values. On success, the * return value is a plain old PHP object with the matching negotiated * `$acceptable` and `$available` value objects; these are to be inspected * by the calling code. * * @param array $available Available values in preference order, if any. * * @return mixed A plain-old PHP object with negotiated `$acceptable` and * `$available` value objects on success, or false on failure. * */ public function negotiate(array $available = null) { // if none available, no possible match if (! $available) { return false; }
// convert to object $clone = clone $this; $clone->set($available); $available = $clone;
// if nothing acceptable specified, use first available if (! $this->acceptable) { return (object) array( 'acceptable' => false, 'available' => $available->get(0), ); }
// loop through acceptable values foreach ($this->acceptable as $accept) {
// if the acceptable quality is zero, skip it if ($accept->getQuality() == 0) { continue; }
// if acceptable value is "anything" return the first available if ($accept->isWildcard()) { return (object) array( 'acceptable' => $accept, 'available' => $available->get(0), ); }
// if acceptable value is available, use it foreach ($available as $avail) { if ($accept->match($avail)) { return (object) array( 'acceptable' => $accept, 'available' => $avail, ); } } }
return false; } }
|