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