1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Caridea |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Caridea\Http; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Provides utilities for working with the HTTP Accept header. |
25
|
|
|
* |
26
|
|
|
* @copyright 2015-2018 LibreWorks contributors |
27
|
|
|
* @license Apache-2.0 |
28
|
|
|
*/ |
29
|
|
|
class AcceptTypes |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var \SplPriorityQueue |
33
|
|
|
*/ |
34
|
|
|
private $types; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates a new AcceptTypes. |
38
|
|
|
* |
39
|
|
|
* @param array $server The server variables. (e.g. `$_SERVER`) |
40
|
|
|
*/ |
41
|
2 |
|
public function __construct(array $server) |
42
|
|
|
{ |
43
|
2 |
|
$this->types = new \SplPriorityQueue(); |
44
|
2 |
|
if (isset($server['HTTP_ACCEPT'])) { |
45
|
2 |
|
foreach (preg_split('#,\s*#', $server['HTTP_ACCEPT']) as $accept) { |
46
|
2 |
|
$split = array_pad(preg_split('#;\s*q=#', $accept, 2), 2, 1.0); |
47
|
2 |
|
$this->types->insert($split[0], (float)$split[1]); |
48
|
|
|
} |
49
|
|
|
} |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the most preferred MIME type out of the provided list. |
54
|
|
|
* |
55
|
|
|
* This method will iterate through the stored MIME types in preferred order |
56
|
|
|
* and attempt to match them to those provided. |
57
|
|
|
* |
58
|
|
|
* Say for example the HTTP Accept header was: |
59
|
|
|
* `text/html,application/xhtml+xml,application/xml;q=0.9,text/*;q=0.8`. |
60
|
|
|
* |
61
|
|
|
* ```php |
62
|
|
|
* // will return 'text/html' |
63
|
|
|
* $types->preferred(['text/css', 'text/html', 'application/javascript']); |
64
|
|
|
* // will return 'application/xml' |
65
|
|
|
* $types->preferred(['application/xml', 'text/css', 'application/json']); |
66
|
|
|
* // will return 'text/plain' |
67
|
|
|
* $types->preferred(['text/plain', 'text/css', 'application/json']); |
68
|
|
|
* // will return null |
69
|
|
|
* $types->preferred(['application/json', 'application/octet-stream']); |
70
|
|
|
* // will return null |
71
|
|
|
* $types->preferred([]); |
72
|
|
|
* ``` |
73
|
|
|
* |
74
|
|
|
* @param string[] $types The MIME types to compare |
75
|
|
|
* @return string|null The most preferred MIME type or null |
76
|
|
|
*/ |
77
|
2 |
|
public function preferred(array $types): ?string |
78
|
|
|
{ |
79
|
2 |
|
if (empty($types)) { |
80
|
2 |
|
return null; |
81
|
|
|
} |
82
|
2 |
|
foreach ($this->types as $type) { |
83
|
2 |
|
if (in_array($type, $types, true)) { |
84
|
2 |
|
return $type; |
85
|
2 |
|
} elseif ('*/*' == $type) { |
86
|
1 |
|
return current($types); |
|
|
|
|
87
|
2 |
|
} elseif (strlen($type) > 2 && substr($type, -2, 2) == '/*') { |
88
|
1 |
|
$prefix = substr($type, 0, strpos($type, '/') + 1); |
89
|
1 |
|
$plen = strlen($prefix); |
90
|
1 |
|
foreach ($types as $t) { |
91
|
1 |
|
if (strncmp($prefix, $t, $plen) === 0) { |
92
|
2 |
|
return $t; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
} |
97
|
1 |
|
return null; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|