1 |
cb15593b
|
Cajova-Houba
|
<?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 |
|
|
|
12 |
|
|
namespace Symfony\Component\HttpFoundation;
|
13 |
|
|
|
14 |
|
|
/**
|
15 |
|
|
* Represents an Accept-* header item.
|
16 |
|
|
*
|
17 |
|
|
* @author Jean-François Simon <contact@jfsimon.fr>
|
18 |
|
|
*/
|
19 |
|
|
class AcceptHeaderItem
|
20 |
|
|
{
|
21 |
|
|
/**
|
22 |
|
|
* @var string
|
23 |
|
|
*/
|
24 |
|
|
private $value;
|
25 |
|
|
|
26 |
|
|
/**
|
27 |
|
|
* @var float
|
28 |
|
|
*/
|
29 |
|
|
private $quality = 1.0;
|
30 |
|
|
|
31 |
|
|
/**
|
32 |
|
|
* @var int
|
33 |
|
|
*/
|
34 |
|
|
private $index = 0;
|
35 |
|
|
|
36 |
|
|
/**
|
37 |
|
|
* @var array
|
38 |
|
|
*/
|
39 |
|
|
private $attributes = array();
|
40 |
|
|
|
41 |
|
|
/**
|
42 |
|
|
* Constructor.
|
43 |
|
|
*
|
44 |
|
|
* @param string $value
|
45 |
|
|
* @param array $attributes
|
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 |
|
|
* Builds an AcceptHeaderInstance instance from a string.
|
57 |
|
|
*
|
58 |
|
|
* @param string $itemValue
|
59 |
|
|
*
|
60 |
|
|
* @return AcceptHeaderItem
|
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 |
|
|
* Returns header value's string representation.
|
86 |
|
|
*
|
87 |
|
|
* @return string
|
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 |
|
|
* Set the item value.
|
103 |
|
|
*
|
104 |
|
|
* @param string $value
|
105 |
|
|
*
|
106 |
|
|
* @return AcceptHeaderItem
|
107 |
|
|
*/
|
108 |
|
|
public function setValue($value)
|
109 |
|
|
{
|
110 |
|
|
$this->value = $value;
|
111 |
|
|
|
112 |
|
|
return $this;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
/**
|
116 |
|
|
* Returns the item value.
|
117 |
|
|
*
|
118 |
|
|
* @return string
|
119 |
|
|
*/
|
120 |
|
|
public function getValue()
|
121 |
|
|
{
|
122 |
|
|
return $this->value;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* Set the item quality.
|
127 |
|
|
*
|
128 |
|
|
* @param float $quality
|
129 |
|
|
*
|
130 |
|
|
* @return AcceptHeaderItem
|
131 |
|
|
*/
|
132 |
|
|
public function setQuality($quality)
|
133 |
|
|
{
|
134 |
|
|
$this->quality = $quality;
|
135 |
|
|
|
136 |
|
|
return $this;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/**
|
140 |
|
|
* Returns the item quality.
|
141 |
|
|
*
|
142 |
|
|
* @return float
|
143 |
|
|
*/
|
144 |
|
|
public function getQuality()
|
145 |
|
|
{
|
146 |
|
|
return $this->quality;
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
/**
|
150 |
|
|
* Set the item index.
|
151 |
|
|
*
|
152 |
|
|
* @param int $index
|
153 |
|
|
*
|
154 |
|
|
* @return AcceptHeaderItem
|
155 |
|
|
*/
|
156 |
|
|
public function setIndex($index)
|
157 |
|
|
{
|
158 |
|
|
$this->index = $index;
|
159 |
|
|
|
160 |
|
|
return $this;
|
161 |
|
|
}
|
162 |
|
|
|
163 |
|
|
/**
|
164 |
|
|
* Returns the item index.
|
165 |
|
|
*
|
166 |
|
|
* @return int
|
167 |
|
|
*/
|
168 |
|
|
public function getIndex()
|
169 |
|
|
{
|
170 |
|
|
return $this->index;
|
171 |
|
|
}
|
172 |
|
|
|
173 |
|
|
/**
|
174 |
|
|
* Tests if an attribute exists.
|
175 |
|
|
*
|
176 |
|
|
* @param string $name
|
177 |
|
|
*
|
178 |
|
|
* @return bool
|
179 |
|
|
*/
|
180 |
|
|
public function hasAttribute($name)
|
181 |
|
|
{
|
182 |
|
|
return isset($this->attributes[$name]);
|
183 |
|
|
}
|
184 |
|
|
|
185 |
|
|
/**
|
186 |
|
|
* Returns an attribute by its name.
|
187 |
|
|
*
|
188 |
|
|
* @param string $name
|
189 |
|
|
* @param mixed $default
|
190 |
|
|
*
|
191 |
|
|
* @return mixed
|
192 |
|
|
*/
|
193 |
|
|
public function getAttribute($name, $default = null)
|
194 |
|
|
{
|
195 |
|
|
return isset($this->attributes[$name]) ? $this->attributes[$name] : $default;
|
196 |
|
|
}
|
197 |
|
|
|
198 |
|
|
/**
|
199 |
|
|
* Returns all attributes.
|
200 |
|
|
*
|
201 |
|
|
* @return array
|
202 |
|
|
*/
|
203 |
|
|
public function getAttributes()
|
204 |
|
|
{
|
205 |
|
|
return $this->attributes;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
/**
|
209 |
|
|
* Set an attribute.
|
210 |
|
|
*
|
211 |
|
|
* @param string $name
|
212 |
|
|
* @param string $value
|
213 |
|
|
*
|
214 |
|
|
* @return AcceptHeaderItem
|
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 |
|
|
}
|