|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* janitor (http://juliangut.com/janitor). |
|
5
|
|
|
* Effortless maintenance management. |
|
6
|
|
|
* |
|
7
|
|
|
* @license BSD-3-Clause |
|
8
|
|
|
* @link https://github.com/juliangut/janitor |
|
9
|
|
|
* @author Julián Gutiérrez <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Janitor\Excluder; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* HTTP request header value maintenance excluder. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Header implements ExcluderInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Request headers. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $headers; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Header constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param array|string $headers |
|
|
|
|
|
|
32
|
|
|
* @param string $value |
|
|
|
|
|
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct($headers = null, $value = null) |
|
35
|
|
|
{ |
|
36
|
|
|
if ($headers !== null && !is_array($headers)) { |
|
37
|
|
|
$headers = [$headers => $value]; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (is_array($headers)) { |
|
41
|
|
|
foreach ($headers as $headerName => $headerValue) { |
|
42
|
|
|
$this->addHeader($headerName, $headerValue); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Add header. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $header |
|
51
|
|
|
* @param string $value |
|
|
|
|
|
|
52
|
|
|
* |
|
53
|
|
|
* @return $this |
|
54
|
|
|
*/ |
|
55
|
|
|
public function addHeader($header, $value = null) |
|
56
|
|
|
{ |
|
57
|
|
|
if (trim($header) !== '') { |
|
58
|
|
|
$this->headers[trim($header)] = $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
* |
|
67
|
|
|
* @throws \RuntimeException |
|
68
|
|
|
*/ |
|
69
|
|
|
public function isExcluded(ServerRequestInterface $request) |
|
70
|
|
|
{ |
|
71
|
|
|
if (!count($this->headers)) { |
|
72
|
|
|
throw new \RuntimeException('No headers defined in header excluder'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
foreach ($this->headers as $header => $value) { |
|
76
|
|
|
if ($value === null && $request->hasHeader($header)) { |
|
77
|
|
|
return true; |
|
78
|
|
|
} elseif ($value !== null |
|
79
|
|
|
&& (trim($value) === $request->getHeaderLine($header) |
|
80
|
|
|
|| @preg_match(trim($value), $request->getHeaderLine($header))) |
|
81
|
|
|
) { |
|
82
|
|
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.