1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\HttpFoundation;
13:
14: 15: 16: 17: 18:
19: class
20: {
21: 22: 23:
24: private $value;
25:
26: 27: 28:
29: private $quality = 1.0;
30:
31: 32: 33:
34: private $index = 0;
35:
36: 37: 38:
39: private $attributes = array();
40:
41: 42: 43: 44: 45: 46:
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:
55: 56: 57: 58: 59: 60: 61:
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:
84: 85: 86: 87: 88:
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:
101: 102: 103: 104: 105: 106: 107:
108: public function setValue($value)
109: {
110: $this->value = $value;
111:
112: return $this;
113: }
114:
115: 116: 117: 118: 119:
120: public function getValue()
121: {
122: return $this->value;
123: }
124:
125: 126: 127: 128: 129: 130: 131:
132: public function setQuality($quality)
133: {
134: $this->quality = $quality;
135:
136: return $this;
137: }
138:
139: 140: 141: 142: 143:
144: public function getQuality()
145: {
146: return $this->quality;
147: }
148:
149: 150: 151: 152: 153: 154: 155:
156: public function setIndex($index)
157: {
158: $this->index = $index;
159:
160: return $this;
161: }
162:
163: 164: 165: 166: 167:
168: public function getIndex()
169: {
170: return $this->index;
171: }
172:
173: 174: 175: 176: 177: 178: 179:
180: public function hasAttribute($name)
181: {
182: return isset($this->attributes[$name]);
183: }
184:
185: 186: 187: 188: 189: 190: 191: 192:
193: public function getAttribute($name, $default = null)
194: {
195: return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
196: }
197:
198: 199: 200: 201: 202:
203: public function getAttributes()
204: {
205: return $this->attributes;
206: }
207:
208: 209: 210: 211: 212: 213: 214: 215:
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: }
227: