27 private $items = array();
32 private $sorted =
true;
41 foreach ($items as $item) {
53 public static function fromString($headerValue)
57 return new self(array_map(
function ($itemValue) use (&$index) {
59 $item->setIndex($index++);
62 }, preg_split(
'/\s*(?:,*("[^"]+"),*|,*(\'[^\']+\'),*|,+)\s*/', $headerValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)));
72 return implode(
',', $this->items);
82 public function has($value)
84 return isset($this->items[$value]);
94 public function get($value)
96 return isset($this->items[$value]) ? $this->items[$value] :
null;
106 public function add(AcceptHeaderItem $item)
108 $this->items[$item->getValue()] = $item;
109 $this->sorted =
false;
119 public function all()
133 public function filter($pattern)
135 return new self(array_filter($this->items,
function (
AcceptHeaderItem $item) use ($pattern) {
136 return preg_match($pattern, $item->
getValue());
145 public function first()
149 return !empty($this->items) ? reset($this->items) :
null;
155 private function sort()
157 if (!$this->sorted) {
158 uasort($this->items,
function ($a, $b) {
159 $qA = $a->getQuality();
160 $qB = $b->getQuality();
163 return $a->getIndex() > $b->getIndex() ? 1 : -1;
166 return $qA > $qB ? -1 : 1;
169 $this->sorted =
true;