AcceptHeaderItem.php 3.2 KB

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