15 class_exists(AcceptHeaderItem::class);
35 private $sorted =
true;
42 foreach ($items as $item) {
54 public static function fromString($headerValue)
60 return new self(array_map(
function ($subParts) use (&$index) {
61 $part = array_shift($subParts);
65 $item->setIndex($index++);
78 return implode(
',', $this->items);
88 public function has($value)
90 return isset($this->items[$value]);
100 public function get($value)
102 return $this->items[$value] ?? $this->items[explode(
'/', $value)[0].
'/*'] ?? $this->items[
'*/*'] ?? $this->items[
'*'] ??
null;
110 public function add(AcceptHeaderItem $item)
112 $this->items[$item->getValue()] = $item;
113 $this->sorted =
false;
123 public function all()
137 public function filter($pattern)
139 return new self(array_filter($this->items,
function (
AcceptHeaderItem $item) use ($pattern) {
140 return preg_match($pattern, $item->
getValue());
149 public function first()
153 return !empty($this->items) ? reset($this->items) :
null;
159 private function sort(): void
161 if (!$this->sorted) {
170 return $qA > $qB ? -1 : 1;
173 $this->sorted =
true;