|
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
|
1 |
|
} |
|
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 |
|
if (utf8_decode($directive) !== $directive || preg_match($regEx, $directive, $matches) !== 1) { |
|
46
|
3 |
|
throw new InvalidArgumentException('Invalid directive: ' . $directive); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
if (isset($matches['VALUE'])) { |
|
50
|
2 |
|
throw new InvalidArgumentException('Invalid value: ' . $matches['VALUE']); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
return new self($matches['NAME']); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public function __toString(): string |
|
60
|
|
|
{ |
|
61
|
1 |
|
return $this->name; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @inheritDoc |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public function setName(string $name): void |
|
68
|
|
|
{ |
|
69
|
5 |
|
if (utf8_decode($name) !== $name || preg_match('{^' . Rfc7230::TOKEN . '$}', $name) !== 1) { |
|
70
|
2 |
|
throw new InvalidArgumentException('Invalid name: ' . $name); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
5 |
|
if (in_array(strtolower($name), FieldListDirective::RESERVED_NAMES, /*strict*/true)) { |
|
74
|
1 |
|
throw new InvalidArgumentException('Name reserved for field list directive: ' . $name); |
|
75
|
|
|
} |
|
76
|
5 |
|
if (in_array(strtolower($name), IntegerDirective::RESERVED_NAMES, /*strict*/true)) { |
|
77
|
1 |
|
throw new InvalidArgumentException('Name reserved for integer directive: ' . $name); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
5 |
|
$this->name = $name; |
|
81
|
5 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return null Value. |
|
85
|
|
|
*/ |
|
86
|
1 |
|
public function getValue(): ?string |
|
87
|
|
|
{ |
|
88
|
1 |
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|