| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
> |
| 18 |
|
| 19 |
class AcceptHeaderItem |
| 20 |
{ |
| 21 |
private float $quality = 1.0; |
| 22 |
private int $index = 0; |
| 23 |
private array $attributes = []; |
| 24 |
|
| 25 |
public function __construct( |
| 26 |
private string $value, |
| 27 |
array $attributes = [], |
| 28 |
) { |
| 29 |
foreach ($attributes as $name => $value) { |
| 30 |
$this->setAttribute($name, $value); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public static function fromString(?string $itemValue): self |
| 38 |
{ |
| 39 |
$parts = HeaderUtils::split($itemValue ?? '', ';='); |
| 40 |
|
| 41 |
$part = array_shift($parts); |
| 42 |
$attributes = HeaderUtils::combine($parts); |
| 43 |
|
| 44 |
return new self($part[0], $attributes); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __toString(): string |
| 51 |
{ |
| 52 |
$string = $this->value.($this->quality < 1 ? ';q='.$this->quality : ''); |
| 53 |
if (\count($this->attributes) > 0) { |
| 54 |
$string .= '; '.HeaderUtils::toString($this->attributes, ';'); |
| 55 |
} |
| 56 |
|
| 57 |
return $string; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function setValue(string $value): static |
| 66 |
{ |
| 67 |
$this->value = $value; |
| 68 |
|
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function getValue(): string |
| 76 |
{ |
| 77 |
return $this->value; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function setQuality(float $quality): static |
| 86 |
{ |
| 87 |
$this->quality = $quality; |
| 88 |
|
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getQuality(): float |
| 96 |
{ |
| 97 |
return $this->quality; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
public function setIndex(int $index): static |
| 106 |
{ |
| 107 |
$this->index = $index; |
| 108 |
|
| 109 |
return $this; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public function getIndex(): int |
| 116 |
{ |
| 117 |
return $this->index; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
public function hasAttribute(string $name): bool |
| 124 |
{ |
| 125 |
return isset($this->attributes[$name]); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function getAttribute(string $name, mixed $default = null): mixed |
| 132 |
{ |
| 133 |
return $this->attributes[$name] ?? $default; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
public function getAttributes(): array |
| 140 |
{ |
| 141 |
return $this->attributes; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function setAttribute(string $name, string $value): static |
| 150 |
{ |
| 151 |
if ('q' === $name) { |
| 152 |
$this->quality = (float) $value; |
| 153 |
} else { |
| 154 |
$this->attributes[$name] = $value; |
| 155 |
} |
| 156 |
|
| 157 |
return $this; |
| 158 |
} |
| 159 |
} |