1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\RemoteRequestPsr\Traits; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Psr\Http\Message\MessageInterface; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait THeaders |
11
|
|
|
* @package kalanis\RemoteRequestPsr\Traits |
12
|
|
|
* Note: Contrary the PSR requirements this class does not offer immutability |
13
|
|
|
* That's because you need to change request properties before you set them to RemoteRequest |
14
|
|
|
*/ |
15
|
|
|
trait THeaders |
16
|
|
|
{ |
17
|
|
|
/** @var array<string, array<int, string>> */ |
18
|
|
|
protected array $headers = []; |
19
|
|
|
|
20
|
5 |
|
public function getHeaders(): array |
21
|
|
|
{ |
22
|
5 |
|
return $this->headers; |
23
|
|
|
} |
24
|
|
|
|
25
|
2 |
|
public function hasHeader(string $name): bool |
26
|
|
|
{ |
27
|
2 |
|
$lookup = mb_strtolower($name); |
28
|
2 |
|
foreach ($this->headers as $key => $values) { |
29
|
1 |
|
if (mb_strtolower($key) == $lookup) { |
30
|
1 |
|
return true; |
31
|
|
|
} |
32
|
|
|
} |
33
|
1 |
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
public function getHeader(string $name): array |
37
|
|
|
{ |
38
|
3 |
|
$lookup = mb_strtolower($name); |
39
|
3 |
|
$wanted = []; |
40
|
3 |
|
foreach ($this->headers as $key => $values) { |
41
|
2 |
|
if (mb_strtolower($key) == $lookup) { |
42
|
2 |
|
$wanted = array_merge($wanted, $values); |
43
|
|
|
} |
44
|
|
|
} |
45
|
3 |
|
return $wanted; |
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
public function getHeaderLine(string $name): string |
49
|
|
|
{ |
50
|
1 |
|
return implode(',', $this->getHeader($name)); |
51
|
|
|
} |
52
|
|
|
|
53
|
6 |
|
public function withHeader(string $name, $value): MessageInterface |
54
|
|
|
{ |
55
|
6 |
|
$lookup = mb_strtolower($name); |
56
|
6 |
|
foreach ($this->headers as $key => $values) { |
57
|
2 |
|
if (mb_strtolower($key) == $lookup) { |
58
|
2 |
|
if (is_array($value)) { |
59
|
1 |
|
$this->headers[$key] = array_merge($this->headers[$key], array_map('strval', $value)); |
60
|
|
|
} else { |
61
|
1 |
|
$this->headers[$key] = array_merge($this->headers[$key], [strval($value)]); |
62
|
|
|
} |
63
|
2 |
|
return $this; |
|
|
|
|
64
|
|
|
} |
65
|
|
|
} |
66
|
6 |
|
return $this->withAddedHeader($name, $value); |
67
|
|
|
} |
68
|
|
|
|
69
|
6 |
|
public function withAddedHeader(string $name, $value): MessageInterface |
70
|
|
|
{ |
71
|
6 |
|
$this->withoutHeader($name); |
72
|
6 |
|
if (is_array($value)) { |
73
|
1 |
|
$this->headers[$name] = array_map('strval', $value); |
74
|
|
|
} else { |
75
|
5 |
|
$this->headers[$name] = [strval($value)]; |
76
|
|
|
} |
77
|
6 |
|
return $this; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
public function withoutHeader(string $name): MessageInterface |
81
|
|
|
{ |
82
|
6 |
|
$lookup = mb_strtolower($name); |
83
|
6 |
|
$wanted = []; |
84
|
6 |
|
foreach ($this->headers as $key => $values) { |
85
|
2 |
|
if (mb_strtolower($key) == $lookup) { |
86
|
2 |
|
$wanted[] = $key; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
foreach ($wanted as $item) { |
91
|
2 |
|
unset($this->headers[$item]); |
92
|
|
|
} |
93
|
6 |
|
return $this; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|