AcceptHeader.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // Help opcache.preload discover always-needed symbols
  12. class_exists(AcceptHeaderItem::class);
  13. /**
  14. * Represents an Accept-* header.
  15. *
  16. * An accept header is compound with a list of items,
  17. * sorted by descending quality.
  18. *
  19. * @author Jean-François Simon <contact@jfsimon.fr>
  20. */
  21. class AcceptHeader
  22. {
  23. /**
  24. * @var AcceptHeaderItem[]
  25. */
  26. private $items = [];
  27. /**
  28. * @var bool
  29. */
  30. private $sorted = true;
  31. /**
  32. * @param AcceptHeaderItem[] $items
  33. */
  34. public function __construct(array $items)
  35. {
  36. foreach ($items as $item) {
  37. $this->add($item);
  38. }
  39. }
  40. /**
  41. * Builds an AcceptHeader instance from a string.
  42. *
  43. * @param string $headerValue
  44. *
  45. * @return self
  46. */
  47. public static function fromString($headerValue)
  48. {
  49. $index = 0;
  50. $parts = HeaderUtils::split((string) $headerValue, ',;=');
  51. return new self(array_map(function ($subParts) use (&$index) {
  52. $part = array_shift($subParts);
  53. $attributes = HeaderUtils::combine($subParts);
  54. $item = new AcceptHeaderItem($part[0], $attributes);
  55. $item->setIndex($index++);
  56. return $item;
  57. }, $parts));
  58. }
  59. /**
  60. * Returns header value's string representation.
  61. *
  62. * @return string
  63. */
  64. public function __toString()
  65. {
  66. return implode(',', $this->items);
  67. }
  68. /**
  69. * Tests if header has given value.
  70. *
  71. * @param string $value
  72. *
  73. * @return bool
  74. */
  75. public function has($value)
  76. {
  77. return isset($this->items[$value]);
  78. }
  79. /**
  80. * Returns given value's item, if exists.
  81. *
  82. * @param string $value
  83. *
  84. * @return AcceptHeaderItem|null
  85. */
  86. public function get($value)
  87. {
  88. return $this->items[$value] ?? $this->items[explode('/', $value)[0].'/*'] ?? $this->items['*/*'] ?? $this->items['*'] ?? null;
  89. }
  90. /**
  91. * Adds an item.
  92. *
  93. * @return $this
  94. */
  95. public function add(AcceptHeaderItem $item)
  96. {
  97. $this->items[$item->getValue()] = $item;
  98. $this->sorted = false;
  99. return $this;
  100. }
  101. /**
  102. * Returns all items.
  103. *
  104. * @return AcceptHeaderItem[]
  105. */
  106. public function all()
  107. {
  108. $this->sort();
  109. return $this->items;
  110. }
  111. /**
  112. * Filters items on their value using given regex.
  113. *
  114. * @param string $pattern
  115. *
  116. * @return self
  117. */
  118. public function filter($pattern)
  119. {
  120. return new self(array_filter($this->items, function (AcceptHeaderItem $item) use ($pattern) {
  121. return preg_match($pattern, $item->getValue());
  122. }));
  123. }
  124. /**
  125. * Returns first item.
  126. *
  127. * @return AcceptHeaderItem|null
  128. */
  129. public function first()
  130. {
  131. $this->sort();
  132. return !empty($this->items) ? reset($this->items) : null;
  133. }
  134. /**
  135. * Sorts items by descending quality.
  136. */
  137. private function sort(): void
  138. {
  139. if (!$this->sorted) {
  140. uasort($this->items, function (AcceptHeaderItem $a, AcceptHeaderItem $b) {
  141. $qA = $a->getQuality();
  142. $qB = $b->getQuality();
  143. if ($qA === $qB) {
  144. return $a->getIndex() > $b->getIndex() ? 1 : -1;
  145. }
  146. return $qA > $qB ? -1 : 1;
  147. });
  148. $this->sorted = true;
  149. }
  150. }
  151. }