AcceptHeaderItem.php 4.6 KB

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