1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Chubbyphp\ApiHttp\Negotiation; |
6
|
|
|
|
7
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @see https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 |
11
|
|
|
*/ |
12
|
|
|
final class AcceptNegotiator implements NegotiatorInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
private $supportedMimeTypes; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param array $supportedMimeTypes |
21
|
|
|
*/ |
22
|
8 |
|
public function __construct(array $supportedMimeTypes) |
23
|
|
|
{ |
24
|
8 |
|
$this->supportedMimeTypes = $supportedMimeTypes; |
25
|
8 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param Request $request |
29
|
|
|
* |
30
|
|
|
* @return NegotiatedValue|null |
31
|
|
|
*/ |
32
|
8 |
View Code Duplication |
public function negotiate(Request $request) |
|
|
|
|
33
|
|
|
{ |
34
|
8 |
|
if ([] === $this->supportedMimeTypes) { |
35
|
1 |
|
return null; |
36
|
|
|
} |
37
|
|
|
|
38
|
7 |
|
if (!$request->hasHeader('Accept')) { |
39
|
1 |
|
return null; |
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
$aggregatedValues = $this->aggregatedValues($request->getHeaderLine('Accept')); |
43
|
|
|
|
44
|
6 |
|
return $this->compareAgainstSupportedTypes($aggregatedValues); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $header |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
6 |
|
private function aggregatedValues(string $header): array |
53
|
|
|
{ |
54
|
6 |
|
$values = []; |
55
|
6 |
|
foreach (explode(',', $header) as $headerValue) { |
56
|
6 |
|
$headerValueParts = explode(';', $headerValue); |
57
|
6 |
|
$mimeType = trim(array_shift($headerValueParts)); |
58
|
6 |
|
$attributes = []; |
59
|
6 |
View Code Duplication |
foreach ($headerValueParts as $attribute) { |
|
|
|
|
60
|
5 |
|
list($attributeKey, $attributeValue) = explode('=', $attribute); |
61
|
5 |
|
$attributes[trim($attributeKey)] = trim($attributeValue); |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
if (!isset($attributes['q'])) { |
65
|
6 |
|
$attributes['q'] = '1.0'; |
66
|
|
|
} |
67
|
|
|
|
68
|
6 |
|
$values[$mimeType] = $attributes; |
69
|
|
|
} |
70
|
|
|
|
71
|
6 |
|
uasort($values, function (array $a, array $b) { |
72
|
6 |
|
return $b['q'] <=> $a['q']; |
73
|
6 |
|
}); |
74
|
|
|
|
75
|
6 |
|
return $values; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $aggregatedValues |
80
|
|
|
* |
81
|
|
|
* @return NegotiatedValue|null |
82
|
|
|
*/ |
83
|
6 |
|
private function compareAgainstSupportedTypes(array $aggregatedValues) |
84
|
|
|
{ |
85
|
6 |
|
foreach ($aggregatedValues as $mimeType => $attributes) { |
86
|
6 |
|
if ('*/*' === $mimeType) { |
87
|
1 |
|
return new NegotiatedValue(reset($this->supportedMimeTypes), $attributes); |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
list($type, $subType) = explode('/', $mimeType); |
91
|
|
|
|
92
|
6 |
|
if ('*' === $type && '*' !== $subType) { // skip invalid value |
93
|
1 |
|
continue; |
94
|
|
|
} |
95
|
|
|
|
96
|
5 |
|
$subTypePattern = $subType !== '*' ? preg_quote($subType) : '.+'; |
97
|
|
|
|
98
|
5 |
|
foreach ($this->supportedMimeTypes as $supportedMimeType) { |
99
|
5 |
|
if (1 === preg_match('/^'.preg_quote($type).'\/'.$subTypePattern.'$/', $supportedMimeType)) { |
100
|
5 |
|
return new NegotiatedValue($supportedMimeType, $attributes); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return null; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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.