1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stadly\Http\Header\Value\CacheControl; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Stadly\Http\Utilities\Rfc7230; |
9
|
|
|
use Stadly\Http\Utilities\Rfc7234; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class for handling cache control directives without value. |
13
|
|
|
* |
14
|
|
|
* Specification: https://tools.ietf.org/html/rfc7234#section-5.2.2 |
15
|
|
|
*/ |
16
|
|
|
final class ValuelessDirective extends Directive |
17
|
|
|
{ |
18
|
|
|
public const RESERVED_NAMES = [ |
19
|
|
|
'must-revalidate', |
20
|
|
|
'no-store', |
21
|
|
|
'no-transform', |
22
|
|
|
'public', |
23
|
|
|
'proxy-revalidate', |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor. |
28
|
|
|
* |
29
|
|
|
* @param string $name Name. |
30
|
|
|
*/ |
31
|
5 |
|
public function __construct(string $name) |
32
|
|
|
{ |
33
|
5 |
|
$this->setName($name); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Construct directive from string. |
38
|
|
|
* |
39
|
|
|
* @param string $directive Directive string. |
40
|
|
|
* @return self Directive generated based on the string. |
41
|
|
|
*/ |
42
|
6 |
|
public static function fromString(string $directive): self |
43
|
|
|
{ |
44
|
6 |
|
$regEx = '{^' . Rfc7234::CACHE_DIRECTIVE_CAPTURE . '$}'; |
45
|
6 |
|
$plainDirective = mb_convert_encoding($directive, 'ISO-8859-1', 'UTF-8'); |
46
|
6 |
|
if ($plainDirective !== $directive || preg_match($regEx, $directive, $matches) !== 1) { |
47
|
3 |
|
throw new InvalidArgumentException('Invalid directive: ' . $directive); |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
if (isset($matches['VALUE'])) { |
51
|
2 |
|
throw new InvalidArgumentException('Invalid value: ' . $matches['VALUE']); |
52
|
|
|
} |
53
|
|
|
|
54
|
1 |
|
return new self($matches['NAME']); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
1 |
|
public function __toString(): string |
61
|
|
|
{ |
62
|
1 |
|
return $this->name; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
5 |
|
public function setName(string $name): void |
69
|
|
|
{ |
70
|
5 |
|
$plainName = mb_convert_encoding($name, 'ISO-8859-1', 'UTF-8'); |
71
|
5 |
|
if ($plainName !== $name || preg_match('{^' . Rfc7230::TOKEN . '$}', $name) !== 1) { |
72
|
2 |
|
throw new InvalidArgumentException('Invalid name: ' . $name); |
73
|
|
|
} |
74
|
|
|
|
75
|
5 |
|
if (in_array(strtolower($name), FieldListDirective::RESERVED_NAMES, /*strict*/true)) { |
76
|
1 |
|
throw new InvalidArgumentException('Name reserved for field list directive: ' . $name); |
77
|
|
|
} |
78
|
5 |
|
if (in_array(strtolower($name), IntegerDirective::RESERVED_NAMES, /*strict*/true)) { |
79
|
1 |
|
throw new InvalidArgumentException('Name reserved for integer directive: ' . $name); |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
$this->name = $name; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return null Value. |
87
|
|
|
*/ |
88
|
1 |
|
public function getValue(): ?string |
89
|
|
|
{ |
90
|
1 |
|
return null; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|