composer package updates
[openemr.git] / vendor / symfony / http-foundation / AcceptHeaderItem.php
blobc69dbbba3566366fc1f9d4e2f12271851793dcd2
1 <?php
3 /*
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;
14 /**
15 * Represents an Accept-* header item.
17 * @author Jean-François Simon <contact@jfsimon.fr>
19 class AcceptHeaderItem
21 private $value;
22 private $quality = 1.0;
23 private $index = 0;
24 private $attributes = array();
26 /**
27 * @param string $value
28 * @param array $attributes
30 public function __construct($value, array $attributes = array())
32 $this->value = $value;
33 foreach ($attributes as $name => $value) {
34 $this->setAttribute($name, $value);
38 /**
39 * Builds an AcceptHeaderInstance instance from a string.
41 * @param string $itemValue
43 * @return self
45 public static function fromString($itemValue)
47 $bits = preg_split('/\s*(?:;*("[^"]+");*|;*(\'[^\']+\');*|;+)\s*/', $itemValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
48 $value = array_shift($bits);
49 $attributes = array();
51 $lastNullAttribute = null;
52 foreach ($bits as $bit) {
53 if (($start = substr($bit, 0, 1)) === ($end = substr($bit, -1)) && ('"' === $start || '\'' === $start)) {
54 $attributes[$lastNullAttribute] = substr($bit, 1, -1);
55 } elseif ('=' === $end) {
56 $lastNullAttribute = $bit = substr($bit, 0, -1);
57 $attributes[$bit] = null;
58 } else {
59 $parts = explode('=', $bit);
60 $attributes[$parts[0]] = isset($parts[1]) && strlen($parts[1]) > 0 ? $parts[1] : '';
64 return new self(($start = substr($value, 0, 1)) === ($end = substr($value, -1)) && ('"' === $start || '\'' === $start) ? substr($value, 1, -1) : $value, $attributes);
67 /**
68 * Returns header value's string representation.
70 * @return string
72 public function __toString()
74 $string = $this->value.($this->quality < 1 ? ';q='.$this->quality : '');
75 if (count($this->attributes) > 0) {
76 $string .= ';'.implode(';', array_map(function ($name, $value) {
77 return sprintf(preg_match('/[,;=]/', $value) ? '%s="%s"' : '%s=%s', $name, $value);
78 }, array_keys($this->attributes), $this->attributes));
81 return $string;
84 /**
85 * Set the item value.
87 * @param string $value
89 * @return $this
91 public function setValue($value)
93 $this->value = $value;
95 return $this;
98 /**
99 * Returns the item value.
101 * @return string
103 public function getValue()
105 return $this->value;
109 * Set the item quality.
111 * @param float $quality
113 * @return $this
115 public function setQuality($quality)
117 $this->quality = $quality;
119 return $this;
123 * Returns the item quality.
125 * @return float
127 public function getQuality()
129 return $this->quality;
133 * Set the item index.
135 * @param int $index
137 * @return $this
139 public function setIndex($index)
141 $this->index = $index;
143 return $this;
147 * Returns the item index.
149 * @return int
151 public function getIndex()
153 return $this->index;
157 * Tests if an attribute exists.
159 * @param string $name
161 * @return bool
163 public function hasAttribute($name)
165 return isset($this->attributes[$name]);
169 * Returns an attribute by its name.
171 * @param string $name
172 * @param mixed $default
174 * @return mixed
176 public function getAttribute($name, $default = null)
178 return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
182 * Returns all attributes.
184 * @return array
186 public function getAttributes()
188 return $this->attributes;
192 * Set an attribute.
194 * @param string $name
195 * @param string $value
197 * @return $this
199 public function setAttribute($name, $value)
201 if ('q' === $name) {
202 $this->quality = (float) $value;
203 } else {
204 $this->attributes[$name] = (string) $value;
207 return $this;