1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Negotiation; |
4
|
|
|
|
5
|
|
|
abstract class BaseAccept |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var float |
9
|
|
|
*/ |
10
|
|
|
private $quality = 1.0; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
private $normalized; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $value; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $parameters; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $type; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $value |
34
|
|
|
*/ |
35
|
107 |
|
public function __construct($value) |
36
|
|
|
{ |
37
|
107 |
|
list($type, $parameters) = $this->parseParameters($value); |
38
|
|
|
|
39
|
107 |
|
if (isset($parameters['q'])) { |
40
|
70 |
|
$this->quality = (float) $parameters['q']; |
41
|
70 |
|
unset($parameters['q']); |
42
|
70 |
|
} |
43
|
|
|
|
44
|
107 |
|
$type = trim(strtolower($type)); |
45
|
|
|
|
46
|
107 |
|
$this->value = $value; |
47
|
107 |
|
$this->normalized = $type . ($parameters ? "; " . $this->buildParametersString($parameters) : ''); |
48
|
107 |
|
$this->type = $type; |
49
|
107 |
|
$this->parameters = $parameters; |
50
|
107 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
3 |
|
public function getNormalizedValue() |
56
|
|
|
{ |
57
|
3 |
|
return $this->normalized; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
31 |
|
public function getValue() |
64
|
|
|
{ |
65
|
31 |
|
return $this->value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
75 |
|
public function getType() |
72
|
|
|
{ |
73
|
75 |
|
return $this->type; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return float |
78
|
|
|
*/ |
79
|
67 |
|
public function getQuality() |
80
|
|
|
{ |
81
|
67 |
|
return $this->quality; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
42 |
|
public function getParameters() |
88
|
|
|
{ |
89
|
42 |
|
return $this->parameters; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $key |
94
|
|
|
* @param mixed $default |
95
|
|
|
* |
96
|
|
|
* @return string|null |
97
|
|
|
*/ |
98
|
1 |
|
public function getParameter($key, $default = null) |
99
|
|
|
{ |
100
|
1 |
|
return isset($this->parameters[$key]) ? $this->parameters[$key] : $default; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $key |
105
|
|
|
* |
106
|
|
|
* @return boolean |
107
|
|
|
*/ |
108
|
1 |
|
public function hasParameter($key) |
109
|
|
|
{ |
110
|
1 |
|
return isset($this->parameters[$key]); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* |
115
|
|
|
* @param string $acceptPart |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
107 |
|
private function parseParameters($acceptPart) |
119
|
|
|
{ |
120
|
107 |
|
$parts = explode(';', $acceptPart); |
121
|
107 |
|
$type = array_shift($parts); |
122
|
|
|
|
123
|
107 |
|
$parameters = []; |
124
|
107 |
|
foreach ($parts as $part) { |
125
|
80 |
|
$part = explode('=', $part); |
126
|
|
|
|
127
|
80 |
|
if (2 !== count($part)) { |
128
|
2 |
|
continue; // TODO: throw exception here? |
129
|
|
|
} |
130
|
|
|
|
131
|
78 |
|
$key = strtolower(trim($part[0])); // TODO: technically not allowed space around "=". throw exception? |
132
|
78 |
|
$parameters[$key] = trim($part[1], ' "'); |
133
|
107 |
|
} |
134
|
|
|
|
135
|
107 |
|
return [ $type, $parameters ]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $parameters |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
27 |
|
private function buildParametersString($parameters) |
144
|
|
|
{ |
145
|
27 |
|
$parts = []; |
146
|
|
|
|
147
|
27 |
|
ksort($parameters); |
148
|
27 |
|
foreach ($parameters as $key => $val) { |
149
|
27 |
|
$parts[] = sprintf('%s=%s', $key, $val); |
150
|
27 |
|
} |
151
|
|
|
|
152
|
27 |
|
return implode('; ', $parts); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|