|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Negotiation; |
|
4
|
|
|
|
|
5
|
|
|
use Negotiation\Exception\InvalidArgument; |
|
6
|
|
|
use Negotiation\Exception\InvalidHeader; |
|
7
|
|
|
|
|
8
|
|
|
abstract class AbstractNegotiator |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param string $header A string containing an `Accept|Accept-*` header. |
|
12
|
|
|
* @param array $priorities A set of server priorities. |
|
13
|
|
|
* |
|
14
|
|
|
* @return AcceptHeader|null best matching type |
|
15
|
|
|
*/ |
|
16
|
69 |
|
public function getBest($header, array $priorities, $strict = false) |
|
17
|
|
|
{ |
|
18
|
69 |
|
if (empty($priorities)) { |
|
19
|
1 |
|
throw new InvalidArgument('A set of server priorities should be given.'); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
68 |
|
if (!$header) { |
|
23
|
3 |
|
throw new InvalidArgument('The header string should not be empty.'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
// Once upon a time, two `array_map` calls were sitting there, but for |
|
27
|
|
|
// some reasons, they triggered `E_WARNING` time to time (because of |
|
28
|
|
|
// PHP bug [55416](https://bugs.php.net/bug.php?id=55416). Now, they |
|
29
|
|
|
// are gone. |
|
30
|
|
|
// See: https://github.com/willdurand/Negotiation/issues/81 |
|
31
|
65 |
|
$acceptedHeaders = array(); |
|
32
|
65 |
|
foreach ($this->parseHeader($header) as $h) { |
|
33
|
|
|
try { |
|
34
|
65 |
|
$acceptedHeaders[] = $this->acceptFactory($h); |
|
|
|
|
|
|
35
|
65 |
|
} catch (Exception\Exception $e) { |
|
36
|
3 |
|
if ($strict) { |
|
37
|
1 |
|
throw $e; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
64 |
|
} |
|
41
|
64 |
|
$acceptedPriorities = array(); |
|
42
|
64 |
|
foreach ($priorities as $p) { |
|
43
|
64 |
|
$acceptedPriorities[] = $this->acceptFactory($p); |
|
44
|
63 |
|
} |
|
45
|
63 |
|
$matches = $this->findMatches($acceptedHeaders, $acceptedPriorities); |
|
46
|
63 |
|
$specificMatches = array_reduce($matches, 'Negotiation\Match::reduce', []); |
|
47
|
|
|
|
|
48
|
63 |
|
usort($specificMatches, 'Negotiation\Match::compare'); |
|
49
|
|
|
|
|
50
|
63 |
|
$match = array_shift($specificMatches); |
|
51
|
|
|
|
|
52
|
63 |
|
return null === $match ? null : $acceptedPriorities[$match->index]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param string $header A string containing an `Accept|Accept-*` header. |
|
57
|
|
|
* |
|
58
|
|
|
* @return [AcceptHeader] An ordered list of accept header elements |
|
|
|
|
|
|
59
|
|
|
*/ |
|
60
|
5 |
|
public function getOrderedElements($header) |
|
61
|
|
|
{ |
|
62
|
5 |
|
if (!$header) { |
|
63
|
1 |
|
throw new InvalidArgument('The header string should not be empty.'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
$elements = array(); |
|
67
|
4 |
|
$orderKeys = array(); |
|
68
|
4 |
|
foreach ($this->parseHeader($header) as $key => $h) { |
|
69
|
|
|
try { |
|
70
|
4 |
|
$element = $this->acceptFactory($h); |
|
|
|
|
|
|
71
|
3 |
|
$elements[] = $element; |
|
72
|
3 |
|
$orderKeys[] = [$element->getQuality(), $key, $element->getValue()]; |
|
73
|
4 |
|
} catch (Exception\Exception $e) { |
|
74
|
|
|
// silently skip in case of invalid headers coming in from a client |
|
75
|
|
|
} |
|
76
|
4 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
// sort based on quality and then original order. This is necessary as |
|
79
|
|
|
// to ensure that the first in the list for two items with the same |
|
80
|
|
|
// quality stays in that order in both PHP5 and PHP7. |
|
81
|
4 |
|
uasort($orderKeys, function ($a, $b) { |
|
82
|
3 |
|
$qA = $a[0]; |
|
83
|
3 |
|
$qB = $b[0]; |
|
84
|
|
|
|
|
85
|
3 |
|
if ($qA == $qB) { |
|
86
|
1 |
|
return $a[1] > $b[1]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
return ($qA > $qB) ? -1 : 1; |
|
90
|
4 |
|
}); |
|
91
|
|
|
|
|
92
|
4 |
|
$orderedElements = []; |
|
93
|
4 |
|
foreach ($orderKeys as $key) { |
|
94
|
3 |
|
$orderedElements[] = $elements[$key[1]]; |
|
95
|
4 |
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
return $orderedElements; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param string $header accept header part or server priority |
|
103
|
|
|
* |
|
104
|
|
|
* @return AcceptHeader Parsed header object |
|
105
|
|
|
*/ |
|
106
|
|
|
abstract protected function acceptFactory($header); |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param AcceptHeader $header |
|
110
|
|
|
* @param AcceptHeader $priority |
|
111
|
|
|
* @param integer $index |
|
112
|
|
|
* |
|
113
|
|
|
* @return Match|null Headers matched |
|
114
|
|
|
*/ |
|
115
|
22 |
|
protected function match(AcceptHeader $header, AcceptHeader $priority, $index) |
|
116
|
|
|
{ |
|
117
|
22 |
|
$ac = $header->getType(); |
|
118
|
22 |
|
$pc = $priority->getType(); |
|
119
|
|
|
|
|
120
|
22 |
|
$equal = !strcasecmp($ac, $pc); |
|
121
|
|
|
|
|
122
|
22 |
|
if ($equal || $ac === '*') { |
|
123
|
19 |
|
$score = 1 * $equal; |
|
124
|
|
|
|
|
125
|
19 |
|
return new Match($header->getQuality() * $priority->getQuality(), $score, $index); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
22 |
|
return null; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param string $header A string that contains an `Accept*` header. |
|
133
|
|
|
* |
|
134
|
|
|
* @return AcceptHeader[] |
|
135
|
|
|
*/ |
|
136
|
86 |
|
private function parseHeader($header) |
|
137
|
|
|
{ |
|
138
|
86 |
|
$res = preg_match_all('/(?:[^,"]*+(?:"[^"]*+")?)+[^,"]*+/', $header, $matches); |
|
139
|
|
|
|
|
140
|
86 |
|
if (!$res) { |
|
141
|
|
|
throw new InvalidHeader(sprintf('Failed to parse accept header: "%s"', $header)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
86 |
|
return array_values(array_filter(array_map('trim', $matches[0]))); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @param AcceptHeader[] $headerParts |
|
149
|
|
|
* @param Priority[] $priorities Configured priorities |
|
150
|
|
|
* |
|
151
|
|
|
* @return Match[] Headers matched |
|
152
|
|
|
*/ |
|
153
|
66 |
|
private function findMatches(array $headerParts, array $priorities) |
|
154
|
|
|
{ |
|
155
|
66 |
|
$matches = []; |
|
156
|
66 |
|
foreach ($priorities as $index => $p) { |
|
157
|
66 |
|
foreach ($headerParts as $h) { |
|
158
|
65 |
|
if (null !== $match = $this->match($h, $p, $index)) { |
|
159
|
56 |
|
$matches[] = $match; |
|
160
|
56 |
|
} |
|
161
|
66 |
|
} |
|
162
|
66 |
|
} |
|
163
|
|
|
|
|
164
|
66 |
|
return $matches; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: