1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-Http package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\Http\Header\Accept; |
12
|
|
|
|
13
|
|
|
use Borobudur\Http\Exception\InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Iqbal Maulana <[email protected]> |
17
|
|
|
* @created 7/18/15 |
18
|
|
|
*/ |
19
|
|
|
class AcceptHeaderItem |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
private $value; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var float |
28
|
|
|
*/ |
29
|
|
|
private $priority = 1.0; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $parameters = array(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $index = null; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param string $value |
45
|
|
|
* @param array|null $parameters |
46
|
|
|
*/ |
47
|
|
|
public function __construct($value, array $parameters = null) |
48
|
|
|
{ |
49
|
|
|
$this->value = $value; |
50
|
|
|
|
51
|
|
|
if (null !== $parameters) { |
52
|
|
|
foreach ($parameters as $name => $value) { |
53
|
|
|
$this->setParameter($name, $value); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Set a parameter. |
60
|
|
|
* |
61
|
|
|
* @param string $name |
62
|
|
|
* @param string $value |
63
|
|
|
* |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function setParameter($name, $value) |
67
|
|
|
{ |
68
|
|
|
if ('q' === $name) { |
69
|
|
|
$this->setPriority($value); |
70
|
|
|
} else { |
71
|
|
|
$this->parameters[$name] = (string)$value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Factory create AcceptHeaderItem from string. |
79
|
|
|
* |
80
|
|
|
* @param string $itemString |
81
|
|
|
* |
82
|
|
|
* @return $this |
83
|
|
|
*/ |
84
|
|
|
public static function fromString($itemString) |
85
|
|
|
{ |
86
|
|
|
preg_match_all('#((("[^"\']++")|(\'[^"\']++\'))|[^;"\'])+#', (string)$itemString, $parts); |
87
|
|
|
$bits = $parts[0]; |
88
|
|
|
$fieldValue = array_shift($bits); |
89
|
|
|
$parameters = array(); |
90
|
|
|
|
91
|
|
|
foreach ($bits as $bit) { |
92
|
|
|
$pos = strpos($bit, '='); |
93
|
|
|
$name = false === $pos ? $bit : substr($bit, 0, $pos); |
94
|
|
|
$value = false === $pos ? null : substr($bit, $pos + 1); |
95
|
|
|
|
96
|
|
|
if (null !== $value |
97
|
|
|
&& in_array($value[0], array('\'', '"')) |
98
|
|
|
&& in_array(substr($value, -1), array('\'', '"')) |
99
|
|
|
) { |
100
|
|
|
$value = substr($value, 1, -1); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$parameters[$name] = $value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return new static($fieldValue, $parameters); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Return header item value. |
111
|
|
|
* |
112
|
|
|
* @return string |
113
|
|
|
*/ |
114
|
|
|
public function getValue() |
115
|
|
|
{ |
116
|
|
|
return $this->value; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get accept header item quality. |
121
|
|
|
* |
122
|
|
|
* @return float |
123
|
|
|
*/ |
124
|
|
|
public function getPriority() |
125
|
|
|
{ |
126
|
|
|
return $this->priority; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Set accept header item quality. |
131
|
|
|
* |
132
|
|
|
* @param float|int $priority |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
* |
136
|
|
|
* @throws InvalidArgumentException |
137
|
|
|
*/ |
138
|
|
|
public function setPriority($priority) |
139
|
|
|
{ |
140
|
|
|
$priority = (float)$priority; |
141
|
|
|
|
142
|
|
|
if (0 > $priority || 1 < $priority) { |
143
|
|
|
throw new InvalidArgumentException(sprintf('Priority must between 0 - 1, "%s" give.', $priority)); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$this->priority = $priority; |
147
|
|
|
|
148
|
|
|
return $this; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Get all parameters. |
153
|
|
|
* |
154
|
|
|
* @return array |
155
|
|
|
*/ |
156
|
|
|
public function getParameters() |
157
|
|
|
{ |
158
|
|
|
return $this->parameters; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Return parameter by name or default value if not exist. |
163
|
|
|
* |
164
|
|
|
* @param string $name |
165
|
|
|
* @param string|null $default |
166
|
|
|
* |
167
|
|
|
* @return string|null |
168
|
|
|
*/ |
169
|
|
|
public function getParameter($name, $default = null) |
170
|
|
|
{ |
171
|
|
|
if (isset($this->parameters[$name])) { |
172
|
|
|
return $this->parameters[$name]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $default; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Check if parameter exist by name. |
180
|
|
|
* |
181
|
|
|
* @param string $name |
182
|
|
|
* |
183
|
|
|
* @return bool |
184
|
|
|
*/ |
185
|
|
|
public function hasParameter($name) |
186
|
|
|
{ |
187
|
|
|
return isset($this->parameters[$name]); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Set accept header item index. |
192
|
|
|
* |
193
|
|
|
* @param int $index |
194
|
|
|
* |
195
|
|
|
* @return $this |
196
|
|
|
*/ |
197
|
|
|
public function setIndex($index) |
198
|
|
|
{ |
199
|
|
|
$this->index = (int)$index; |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Get index. |
206
|
|
|
* |
207
|
|
|
* @return int |
208
|
|
|
*/ |
209
|
|
|
public function getIndex() |
210
|
|
|
{ |
211
|
|
|
return $this->index; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Cast accept item to string representation. |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function __toString() |
220
|
|
|
{ |
221
|
|
|
$string = $this->value . ($this->priority < 1 ? ';q=' . $this->priority : ''); |
222
|
|
|
|
223
|
|
|
if (count($this->parameters)) { |
224
|
|
|
foreach ($this->parameters as $name => $value) { |
225
|
|
|
$format = preg_match('/[,;=]/', $value) ? ';%s="%s"' : ';%s=%s'; |
226
|
|
|
$string .= sprintf($format, $name, $value); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $string; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
} |
234
|
|
|
|