|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Http\Header\Internal; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Psr\Http\Message\MessageInterface; |
|
9
|
|
|
use Yiisoft\Http\Header\Header; |
|
10
|
|
|
use Yiisoft\Http\Header\Parser\HeaderParsingParams; |
|
11
|
|
|
|
|
12
|
|
|
abstract class BaseHeaderValue |
|
13
|
|
|
{ |
|
14
|
|
|
public const NAME = null; |
|
15
|
|
|
|
|
16
|
|
|
protected string $value; |
|
17
|
|
|
protected ?Exception $error = null; |
|
18
|
|
|
|
|
19
|
|
|
protected const PARSING_LIST = false; |
|
20
|
|
|
protected const PARSING_Q_PARAM = false; |
|
21
|
|
|
protected const PARSING_PARAMS = false; |
|
22
|
|
|
|
|
23
|
223 |
|
public function __construct(string $value = '') |
|
24
|
|
|
{ |
|
25
|
223 |
|
$this->setValue($value); |
|
26
|
223 |
|
} |
|
27
|
12 |
|
public function __toString(): string |
|
28
|
|
|
{ |
|
29
|
12 |
|
return $this->value; |
|
30
|
|
|
} |
|
31
|
|
|
final public function inject(MessageInterface $message, bool $replace = true): MessageInterface |
|
32
|
|
|
{ |
|
33
|
|
|
if (static::NAME === null) { |
|
|
|
|
|
|
34
|
|
|
throw new \RuntimeException('Can not inject unnamed header value'); |
|
35
|
|
|
} |
|
36
|
|
|
if ($replace) { |
|
37
|
|
|
$message = $message->withoutHeader(static::NAME); |
|
38
|
|
|
} |
|
39
|
|
|
return $message->withAddedHeader(static::NAME, $this->__toString()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
public static function createHeader(): Header |
|
43
|
|
|
{ |
|
44
|
1 |
|
return new Header(static::class); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public static function extract(MessageInterface $message): Header |
|
48
|
|
|
{ |
|
49
|
|
|
return (new Header(static::class))->extract($message); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
66 |
|
public function withValue(string $value): self |
|
53
|
|
|
{ |
|
54
|
66 |
|
$clone = clone $this; |
|
55
|
66 |
|
$clone->setValue($value); |
|
56
|
66 |
|
return $clone; |
|
57
|
|
|
} |
|
58
|
84 |
|
public function getValue(): string |
|
59
|
|
|
{ |
|
60
|
84 |
|
return $this->value; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param Exception|null $error |
|
65
|
|
|
*/ |
|
66
|
20 |
|
final public function withError(?Exception $error): self |
|
67
|
|
|
{ |
|
68
|
20 |
|
$clone = clone $this; |
|
69
|
20 |
|
$clone->error = $error; |
|
70
|
20 |
|
return $clone; |
|
71
|
|
|
} |
|
72
|
167 |
|
final public function hasError(): bool |
|
73
|
|
|
{ |
|
74
|
167 |
|
return $this->error !== null; |
|
75
|
|
|
} |
|
76
|
|
|
final public function getError(): ?Exception |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->error; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
87 |
|
final public static function getParsingParams(): HeaderParsingParams |
|
82
|
|
|
{ |
|
83
|
87 |
|
$params = new HeaderParsingParams(); |
|
84
|
87 |
|
$params->directives = is_subclass_of(static::class, DirectivesHeaderValue::class, true); |
|
85
|
87 |
|
$params->valuesList = $params->directives || static::PARSING_LIST; |
|
86
|
87 |
|
$params->withParams = $params->directives || static::PARSING_PARAMS; |
|
87
|
87 |
|
$params->q = static::PARSING_Q_PARAM; |
|
88
|
87 |
|
return $params; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
166 |
|
protected function setValue(string $value): void |
|
92
|
|
|
{ |
|
93
|
166 |
|
$this->value = $value; |
|
94
|
166 |
|
} |
|
95
|
59 |
|
final protected function encodeQuotedString(string $string): string |
|
96
|
|
|
{ |
|
97
|
59 |
|
return preg_replace('/([\\\\"])/', '\\\\$1', $string); |
|
98
|
|
|
} |
|
99
|
22 |
|
final protected function validateDateTime(string $value): bool |
|
100
|
|
|
{ |
|
101
|
22 |
|
return preg_match( |
|
102
|
|
|
'/^\\w{3,}, [0-3]?\\d[ \\-]\\w{3}[ \\-]\\d+ [0-2]\\d:[0-5]\\d:[0-5]\\d \\w+|' |
|
103
|
22 |
|
. '\\w{3} \\w{3} [0-3]?\\d [0-2]\\d:[0-5]\\d:[0-5]\\d \\d+$/i', |
|
104
|
22 |
|
trim($value) |
|
105
|
22 |
|
) === 1; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|