|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Http\Header; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Yiisoft\Http\Header\Internal\DirectivesHeaderValue; |
|
9
|
|
|
use Yiisoft\Http\Header\Value\Unnamed\DirectiveValue; |
|
10
|
|
|
|
|
11
|
|
|
final class DirectiveHeader extends Header |
|
12
|
|
|
{ |
|
13
|
|
|
protected const DEFAULT_VALUE_CLASS = DirectiveValue::class; |
|
14
|
|
|
|
|
15
|
6 |
|
public function __construct(string $nameOrClass) |
|
16
|
|
|
{ |
|
17
|
6 |
|
parent::__construct($nameOrClass); |
|
18
|
6 |
|
if (!is_a($this->headerClass, DirectivesHeaderValue::class, true)) { |
|
19
|
1 |
|
throw new InvalidArgumentException( |
|
20
|
1 |
|
sprintf('%s class is not an instance of %s', $this->headerClass, DirectivesHeaderValue::class) |
|
21
|
|
|
); |
|
22
|
|
|
} |
|
23
|
5 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function withDirective(string $directive, string $argument = null): self |
|
26
|
|
|
{ |
|
27
|
|
|
$clone = clone $this; |
|
28
|
|
|
/** @var DirectivesHeaderValue $headerValue */ |
|
29
|
|
|
$headerValue = new $this->headerClass(); |
|
30
|
|
|
$clone->addValue($headerValue->withDirective($directive, $argument)); |
|
31
|
|
|
return $clone; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param bool $ignoreIncorrect |
|
36
|
|
|
* @return null[][]|string[][] Returns array of array<directive name => directive value> |
|
37
|
|
|
* @psalm-return array<int, array<string, null|string>>|array<empty, empty> |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getDirectives(bool $ignoreIncorrect = true): array |
|
40
|
|
|
{ |
|
41
|
|
|
$result = []; |
|
42
|
|
|
/** @var DirectivesHeaderValue $header */ |
|
43
|
|
|
foreach ($this->collection as $header) { |
|
44
|
|
|
if ($ignoreIncorrect && $header->hasError()) { |
|
45
|
|
|
continue; |
|
46
|
|
|
} |
|
47
|
|
|
$result[] = [$header->getDirective() => $header->getArgument()]; |
|
48
|
|
|
} |
|
49
|
|
|
return $result; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|