-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathHeader.php
More file actions
61 lines (47 loc) · 1.34 KB
/
Header.php
File metadata and controls
61 lines (47 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace App\HttpClient;
use App\HttpClient\Exception\InvalidHeaderKey;
use App\HttpClient\Exception\InvalidHeaderValue;
class Header
{
private $key;
private $normalizedKey;
private $value;
/**
* @throws InvalidHeaderKey When a the key contains invalid characters
* @throws InvalidHeaderValue When a the value contains invalid characters
*/
public function __construct(string $key, string $value)
{
if (strpos($key, "\r\n") !== false || strpos($key, ':') !== false) {
throw new InvalidHeaderKey();
}
if (strpos($value, "\r\n") !== false) {
throw new InvalidHeaderValue();
}
$this->key = $key;
$this->normalizedKey = strtolower($key);
$this->value = $value;
}
public static function createFromString(string $header): self
{
$headerParts = explode(': ', $header);
return new self($headerParts[0], $headerParts[1]);
}
public function getKey(): string
{
return $this->key;
}
public function getNormalizedKey(): string
{
return $this->normalizedKey;
}
public function getValue(): string
{
return $this->value;
}
public function toString(): string
{
return sprintf("%s: %s\r\n", $this->key, $this->value);
}
}