Open Journal Systems  3.3.0
lib/vendor/symfony/http-foundation/AcceptHeader.php
1 <?php
2 
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
23 {
27  private $items = array();
28 
32  private $sorted = true;
33 
39  public function __construct(array $items)
40  {
41  foreach ($items as $item) {
42  $this->add($item);
43  }
44  }
45 
53  public static function fromString($headerValue)
54  {
55  $index = 0;
56 
57  return new self(array_map(function ($itemValue) use (&$index) {
58  $item = AcceptHeaderItem::fromString($itemValue);
59  $item->setIndex($index++);
60 
61  return $item;
62  }, preg_split('/\s*(?:,*("[^"]+"),*|,*(\'[^\']+\'),*|,+)\s*/', $headerValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)));
63  }
64 
70  public function __toString()
71  {
72  return implode(',', $this->items);
73  }
74 
82  public function has($value)
83  {
84  return isset($this->items[$value]);
85  }
86 
94  public function get($value)
95  {
96  return isset($this->items[$value]) ? $this->items[$value] : null;
97  }
98 
106  public function add(AcceptHeaderItem $item)
107  {
108  $this->items[$item->getValue()] = $item;
109  $this->sorted = false;
110 
111  return $this;
112  }
113 
119  public function all()
120  {
121  $this->sort();
122 
123  return $this->items;
124  }
125 
133  public function filter($pattern)
134  {
135  return new self(array_filter($this->items, function (AcceptHeaderItem $item) use ($pattern) {
136  return preg_match($pattern, $item->getValue());
137  }));
138  }
139 
145  public function first()
146  {
147  $this->sort();
148 
149  return !empty($this->items) ? reset($this->items) : null;
150  }
151 
155  private function sort()
156  {
157  if (!$this->sorted) {
158  uasort($this->items, function ($a, $b) {
159  $qA = $a->getQuality();
160  $qB = $b->getQuality();
161 
162  if ($qA === $qB) {
163  return $a->getIndex() > $b->getIndex() ? 1 : -1;
164  }
165 
166  return $qA > $qB ? -1 : 1;
167  });
168 
169  $this->sorted = true;
170  }
171  }
172 }
Symfony\Component\HttpFoundation\AcceptHeader\__construct
__construct(array $items)
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:45
Symfony\Component\HttpFoundation\AcceptHeader\filter
filter($pattern)
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:139
Symfony\Component\HttpFoundation\AcceptHeaderItem
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:19
Symfony\Component\HttpFoundation\AcceptHeader\__toString
__toString()
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:76
Symfony\Component\HttpFoundation\AcceptHeader\has
has($value)
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:88
Symfony\Component\HttpFoundation\AcceptHeader\fromString
static fromString($headerValue)
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:59
Symfony\Component\HttpFoundation\AcceptHeaderItem\fromString
static fromString($itemValue)
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:74
Symfony\Component\HttpFoundation\AcceptHeader\all
all()
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:125
Symfony\Component\HttpFoundation\AcceptHeader\add
add(AcceptHeaderItem $item)
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:112
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\AcceptHeaderItem\getValue
getValue()
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:132
Symfony\Component\HttpFoundation\AcceptHeader
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:22
Symfony\Component\HttpFoundation\AcceptHeader\first
first()
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:151