|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stadly\Http\Header\Value\EntityTag; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Stadly\Http\Utilities\Rfc7232; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class for handling entity tags. |
|
12
|
|
|
* |
|
13
|
|
|
* Specification: https://tools.ietf.org/html/rfc7232#section-2.3 |
|
14
|
|
|
*/ |
|
15
|
|
|
final class EntityTag |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string Entity tag value. |
|
19
|
|
|
*/ |
|
20
|
|
|
private $value; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool Whether the entity tag is a weak validator. |
|
24
|
|
|
*/ |
|
25
|
|
|
private $isWeak; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Constructor. |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $value Entity tag value. |
|
31
|
|
|
* @param bool $isWeak Whether the entity tag is a weak validator. |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function __construct(string $value, bool $isWeak = false) |
|
34
|
|
|
{ |
|
35
|
|
|
// Not possible to change value, since it may be used as key in arrays. |
|
36
|
4 |
|
$plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8'); |
|
37
|
4 |
|
if ($plainValue !== $value || preg_match('{^' . Rfc7232::ETAGC . '*$}', $value) !== 1) { |
|
38
|
1 |
|
throw new InvalidArgumentException('Invalid entity tag: ' . $value); |
|
39
|
|
|
} |
|
40
|
3 |
|
$this->value = $value; |
|
41
|
|
|
|
|
42
|
3 |
|
$this->setWeak($isWeak); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Construct entity tag from string. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $entityTag Entity tag string. |
|
49
|
|
|
* @return self Entity tag generated based on the string. |
|
50
|
|
|
*/ |
|
51
|
5 |
|
public static function fromString(string $entityTag): self |
|
52
|
|
|
{ |
|
53
|
5 |
|
$regEx = '{^' . Rfc7232::ENTITY_TAG_CAPTURE . '$}'; |
|
54
|
5 |
|
$plainEntityTag = mb_convert_encoding($entityTag, 'ISO-8859-1', 'UTF-8'); |
|
55
|
5 |
|
if ($plainEntityTag !== $entityTag || preg_match($regEx, $entityTag, $matches) !== 1) { |
|
56
|
2 |
|
throw new InvalidArgumentException('Invalid entity tag: ' . $entityTag); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
return new self($matches['ETAGCS'], preg_match('{^' . Rfc7232::WEAK . '$}', $matches['WEAK']) === 1); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @return string String representation of the entity tag. |
|
64
|
|
|
*/ |
|
65
|
3 |
|
public function __toString(): string |
|
66
|
|
|
{ |
|
67
|
3 |
|
$entityTag = ''; |
|
68
|
|
|
|
|
69
|
3 |
|
if ($this->isWeak) { |
|
70
|
1 |
|
$entityTag .= 'W/'; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
$entityTag .= '"' . $this->value . '"'; |
|
74
|
|
|
|
|
75
|
3 |
|
return $entityTag; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Specification: https://tools.ietf.org/html/rfc7232#section-2.1 |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool Whether the entity tag is a weak validator. |
|
82
|
|
|
*/ |
|
83
|
2 |
|
public function isWeak(): bool |
|
84
|
|
|
{ |
|
85
|
2 |
|
return $this->isWeak; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param bool $isWeak Whether the entity tag is a weak validator. |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public function setWeak(bool $isWeak): void |
|
92
|
|
|
{ |
|
93
|
2 |
|
$this->isWeak = $isWeak; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return string Entity tag value. |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getValue(): string |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->value; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Specification: https://tools.ietf.org/html/rfc7232#section-2.3.2 |
|
106
|
|
|
* |
|
107
|
|
|
* @param self $entityTag Entity tag to compare with. |
|
108
|
|
|
* @return bool Whether the entity tags match each other, using strong comparison. |
|
109
|
|
|
*/ |
|
110
|
8 |
|
public function compareStrongly(self $entityTag): bool |
|
111
|
|
|
{ |
|
112
|
8 |
|
return !$this->isWeak && !$entityTag->isWeak && $this->value === $entityTag->value; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Specification: https://tools.ietf.org/html/rfc7232#section-2.3.2 |
|
117
|
|
|
* |
|
118
|
|
|
* @param self $entityTag Entity tag to compare with. |
|
119
|
|
|
* @return bool Whether the entity tags match each other, using weak comparison. |
|
120
|
|
|
*/ |
|
121
|
8 |
|
public function compareWeakly(self $entityTag): bool |
|
122
|
|
|
{ |
|
123
|
8 |
|
return $this->value === $entityTag->value; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|