|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message\Header; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Import classes |
|
16
|
|
|
*/ |
|
17
|
|
|
use DateTimeInterface; |
|
18
|
|
|
use Sunrise\Http\Message\Exception\InvalidHeaderValueException; |
|
19
|
|
|
use Sunrise\Http\Message\Header; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Import functions |
|
23
|
|
|
*/ |
|
24
|
|
|
use function sprintf; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @link https://tools.ietf.org/html/rfc2616#section-14.46 |
|
28
|
|
|
*/ |
|
29
|
|
|
class WarningHeader extends Header |
|
30
|
|
|
{ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* HTTP Warning Codes |
|
34
|
|
|
* |
|
35
|
|
|
* @link https://www.iana.org/assignments/http-warn-codes/http-warn-codes.xhtml |
|
36
|
|
|
*/ |
|
37
|
|
|
public const HTTP_WARNING_CODE_RESPONSE_IS_STALE = 110; |
|
38
|
|
|
public const HTTP_WARNING_CODE_REVALIDATION_FAILED = 111; |
|
39
|
|
|
public const HTTP_WARNING_CODE_DISCONNECTED_OPERATION = 112; |
|
40
|
|
|
public const HTTP_WARNING_CODE_HEURISTIC_EXPIRATION = 113; |
|
41
|
|
|
public const HTTP_WARNING_CODE_MISCELLANEOUS_WARNING = 199; |
|
42
|
|
|
public const HTTP_WARNING_CODE_TRANSFORMATION_APPLIED = 214; |
|
43
|
|
|
public const HTTP_WARNING_CODE_MISCELLANEOUS_PERSISTENT_WARNING = 299; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @var int |
|
47
|
|
|
*/ |
|
48
|
|
|
private int $code; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
private string $agent; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @var string |
|
57
|
|
|
*/ |
|
58
|
|
|
private string $text; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @var DateTimeInterface|null |
|
62
|
|
|
*/ |
|
63
|
|
|
private ?DateTimeInterface $date; |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Constructor of the class |
|
67
|
|
|
* |
|
68
|
|
|
* @param int $code |
|
69
|
|
|
* @param string $agent |
|
70
|
|
|
* @param string $text |
|
71
|
|
|
* @param DateTimeInterface|null $date |
|
72
|
|
|
* |
|
73
|
|
|
* @throws InvalidHeaderValueException |
|
74
|
|
|
* If one of parameters isn't valid. |
|
75
|
|
|
*/ |
|
76
|
12 |
|
public function __construct(int $code, string $agent, string $text, ?DateTimeInterface $date = null) |
|
77
|
|
|
{ |
|
78
|
12 |
|
$this->validateCode($code); |
|
79
|
10 |
|
$this->validateToken($agent); |
|
80
|
8 |
|
$this->validateQuotedString($text); |
|
81
|
|
|
|
|
82
|
7 |
|
$this->code = $code; |
|
83
|
7 |
|
$this->agent = $agent; |
|
84
|
7 |
|
$this->text = $text; |
|
85
|
7 |
|
$this->date = $date; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
8 |
|
public function getFieldName(): string |
|
92
|
|
|
{ |
|
93
|
8 |
|
return 'Warning'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* {@inheritdoc} |
|
98
|
|
|
*/ |
|
99
|
5 |
|
public function getFieldValue(): string |
|
100
|
|
|
{ |
|
101
|
5 |
|
$value = sprintf('%s %s "%s"', $this->code, $this->agent, $this->text); |
|
102
|
|
|
|
|
103
|
5 |
|
if (isset($this->date)) { |
|
104
|
1 |
|
$value .= sprintf(' "%s"', $this->formatDateTime($this->date)); |
|
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
5 |
|
return $value; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Validates the given code |
|
112
|
|
|
* |
|
113
|
|
|
* @param int $code |
|
114
|
|
|
* |
|
115
|
|
|
* @return void |
|
116
|
|
|
* |
|
117
|
|
|
* @throws InvalidHeaderValueException |
|
118
|
|
|
* If the code isn't valid. |
|
119
|
|
|
*/ |
|
120
|
12 |
|
private function validateCode(int $code): void |
|
121
|
|
|
{ |
|
122
|
12 |
|
if (! ($code >= 100 && $code <= 999)) { |
|
123
|
2 |
|
throw new InvalidHeaderValueException(sprintf( |
|
124
|
2 |
|
'The code "%2$d" for the header "%1$s" is not valid', |
|
125
|
2 |
|
$this->getFieldName(), |
|
126
|
2 |
|
$code |
|
127
|
2 |
|
)); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|