Open Journal Systems  3.3.0
lib/vendor/symfony/http-foundation/AcceptHeaderItem.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 
20 {
24  private $value;
25 
29  private $quality = 1.0;
30 
34  private $index = 0;
35 
39  private $attributes = array();
40 
47  public function __construct($value, array $attributes = array())
48  {
49  $this->value = $value;
50  foreach ($attributes as $name => $value) {
51  $this->setAttribute($name, $value);
52  }
53  }
54 
62  public static function fromString($itemValue)
63  {
64  $bits = preg_split('/\s*(?:;*("[^"]+");*|;*(\'[^\']+\');*|;+)\s*/', $itemValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
65  $value = array_shift($bits);
66  $attributes = array();
67 
68  $lastNullAttribute = null;
69  foreach ($bits as $bit) {
70  if (($start = substr($bit, 0, 1)) === ($end = substr($bit, -1)) && ($start === '"' || $start === '\'')) {
71  $attributes[$lastNullAttribute] = substr($bit, 1, -1);
72  } elseif ('=' === $end) {
73  $lastNullAttribute = $bit = substr($bit, 0, -1);
74  $attributes[$bit] = null;
75  } else {
76  $parts = explode('=', $bit);
77  $attributes[$parts[0]] = isset($parts[1]) && strlen($parts[1]) > 0 ? $parts[1] : '';
78  }
79  }
80 
81  return new self(($start = substr($value, 0, 1)) === ($end = substr($value, -1)) && ($start === '"' || $start === '\'') ? substr($value, 1, -1) : $value, $attributes);
82  }
83 
89  public function __toString()
90  {
91  $string = $this->value.($this->quality < 1 ? ';q='.$this->quality : '');
92  if (count($this->attributes) > 0) {
93  $string .= ';'.implode(';', array_map(function ($name, $value) {
94  return sprintf(preg_match('/[,;=]/', $value) ? '%s="%s"' : '%s=%s', $name, $value);
95  }, array_keys($this->attributes), $this->attributes));
96  }
97 
98  return $string;
99  }
100 
108  public function setValue($value)
109  {
110  $this->value = $value;
111 
112  return $this;
113  }
114 
120  public function getValue()
121  {
122  return $this->value;
123  }
124 
132  public function setQuality($quality)
133  {
134  $this->quality = $quality;
135 
136  return $this;
137  }
138 
144  public function getQuality()
145  {
146  return $this->quality;
147  }
148 
156  public function setIndex($index)
157  {
158  $this->index = $index;
159 
160  return $this;
161  }
162 
168  public function getIndex()
169  {
170  return $this->index;
171  }
172 
180  public function hasAttribute($name)
181  {
182  return isset($this->attributes[$name]);
183  }
184 
193  public function getAttribute($name, $default = null)
194  {
195  return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
196  }
197 
203  public function getAttributes()
204  {
205  return $this->attributes;
206  }
207 
216  public function setAttribute($name, $value)
217  {
218  if ('q' === $name) {
219  $this->quality = (float) $value;
220  } else {
221  $this->attributes[$name] = (string) $value;
222  }
223 
224  return $this;
225  }
226 }
Symfony\Component\HttpFoundation\AcceptHeaderItem\getIndex
getIndex()
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:180
Symfony\Component\HttpFoundation\AcceptHeaderItem\getAttribute
getAttribute($name, $default=null)
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:205
Symfony\Component\HttpFoundation\AcceptHeaderItem\__construct
__construct($value, array $attributes=array())
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:59
Symfony\Component\HttpFoundation\AcceptHeaderItem\getAttributes
getAttributes()
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:215
Symfony\Component\HttpFoundation\AcceptHeaderItem
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:19
Symfony\Component\HttpFoundation\AcceptHeaderItem\setAttribute
setAttribute($name, $value)
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:228
Symfony\Component\HttpFoundation\AcceptHeaderItem\hasAttribute
hasAttribute($name)
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:192
Symfony\Component\HttpFoundation\AcceptHeaderItem\fromString
static fromString($itemValue)
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:74
Symfony\Component\HttpFoundation\AcceptHeaderItem\getQuality
getQuality()
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:156
Symfony\Component\HttpFoundation\AcceptHeaderItem\__toString
__toString()
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:101
Symfony\Component\HttpFoundation\AcceptHeaderItem\setValue
setValue($value)
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:120
Symfony\Component\HttpFoundation
Definition: lib/vendor/symfony/http-foundation/AcceptHeader.php:12
Symfony\Component\HttpFoundation\AcceptHeaderItem\setIndex
setIndex($index)
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:168
Symfony\Component\HttpFoundation\AcceptHeaderItem\getValue
getValue()
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:132
Symfony\Component\HttpFoundation\AcceptHeaderItem\setQuality
setQuality($quality)
Definition: lib/vendor/symfony/http-foundation/AcceptHeaderItem.php:144