1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiHttp\Negotiation; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 |
9
|
|
|
*/ |
10
|
|
|
final class AcceptNegotiator implements NegotiatorInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param string $header |
14
|
|
|
* @param array $supported |
15
|
|
|
* |
16
|
|
|
* @return NegotiatedValue|null |
17
|
|
|
*/ |
18
|
6 |
|
public function negotiate(string $header, array $supported) |
19
|
|
|
{ |
20
|
6 |
|
if ([] === $supported) { |
21
|
|
|
return null; |
22
|
|
|
} |
23
|
|
|
|
24
|
6 |
|
$aggregatedValues = $this->aggregatedValues($header); |
25
|
|
|
|
26
|
6 |
|
return $this->compareAgainstSupportedTypes($aggregatedValues, $supported); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $header |
31
|
|
|
* |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
6 |
|
private function aggregatedValues(string $header): array |
35
|
|
|
{ |
36
|
6 |
|
$values = []; |
37
|
6 |
|
foreach (explode(',', $header) as $headerValue) { |
38
|
6 |
|
$headerValueParts = explode(';', $headerValue); |
39
|
6 |
|
$mimeType = trim(array_shift($headerValueParts)); |
40
|
6 |
|
$attributes = []; |
41
|
6 |
View Code Duplication |
foreach ($headerValueParts as $attribute) { |
|
|
|
|
42
|
5 |
|
list($attributeKey, $attributeValue) = explode('=', $attribute); |
43
|
5 |
|
$attributes[trim($attributeKey)] = trim($attributeValue); |
44
|
|
|
} |
45
|
|
|
|
46
|
6 |
|
if (!isset($attributes['q'])) { |
47
|
6 |
|
$attributes['q'] = '1.0'; |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
$values[$mimeType] = $attributes; |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
uasort($values, function (array $a, array $b) { |
54
|
6 |
|
return $b['q'] <=> $a['q']; |
55
|
6 |
|
}); |
56
|
|
|
|
57
|
6 |
|
return $values; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $aggregatedValues |
62
|
|
|
* @param array $supportedMimeTypes |
63
|
|
|
* |
64
|
|
|
* @return NegotiatedValue|null |
65
|
|
|
*/ |
66
|
6 |
|
private function compareAgainstSupportedTypes(array $aggregatedValues, array $supportedMimeTypes) |
67
|
|
|
{ |
68
|
6 |
|
foreach ($aggregatedValues as $mimeType => $attributes) { |
69
|
6 |
|
if ('*/*' === $mimeType) { |
70
|
1 |
|
return new NegotiatedValue(reset($supportedMimeTypes), $attributes); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
list($type, $subType) = explode('/', $mimeType); |
74
|
|
|
|
75
|
6 |
|
if ('*' === $type && '*' !== $subType) { // skip invalid value |
76
|
1 |
|
continue; |
77
|
|
|
} |
78
|
|
|
|
79
|
5 |
|
$subTypePattern = $subType !== '*' ? preg_quote($subType) : '.+'; |
80
|
|
|
|
81
|
5 |
|
foreach ($supportedMimeTypes as $supportedMimeType) { |
82
|
5 |
|
if (1 === preg_match('/^'.preg_quote($type).'\/'.$subTypePattern.'$/', $supportedMimeType)) { |
83
|
5 |
|
return new NegotiatedValue($supportedMimeType, $attributes); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return null; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.