AcceptHeaderItem.php 4.8 KB

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