Skip to content

Commit f3d6768

Browse files
Anton ShevchukAnton Shevchuk
authored andcommitted
Added support Accpet-Language header
1 parent 810c49a commit f3d6768

1 file changed

Lines changed: 60 additions & 30 deletions

File tree

src/Proxy/Request.php

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ final class Request
5252
*/
5353
static private $accept;
5454

55+
/**
56+
* @var array|null Accepted languages
57+
*/
58+
static private $language;
59+
5560
/**
5661
* Init instance
5762
*
@@ -253,41 +258,66 @@ public static function getMethod(): string
253258
public static function getAccept(): array
254259
{
255260
if (!self::$accept) {
256-
// save to static variable
257-
self::$accept = [];
261+
// get header from request
262+
self::$accept = self::parseAcceptHeader(self::getHeader('Accept'));
263+
}
264+
return self::$accept;
265+
}
258266

267+
/**
268+
* Get Accept MIME Type
269+
*
270+
* @return array
271+
*/
272+
public static function getAcceptLanguage(): array
273+
{
274+
if (!self::$language) {
259275
// get header from request
260-
$header = self::getHeader('accept');
276+
self::$language = self::parseAcceptHeader(self::getHeader('Accept-Language'));
277+
}
278+
return self::$language;
279+
}
261280

262-
// nothing ...
263-
if (!$header) {
264-
return self::$accept;
265-
}
281+
/**
282+
* parseAcceptHeader
283+
*
284+
* @param string $header
285+
*
286+
* @return array
287+
*/
288+
private static function parseAcceptHeader($header): array
289+
{
290+
// empty array
291+
$accept = [];
292+
293+
// check empty
294+
if (!$header || $header === '') {
295+
return $accept;
296+
}
266297

267-
// make array if types
268-
$header = explode(',', $header);
269-
$header = array_map('trim', $header);
270-
271-
foreach ($header as $a) {
272-
// the default quality is 1.
273-
$q = 1;
274-
// check if there is a different quality
275-
if (strpos($a, ';q=') or strpos($a, '; q=')) {
276-
// divide "mime/type;q=X" into two parts: "mime/type" i "X"
277-
[$a, $q] = preg_split('/;([ ]?)q=/', $a);
278-
}
279-
// remove other extension
280-
if (strpos($a, ';')) {
281-
$a = substr($a, 0, strpos($a, ';'));
282-
}
283-
284-
// mime-type $a is accepted with the quality $q
285-
// WARNING: $q == 0 means, that mime-type isn’t supported!
286-
self::$accept[$a] = (float)$q;
298+
// make array from header
299+
$values = explode(',', $header);
300+
$values = array_map('trim', $values);
301+
302+
foreach ($values as $a) {
303+
// the default quality is 1.
304+
$q = 1;
305+
// check if there is a different quality
306+
if (strpos($a, ';q=') or strpos($a, '; q=')) {
307+
// divide "mime/type;q=X" into two parts: "mime/type" i "X"
308+
[$a, $q] = preg_split('/;([ ]?)q=/', $a);
287309
}
288-
arsort(self::$accept);
310+
// remove other extension
311+
if (strpos($a, ';')) {
312+
$a = substr($a, 0, strpos($a, ';'));
313+
}
314+
315+
// mime-type $a is accepted with the quality $q
316+
// WARNING: $q == 0 means, that isn’t supported!
317+
$accept[$a] = (float)$q;
289318
}
290-
return self::$accept;
319+
arsort($accept);
320+
return $accept;
291321
}
292322

293323
/**
@@ -320,7 +350,7 @@ public static function checkAccept(array $allowTypes = [])
320350

321351
// let’s check our supported types:
322352
foreach ($accept as $mime => $quality) {
323-
if ($quality && \in_array($mime, $allowTypes)) {
353+
if ($quality && \in_array($mime, $allowTypes, true)) {
324354
return $mime;
325355
}
326356
}

0 commit comments

Comments
 (0)