|
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\Date; |
|
9
|
|
|
use Stadly\Http\Utilities\Rfc7232; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class for handling the HTTP header field If-Unmodified-Since. |
|
13
|
|
|
* |
|
14
|
|
|
* Specification: https://tools.ietf.org/html/rfc7232#section-3.4 |
|
15
|
|
|
*/ |
|
16
|
|
|
final class IfUnmodifiedSince implements Header |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Date Date. |
|
20
|
|
|
*/ |
|
21
|
|
|
private $date; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param Date $date Date. |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function __construct(Date $date) |
|
29
|
|
|
{ |
|
30
|
1 |
|
$this->setDate($date); |
|
31
|
1 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Construct header from value. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $value Header value. |
|
37
|
|
|
* @return self Header generated based on the value. |
|
38
|
|
|
* @throws InvalidHeader If the header value is invalid. |
|
39
|
|
|
*/ |
|
40
|
2 |
|
public static function fromValue(string $value): self |
|
41
|
|
|
{ |
|
42
|
2 |
|
$regEx = '{^' . Rfc7232::IF_UNMODIFIED_SINCE . '$}'; |
|
43
|
2 |
|
if (utf8_decode($value) !== $value || preg_match($regEx, $value) !== 1) { |
|
44
|
1 |
|
throw new InvalidHeader('Invalid header value: ' . $value); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
return new self(Date::fromString($value)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritDoc |
|
52
|
|
|
* @throws void The header is always valid. |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function __toString(): string |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->getName() . ': ' . $this->getValue(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return true The header is always valid. |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function isValid(): bool |
|
63
|
|
|
{ |
|
64
|
1 |
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritDoc |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getName(): string |
|
71
|
|
|
{ |
|
72
|
1 |
|
return 'If-Unmodified-Since'; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritDoc |
|
77
|
|
|
* @throws void The header is always valid. |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function getValue(): string |
|
80
|
|
|
{ |
|
81
|
1 |
|
return (string)$this->date; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param Date|null $lastModifiedDate Last modified date, or null if unknown. |
|
86
|
|
|
* @return bool Whether the condition is satisfied by the last modified date. |
|
87
|
|
|
*/ |
|
88
|
4 |
|
public function evaluate(?Date $lastModifiedDate): bool |
|
89
|
|
|
{ |
|
90
|
|
|
// If last modified date is unknown, the condition is not satisfied. |
|
91
|
4 |
|
if ($lastModifiedDate === null) { |
|
92
|
1 |
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
return $lastModifiedDate->isLte($this->date); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return Date Date. |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function getDate(): Date |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->date; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set date. |
|
108
|
|
|
* |
|
109
|
|
|
* @param Date $date Date. |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function setDate(Date $date): void |
|
112
|
|
|
{ |
|
113
|
1 |
|
$this->date = $date; |
|
114
|
1 |
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|