1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.4.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Http\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Trait Header |
19
|
|
|
* @package Quantum\Http\Request |
20
|
|
|
*/ |
21
|
|
|
trait Header |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Request headers |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private static $__headers = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Checks the request header existence by given key |
32
|
|
|
* @param string $key |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public static function hasHeader(string $key): bool |
36
|
|
|
{ |
37
|
|
|
list($keyWithHyphens, $keyWithUnderscores) = self::normalizeHeaderKey($key); |
38
|
|
|
|
39
|
|
|
return isset(self::$__headers[$keyWithHyphens]) || isset(self::$__headers[$keyWithUnderscores]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets the request header by given key |
44
|
|
|
* @param string $key |
45
|
|
|
* @return string|null |
46
|
|
|
*/ |
47
|
|
|
public static function getHeader(string $key): ?string |
48
|
|
|
{ |
49
|
|
|
if (self::hasHeader($key)) { |
50
|
|
|
list($keyWithHyphens, $keyWithUnderscores) = self::normalizeHeaderKey($key); |
51
|
|
|
return self::$__headers[$keyWithHyphens] ?? self::$__headers[$keyWithUnderscores]; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sets the request header |
59
|
|
|
* @param string $key |
60
|
|
|
* @param mixed $value |
61
|
|
|
*/ |
62
|
|
|
public static function setHeader(string $key, $value) |
63
|
|
|
{ |
64
|
|
|
self::$__headers[strtolower($key)] = $value; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets all request headers |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
|
|
public static function allHeaders(): array |
72
|
|
|
{ |
73
|
|
|
return self::$__headers; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Deletes the header by given key |
78
|
|
|
* @param string $key |
79
|
|
|
*/ |
80
|
|
|
public static function deleteHeader(string $key) |
81
|
|
|
{ |
82
|
|
|
if (self::hasHeader($key)) { |
83
|
|
|
unset(self::$__headers[strtolower($key)]); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $key |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
private static function normalizeHeaderKey(string $key): array |
92
|
|
|
{ |
93
|
|
|
$keyWithHyphens = str_replace('_', '-', strtolower($key)); |
94
|
|
|
$keyWithUnderscores = str_replace('-', '_', $key); |
95
|
|
|
return [$keyWithHyphens, $keyWithUnderscores]; |
96
|
|
|
} |
97
|
|
|
} |