| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation; |
| 13 |
|
| 14 |
/** |
| 15 |
* Represents an Accept-* header item. |
| 16 |
* |
| 17 |
* @author Jean-François Simon <contact@jfsimon.fr> |
| 18 |
*/ |
| 19 |
class AcceptHeaderItem |
| 20 |
{ |
| 21 |
private $value; |
| 22 |
private $quality = 1.0; |
| 23 |
private $index = 0; |
| 24 |
private $attributes = []; |
| 25 |
|
| 26 |
public function __construct(string $value, array $attributes = []) |
| 27 |
{ |
| 28 |
$this->value = $value; |
| 29 |
foreach ($attributes as $name => $value) { |
| 30 |
$this->setAttribute($name, $value); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Builds an AcceptHeaderInstance instance from a string. |
| 36 |
* |
| 37 |
* @return self |
| 38 |
*/ |
| 39 |
public static function fromString(?string $itemValue) |
| 40 |
{ |
| 41 |
$parts = HeaderUtils::split($itemValue ?? '', ';='); |
| 42 |
|
| 43 |
$part = array_shift($parts); |
| 44 |
$attributes = HeaderUtils::combine($parts); |
| 45 |
|
| 46 |
return new self($part[0], $attributes); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Returns header value's string representation. |
| 51 |
* |
| 52 |
* @return string |
| 53 |
*/ |
| 54 |
public function __toString() |
| 55 |
{ |
| 56 |
$string = $this->value.($this->quality < 1 ? ';q='.$this->quality : ''); |
| 57 |
if (\count($this->attributes) > 0) { |
| 58 |
$string .= '; '.HeaderUtils::toString($this->attributes, ';'); |
| 59 |
} |
| 60 |
|
| 61 |
return $string; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Set the item value. |
| 66 |
* |
| 67 |
* @return $this |
| 68 |
*/ |
| 69 |
public function setValue(string $value) |
| 70 |
{ |
| 71 |
$this->value = $value; |
| 72 |
|
| 73 |
return $this; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns the item value. |
| 78 |
* |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
public function getValue() |
| 82 |
{ |
| 83 |
return $this->value; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Set the item quality. |
| 88 |
* |
| 89 |
* @return $this |
| 90 |
*/ |
| 91 |
public function setQuality(float $quality) |
| 92 |
{ |
| 93 |
$this->quality = $quality; |
| 94 |
|
| 95 |
return $this; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Returns the item quality. |
| 100 |
* |
| 101 |
* @return float |
| 102 |
*/ |
| 103 |
public function getQuality() |
| 104 |
{ |
| 105 |
return $this->quality; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Set the item index. |
| 110 |
* |
| 111 |
* @return $this |
| 112 |
*/ |
| 113 |
public function setIndex(int $index) |
| 114 |
{ |
| 115 |
$this->index = $index; |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Returns the item index. |
| 122 |
* |
| 123 |
* @return int |
| 124 |
*/ |
| 125 |
public function getIndex() |
| 126 |
{ |
| 127 |
return $this->index; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Tests if an attribute exists. |
| 132 |
* |
| 133 |
* @return bool |
| 134 |
*/ |
| 135 |
public function hasAttribute(string $name) |
| 136 |
{ |
| 137 |
return isset($this->attributes[$name]); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Returns an attribute by its name. |
| 142 |
* |
| 143 |
* @param mixed $default |
| 144 |
* |
| 145 |
* @return mixed |
| 146 |
*/ |
| 147 |
public function getAttribute(string $name, $default = null) |
| 148 |
{ |
| 149 |
return $this->attributes[$name] ?? $default; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Returns all attributes. |
| 154 |
* |
| 155 |
* @return array |
| 156 |
*/ |
| 157 |
public function getAttributes() |
| 158 |
{ |
| 159 |
return $this->attributes; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Set an attribute. |
| 164 |
* |
| 165 |
* @return $this |
| 166 |
*/ |
| 167 |
public function setAttribute(string $name, string $value) |
| 168 |
{ |
| 169 |
if ('q' === $name) { |
| 170 |
$this->quality = (float) $value; |
| 171 |
} else { |
| 172 |
$this->attributes[$name] = $value; |
| 173 |
} |
| 174 |
|
| 175 |
return $this; |
| 176 |
} |
| 177 |
} |
| 178 |
|