1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Http\Header; |
6
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
use Yiisoft\Http\Header\Value\Accept\Accept; |
9
|
|
|
use Yiisoft\Http\Header\Internal\BaseHeaderValue; |
10
|
|
|
|
11
|
|
|
final class AcceptHeader extends Header |
12
|
|
|
{ |
13
|
|
|
// todo: comparing |
14
|
|
|
protected const DEFAULT_VALUE_CLASS = Accept::class; |
15
|
|
|
|
16
|
14 |
|
public function __construct(string $nameOrClass) |
17
|
|
|
{ |
18
|
14 |
|
parent::__construct($nameOrClass); |
19
|
14 |
|
if (!is_a($this->headerClass, Accept::class, true)) { |
20
|
1 |
|
throw new InvalidArgumentException( |
21
|
1 |
|
sprintf('%s class is not an instance of %s', $this->headerClass, Accept::class) |
22
|
|
|
); |
23
|
|
|
} |
24
|
13 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Add value in order |
28
|
|
|
*/ |
29
|
12 |
|
protected function collect(BaseHeaderValue $value): void |
30
|
|
|
{ |
31
|
12 |
|
if (count($this->collection) === 0) { |
32
|
12 |
|
$this->collection[] = $value; |
33
|
12 |
|
return; |
34
|
|
|
} |
35
|
11 |
|
for ($pos = array_key_last($this->collection); $pos >= 0; --$pos) { |
36
|
11 |
|
$item = $this->collection[$pos]; |
37
|
11 |
|
$result = (float)$item->getQuality() <=> (float)$value->getQuality(); |
|
|
|
|
38
|
11 |
|
if ($result > 0) { |
39
|
5 |
|
break; |
40
|
|
|
} |
41
|
11 |
|
if ($result === 0) { |
42
|
10 |
|
$separator = $this->headerClass::VALUE_SEPARATOR; |
43
|
10 |
|
if ($separator !== '') { |
44
|
6 |
|
$itemTypes = array_reverse(explode($separator, $item->getValue())); |
45
|
6 |
|
$valueTypes = array_reverse(explode($separator, $value->getValue())); |
46
|
|
|
} else { |
47
|
4 |
|
$itemTypes = [$item->getValue()]; |
48
|
4 |
|
$valueTypes = [$value->getValue()]; |
49
|
|
|
} |
50
|
10 |
|
$result = count($itemTypes) <=> count($valueTypes); |
51
|
10 |
|
if ($result > 0) { |
52
|
3 |
|
break; |
53
|
|
|
} |
54
|
10 |
|
if ($result === 0) { |
55
|
9 |
|
foreach ($itemTypes as $part => $itemType) { |
56
|
9 |
|
if ($itemType === '*' xor $valueTypes[$part] === '*') { |
57
|
7 |
|
if ($itemType !== '*') { |
58
|
4 |
|
break 2; |
59
|
|
|
} |
60
|
7 |
|
$this->collection[$pos + 1] = $item; |
61
|
7 |
|
continue 2; |
62
|
|
|
} |
63
|
|
|
} |
64
|
8 |
|
if (count($item->getParams()) >= count($value->getParams())) { |
|
|
|
|
65
|
7 |
|
break; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
8 |
|
$this->collection[$pos + 1] = $item; |
70
|
|
|
} |
71
|
11 |
|
$this->collection[$pos + 1] = $value; |
72
|
11 |
|
} |
73
|
|
|
} |
74
|
|
|
|