1 | <?php |
||
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 accept header part or server priority |
||
57 | * |
||
58 | * @return AcceptHeader Parsed header object |
||
59 | */ |
||
60 | abstract protected function acceptFactory($header); |
||
61 | |||
62 | /** |
||
63 | * @param AcceptHeader $header |
||
64 | * @param AcceptHeader $priority |
||
65 | * @param integer $index |
||
66 | * |
||
67 | * @return Match|null Headers matched |
||
68 | */ |
||
69 | 22 | protected function match(AcceptHeader $header, AcceptHeader $priority, $index) |
|
84 | |||
85 | /** |
||
86 | * @param string $header A string that contains an `Accept*` header. |
||
87 | * |
||
88 | * @return AcceptHeader[] |
||
89 | */ |
||
90 | 82 | private function parseHeader($header) |
|
100 | |||
101 | /** |
||
102 | * @param AcceptHeader[] $headerParts |
||
103 | * @param Priority[] $priorities Configured priorities |
||
104 | * |
||
105 | * @return Match[] Headers matched |
||
106 | */ |
||
107 | 66 | private function findMatches(array $headerParts, array $priorities) |
|
120 | } |
||
121 |
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: