AcceptHeaderItem.php 3.6KB

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