You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

209 lines
4.5 KiB

  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. private $value;
  19. private $quality = 1.0;
  20. private $index = 0;
  21. private $attributes = [];
  22. /**
  23. * @param string $value
  24. */
  25. public function __construct($value, array $attributes = [])
  26. {
  27. $this->value = $value;
  28. foreach ($attributes as $name => $value) {
  29. $this->setAttribute($name, $value);
  30. }
  31. }
  32. /**
  33. * Builds an AcceptHeaderInstance instance from a string.
  34. *
  35. * @param string $itemValue
  36. *
  37. * @return self
  38. */
  39. public static function fromString($itemValue)
  40. {
  41. $bits = preg_split('/\s*(?:;*("[^"]+");*|;*(\'[^\']+\');*|;+)\s*/', $itemValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  42. $value = array_shift($bits);
  43. $attributes = [];
  44. $lastNullAttribute = null;
  45. foreach ($bits as $bit) {
  46. if (($start = substr($bit, 0, 1)) === ($end = substr($bit, -1)) && ('"' === $start || '\'' === $start)) {
  47. $attributes[$lastNullAttribute] = substr($bit, 1, -1);
  48. } elseif ('=' === $end) {
  49. $lastNullAttribute = $bit = substr($bit, 0, -1);
  50. $attributes[$bit] = null;
  51. } else {
  52. $parts = explode('=', $bit);
  53. $attributes[$parts[0]] = isset($parts[1]) && \strlen($parts[1]) > 0 ? $parts[1] : '';
  54. }
  55. }
  56. return new self(($start = substr($value, 0, 1)) === ($end = substr($value, -1)) && ('"' === $start || '\'' === $start) ? substr($value, 1, -1) : $value, $attributes);
  57. }
  58. /**
  59. * Returns header value's string representation.
  60. *
  61. * @return string
  62. */
  63. public function __toString()
  64. {
  65. $string = $this->value.($this->quality < 1 ? ';q='.$this->quality : '');
  66. if (\count($this->attributes) > 0) {
  67. $string .= ';'.implode(';', array_map(function ($name, $value) {
  68. return sprintf(preg_match('/[,;=]/', $value) ? '%s="%s"' : '%s=%s', $name, $value);
  69. }, array_keys($this->attributes), $this->attributes));
  70. }
  71. return $string;
  72. }
  73. /**
  74. * Set the item value.
  75. *
  76. * @param string $value
  77. *
  78. * @return $this
  79. */
  80. public function setValue($value)
  81. {
  82. $this->value = $value;
  83. return $this;
  84. }
  85. /**
  86. * Returns the item value.
  87. *
  88. * @return string
  89. */
  90. public function getValue()
  91. {
  92. return $this->value;
  93. }
  94. /**
  95. * Set the item quality.
  96. *
  97. * @param float $quality
  98. *
  99. * @return $this
  100. */
  101. public function setQuality($quality)
  102. {
  103. $this->quality = $quality;
  104. return $this;
  105. }
  106. /**
  107. * Returns the item quality.
  108. *
  109. * @return float
  110. */
  111. public function getQuality()
  112. {
  113. return $this->quality;
  114. }
  115. /**
  116. * Set the item index.
  117. *
  118. * @param int $index
  119. *
  120. * @return $this
  121. */
  122. public function setIndex($index)
  123. {
  124. $this->index = $index;
  125. return $this;
  126. }
  127. /**
  128. * Returns the item index.
  129. *
  130. * @return int
  131. */
  132. public function getIndex()
  133. {
  134. return $this->index;
  135. }
  136. /**
  137. * Tests if an attribute exists.
  138. *
  139. * @param string $name
  140. *
  141. * @return bool
  142. */
  143. public function hasAttribute($name)
  144. {
  145. return isset($this->attributes[$name]);
  146. }
  147. /**
  148. * Returns an attribute by its name.
  149. *
  150. * @param string $name
  151. * @param mixed $default
  152. *
  153. * @return mixed
  154. */
  155. public function getAttribute($name, $default = null)
  156. {
  157. return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
  158. }
  159. /**
  160. * Returns all attributes.
  161. *
  162. * @return array
  163. */
  164. public function getAttributes()
  165. {
  166. return $this->attributes;
  167. }
  168. /**
  169. * Set an attribute.
  170. *
  171. * @param string $name
  172. * @param string $value
  173. *
  174. * @return $this
  175. */
  176. public function setAttribute($name, $value)
  177. {
  178. if ('q' === $name) {
  179. $this->quality = (float) $value;
  180. } else {
  181. $this->attributes[$name] = (string) $value;
  182. }
  183. return $this;
  184. }
  185. }