4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpFoundation;
15 * Represents an Accept-* header item.
17 * @author Jean-François Simon <contact@jfsimon.fr>
19 class AcceptHeaderItem
29 private $quality = 1.0;
39 private $attributes = array();
44 * @param string $value
45 * @param array $attributes
47 public function __construct($value, array $attributes = array())
49 $this->value = $value;
50 foreach ($attributes as $name => $value) {
51 $this->setAttribute($name, $value);
56 * Builds an AcceptHeaderInstance instance from a string.
58 * @param string $itemValue
62 public static function fromString($itemValue)
64 $bits = preg_split('/\s*(?:;*("[^"]+");*|;*(\'[^\']+\');*|;+)\s*/', $itemValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
65 $value = array_shift($bits);
66 $attributes = array();
68 $lastNullAttribute = null;
69 foreach ($bits as $bit) {
70 if (($start = substr($bit, 0, 1)) === ($end = substr($bit, -1)) && ($start === '"' || $start === '\'')) {
71 $attributes[$lastNullAttribute] = substr($bit, 1, -1);
72 } elseif ('=' === $end) {
73 $lastNullAttribute = $bit = substr($bit, 0, -1);
74 $attributes[$bit] = null;
76 $parts = explode('=', $bit);
77 $attributes[$parts[0]] = isset($parts[1]) && strlen($parts[1]) > 0 ? $parts[1] : '';
81 return new self(($start = substr($value, 0, 1)) === ($end = substr($value, -1)) && ($start === '"' || $start === '\'') ? substr($value, 1, -1) : $value, $attributes);
85 * Returns header value's string representation.
89 public function __toString()
91 $string = $this->value.($this->quality < 1 ? ';q='.$this->quality : '');
92 if (count($this->attributes) > 0) {
93 $string .= ';'.implode(';', array_map(function ($name, $value) {
94 return sprintf(preg_match('/[,;=]/', $value) ? '%s="%s"' : '%s=%s', $name, $value);
95 }, array_keys($this->attributes), $this->attributes));
102 * Set the item value.
104 * @param string $value
108 public function setValue($value)
110 $this->value = $value;
116 * Returns the item value.
120 public function getValue()
126 * Set the item quality.
128 * @param float $quality
132 public function setQuality($quality)
134 $this->quality = $quality;
140 * Returns the item quality.
144 public function getQuality()
146 return $this->quality;
150 * Set the item index.
156 public function setIndex($index)
158 $this->index = $index;
164 * Returns the item index.
168 public function getIndex()
174 * Tests if an attribute exists.
176 * @param string $name
180 public function hasAttribute($name)
182 return isset($this->attributes[$name]);
186 * Returns an attribute by its name.
188 * @param string $name
189 * @param mixed $default
193 public function getAttribute($name, $default = null)
195 return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
199 * Returns all attributes.
203 public function getAttributes()
205 return $this->attributes;
211 * @param string $name
212 * @param string $value
216 public function setAttribute($name, $value)
219 $this->quality = (float) $value;
221 $this->attributes[$name] = (string) $value;