29 private $quality = 1.0;
39 private $attributes = array();
47 public function __construct($value, array $attributes = array())
49 $this->value = $value;
50 foreach ($attributes as $name => $value) {
64 $bits = preg_split(
'/\s*(?:;*("[^"]+");*|;*(\'[^\']+\');*|;+)\s*/', $itemValue, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
65 $value = array_shift($bits);
66 $attributes = array();
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;
76 $parts = explode(
'=', $bit);
77 $attributes[$parts[0]] = isset($parts[1]) && strlen($parts[1]) > 0 ? $parts[1] :
'';
81 return new self(($start = substr($value, 0, 1)) === ($end = substr($value, -1)) && ($start ===
'"' || $start ===
'\'') ? substr($value, 1, -1) : $value, $attributes);
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));
110 $this->value = $value;
134 $this->quality = $quality;
146 return $this->quality;
158 $this->index = $index;
182 return isset($this->attributes[$name]);
195 return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
205 return $this->attributes;
219 $this->quality = (float) $value;
221 $this->attributes[$name] = (string) $value;