1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stadly\Http\Header\Request; |
6
|
|
|
|
7
|
|
|
use Stadly\Http\Exception\InvalidHeader; |
8
|
|
|
use Stadly\Http\Header\Value\Range\RangeSet; |
9
|
|
|
use Stadly\Http\Header\Value\Range\RangeSetFactory; |
10
|
|
|
use Stadly\Http\Utilities\Rfc7233; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class for handling the HTTP header field Range. |
14
|
|
|
* |
15
|
|
|
* Specification: https://tools.ietf.org/html/rfc7233#section-3.1 |
16
|
|
|
*/ |
17
|
|
|
final class Range implements Header |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var RangeSet Range set. |
21
|
|
|
*/ |
22
|
|
|
private $rangeSet; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
* |
27
|
|
|
* @param RangeSet $rangeSet Set of ranges. |
28
|
|
|
*/ |
29
|
1 |
|
public function __construct(RangeSet $rangeSet) |
30
|
|
|
{ |
31
|
1 |
|
$this->setRangeSet($rangeSet); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Construct header from value. |
36
|
|
|
* |
37
|
|
|
* @param string $value Header value. |
38
|
|
|
* @return self Header generated based on the value. |
39
|
|
|
* @throws InvalidHeader If the header value is invalid. |
40
|
|
|
*/ |
41
|
2 |
|
public static function fromValue(string $value): self |
42
|
|
|
{ |
43
|
2 |
|
$regEx = '{^' . Rfc7233::RANGE . '$}'; |
44
|
2 |
|
$plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8'); |
45
|
2 |
|
if ($plainValue !== $value || preg_match($regEx, $value) !== 1) { |
46
|
1 |
|
throw new InvalidHeader('Invalid header value: ' . $value); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
return new self(RangeSetFactory::fromString($value)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
* @throws void The header is always valid. |
55
|
|
|
*/ |
56
|
2 |
|
public function __toString(): string |
57
|
|
|
{ |
58
|
2 |
|
return $this->getName() . ': ' . $this->getValue(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return true The header is always valid. |
63
|
|
|
*/ |
64
|
1 |
|
public function isValid(): bool |
65
|
|
|
{ |
66
|
1 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritDoc |
71
|
|
|
*/ |
72
|
1 |
|
public function getName(): string |
73
|
|
|
{ |
74
|
1 |
|
return 'Range'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
* @throws void The header is always valid. |
80
|
|
|
*/ |
81
|
2 |
|
public function getValue(): string |
82
|
|
|
{ |
83
|
2 |
|
return (string)$this->rangeSet; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return RangeSet Set of ranges. |
88
|
|
|
*/ |
89
|
1 |
|
public function getRangeSet(): RangeSet |
90
|
|
|
{ |
91
|
1 |
|
return $this->rangeSet; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Set range set. |
96
|
|
|
* |
97
|
|
|
* @param RangeSet $rangeSet Set of ranges. |
98
|
|
|
*/ |
99
|
1 |
|
public function setRangeSet(RangeSet $rangeSet): void |
100
|
|
|
{ |
101
|
1 |
|
$this->rangeSet = $rangeSet; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|