AcceptHeaderItem.php 3.3 KB

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