1
|
|
|
<?php |
2
|
|
|
namespace LunixREST\RequestFactory\HeaderParser; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Parse out all the common headers from a request. |
6
|
|
|
* Class DefaultHeaderParser |
7
|
|
|
* @package LunixREST\RequestFactory\HeaderParser |
8
|
|
|
*/ |
9
|
|
|
class DefaultHeaderParser implements HeaderParser |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $apiKeyHeaderKey; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* DefaultHeaderParser constructor. |
19
|
|
|
* @param string $apiKeyHeaderKey |
20
|
|
|
*/ |
21
|
7 |
|
public function __construct($apiKeyHeaderKey = 'x-api-key') |
22
|
|
|
{ |
23
|
7 |
|
$this->apiKeyHeaderKey = $apiKeyHeaderKey; |
24
|
7 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param array $headers |
28
|
|
|
* @return ParsedHeaders |
29
|
|
|
*/ |
30
|
6 |
|
public function parse(array $headers): ParsedHeaders |
31
|
|
|
{ |
32
|
6 |
|
$contentType = $this->getContentType($headers); |
|
|
|
|
33
|
6 |
|
$acceptableMIMETypes = $this->findAcceptableMIMETypes($headers); |
34
|
6 |
|
$apiKey = $this->findAPIKey($headers); |
|
|
|
|
35
|
|
|
|
36
|
6 |
|
return new ParsedHeaders($contentType, $acceptableMIMETypes, $apiKey); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param array $headers |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
6 |
|
protected function findAcceptableMIMETypes(array $headers): array |
44
|
|
|
{ |
45
|
|
|
//TODO: follow RFC2616 order |
46
|
6 |
|
$acceptedMIMETypes = []; |
47
|
|
|
|
48
|
6 |
|
foreach ($headers as $key => $values) { |
49
|
6 |
|
if (strtolower($key) == 'http-accept') { |
50
|
1 |
|
foreach ($values as $acceptedType) { |
51
|
1 |
|
$typeParts = explode(';', $acceptedType); |
52
|
1 |
|
if (count($typeParts) > 0) { |
53
|
1 |
|
$acceptedMIMETypes[] = trim($typeParts[0]); |
54
|
|
|
} |
55
|
|
|
} |
56
|
1 |
|
break; |
57
|
|
|
} |
58
|
|
|
} |
59
|
6 |
|
return $acceptedMIMETypes; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $headers |
64
|
|
|
* @return null |
65
|
|
|
*/ |
66
|
6 |
View Code Duplication |
protected function findAPIKey(array $headers) |
|
|
|
|
67
|
|
|
{ |
68
|
6 |
|
foreach ($headers as $key => $values) { |
69
|
6 |
|
if (strtolower($key) == strtolower($this->apiKeyHeaderKey)) { |
70
|
3 |
|
foreach($values as $value) { |
71
|
3 |
|
return $value; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
3 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param array $headers |
80
|
|
|
* @return null |
81
|
|
|
*/ |
82
|
6 |
View Code Duplication |
protected function getContentType(array $headers) |
|
|
|
|
83
|
|
|
{ |
84
|
6 |
|
foreach ($headers as $key => $values) { |
85
|
6 |
|
if (strtolower($key) == 'content-type') { |
86
|
2 |
|
foreach ($values as $value) { |
87
|
2 |
|
return $value; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
4 |
|
return null; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()
can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.