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