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\EntityTag\EntityTag; |
9
|
|
|
use Stadly\Http\Header\Value\EntityTag\EntityTagSet; |
10
|
|
|
use Stadly\Http\Utilities\Rfc7232; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class for handling the HTTP header field If-Match. |
14
|
|
|
* |
15
|
|
|
* Specification: https://tools.ietf.org/html/rfc7232#section-3.1 |
16
|
|
|
*/ |
17
|
|
|
final class IfMatch implements Header |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var EntityTagSet Entity tag set. |
21
|
|
|
*/ |
22
|
|
|
private $entityTagSet; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Constructor. |
26
|
|
|
* |
27
|
|
|
* @param EntityTagSet $entityTagSet Set of entity tags. |
28
|
|
|
*/ |
29
|
1 |
|
public function __construct(EntityTagSet $entityTagSet) |
30
|
|
|
{ |
31
|
1 |
|
$this->setEntityTagSet($entityTagSet); |
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
|
3 |
|
public static function fromValue(string $value): self |
42
|
|
|
{ |
43
|
3 |
|
$regEx = '{^' . Rfc7232::IF_MATCH . '$}'; |
44
|
3 |
|
$plainValue = mb_convert_encoding($value, 'ISO-8859-1', 'UTF-8'); |
45
|
3 |
|
if ($plainValue !== $value || preg_match($regEx, $value) !== 1) { |
46
|
1 |
|
throw new InvalidHeader('Invalid header value: ' . $value); |
47
|
|
|
} |
48
|
|
|
|
49
|
2 |
|
return new self(EntityTagSet::fromString($value)); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @inheritDoc |
54
|
|
|
* @throws void The header is always valid. |
55
|
|
|
*/ |
56
|
3 |
|
public function __toString(): string |
57
|
|
|
{ |
58
|
3 |
|
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 'If-Match'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritDoc |
79
|
|
|
* @throws void The header is always valid. |
80
|
|
|
*/ |
81
|
3 |
|
public function getValue(): string |
82
|
|
|
{ |
83
|
3 |
|
return (string)$this->entityTagSet; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param EntityTag|null $entityTag Entity tag, or null if unknown. |
88
|
|
|
* @return bool Whether the condition is satisfied by the entity tag. |
89
|
|
|
*/ |
90
|
10 |
|
public function evaluate(?EntityTag $entityTag): bool |
91
|
|
|
{ |
92
|
10 |
|
return $this->entityTagSet->compareStrongly($entityTag); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return EntityTagSet Set of entity tags. |
97
|
|
|
*/ |
98
|
1 |
|
public function getEntityTagSet(): EntityTagSet |
99
|
|
|
{ |
100
|
1 |
|
return $this->entityTagSet; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set entity tag set. |
105
|
|
|
* |
106
|
|
|
* @param EntityTagSet $entityTagSet Set of entity tags. |
107
|
|
|
*/ |
108
|
1 |
|
public function setEntityTagSet(EntityTagSet $entityTagSet): void |
109
|
|
|
{ |
110
|
1 |
|
$this->entityTagSet = $entityTagSet; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|