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
|
|
|
public function getOrderedElements($header) |
61
|
|
|
{ |
62
|
|
|
if (!$header) { |
63
|
|
|
throw new InvalidArgument('The header string should not be empty.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$elements = array(); |
67
|
|
|
foreach ($this->parseHeader($header) as $h) { |
68
|
|
|
try { |
69
|
22 |
|
$elements[] = $this->acceptFactory($h); |
|
|
|
|
70
|
|
|
} catch (Exception\Exception $e) { |
71
|
22 |
|
// silently skip in case of invalid headers coming in from a client |
72
|
22 |
|
} |
73
|
|
|
} |
74
|
22 |
|
|
75
|
|
|
// sort based on quality |
76
|
22 |
|
usort($elements, function (BaseAccept $a, BaseAccept $b) { |
77
|
19 |
|
return $a->getQuality() < $b->getQuality(); |
78
|
|
|
}); |
79
|
19 |
|
|
80
|
|
|
return $elements; |
81
|
|
|
} |
82
|
22 |
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param string $header accept header part or server priority |
86
|
|
|
* |
87
|
|
|
* @return AcceptHeader Parsed header object |
88
|
|
|
*/ |
89
|
|
|
abstract protected function acceptFactory($header); |
90
|
82 |
|
|
91
|
|
|
/** |
92
|
82 |
|
* @param AcceptHeader $header |
93
|
|
|
* @param AcceptHeader $priority |
94
|
82 |
|
* @param integer $index |
95
|
|
|
* |
96
|
|
|
* @return Match|null Headers matched |
97
|
|
|
*/ |
98
|
82 |
|
protected function match(AcceptHeader $header, AcceptHeader $priority, $index) |
99
|
|
|
{ |
100
|
|
|
$ac = $header->getType(); |
101
|
|
|
$pc = $priority->getType(); |
102
|
|
|
|
103
|
|
|
$equal = !strcasecmp($ac, $pc); |
104
|
|
|
|
105
|
|
|
if ($equal || $ac === '*') { |
106
|
|
|
$score = 1 * $equal; |
107
|
66 |
|
|
108
|
|
|
return new Match($header->getQuality() * $priority->getQuality(), $score, $index); |
109
|
66 |
|
} |
110
|
66 |
|
|
111
|
66 |
|
return null; |
112
|
65 |
|
} |
113
|
56 |
|
|
114
|
56 |
|
/** |
115
|
66 |
|
* @param string $header A string that contains an `Accept*` header. |
116
|
66 |
|
* |
117
|
|
|
* @return AcceptHeader[] |
118
|
66 |
|
*/ |
119
|
|
|
private function parseHeader($header) |
120
|
|
|
{ |
121
|
|
|
$res = preg_match_all('/(?:[^,"]*+(?:"[^"]*+")?)+[^,"]*+/', $header, $matches); |
122
|
|
|
|
123
|
|
|
if (!$res) { |
124
|
|
|
throw new InvalidHeader(sprintf('Failed to parse accept header: "%s"', $header)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return array_values(array_filter(array_map('trim', $matches[0]))); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param AcceptHeader[] $headerParts |
132
|
|
|
* @param Priority[] $priorities Configured priorities |
133
|
|
|
* |
134
|
|
|
* @return Match[] Headers matched |
135
|
|
|
*/ |
136
|
|
|
private function findMatches(array $headerParts, array $priorities) |
137
|
|
|
{ |
138
|
|
|
$matches = []; |
139
|
|
|
foreach ($priorities as $index => $p) { |
140
|
|
|
foreach ($headerParts as $h) { |
141
|
|
|
if (null !== $match = $this->match($h, $p, $index)) { |
142
|
|
|
$matches[] = $match; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return $matches; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
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: