-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNegotiator.php
188 lines (158 loc) · 5.61 KB
/
Negotiator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
namespace AC\WebServicesBundle;
use Negotiation\Negotiator as BasicNegotiator;
use Negotiation\FormatNegotiator;
use Negotiation\LanguageNegotiator;
use Symfony\Component\HttpFoundation\Request;
/**
* A convenience class for using the willdurand/negotiation Negotiator. It also has the added functionality of using some configuration to
* map incoming content-type headers to desired formats. This is mainly used for determining which format to use when deserializing incoming
* api data.
*
*/
class Negotiator
{
private $inputFormatMap = array();
private $langNegotiator;
private $formatNegotiator;
private $langPriorities;
private $formatPriorities;
private $charsetPriorities;
private $basicNegotiator;
private $additionalResponseNegotiationFormats = array();
/**
* Receives configuration to use for negogiation tasks.
*
* @param array $inputFormatMap A map of 'content/type'=>'format'
* @param array $formatPriorities Array of priorities for preferred formats
* @param array $langPriorities Array of priorities for preferred languages
* @param array $charsetPriorities Array of priorities for preferred charsets
* @param array $encodingPriorities Array of priorities for preferred encodings
*/
public function __construct(
$inputFormatMap = array(),
$formatPriorities = array(),
$langPriorities = array(),
$charsetPriorities = array(),
$encodingPriorities = array(),
$additionalResponseNegotiationFormats = array()
)
{
$this->inputFormatMap = $inputFormatMap;
$this->langPriorities = $langPriorities;
$this->formatPriorities = $formatPriorities;
$this->charsetPriorities = $charsetPriorities;
$this->encodingPriorities = $encodingPriorities;
$this->additionalResponseNegotiationFormats = $additionalResponseNegotiationFormats;
}
/**
* Convenient static creation method for chainable creation/use.
*
* @param array $map
* @param array $formats
* @param array $langs
* @param array $charsets
* @return Negotiator
*/
public static function create($map = array(), $formats = array(), $langs = array(), $charsets = array())
{
return new Negotiator($map, $formats, $langs, $charsets);
}
/**
* Return the incoming format, derived from the Requests content-type header. Generally this is used for determing which
* format to use during deserialization.
*
* @param Request $req
* @return string
*/
public function negotiateRequestFormat(Request $req)
{
$header = $req->headers->get('Content-Type');
$exp = explode(';', $header);
$type = $exp[0];
return isset($this->inputFormatMap[$type]) ? $this->inputFormatMap[$type] : false;
}
/**
* Return the response format type, derived from a Request's Accept header, and configured priorities.
*
* @param Request $req
* @return string
*/
public function negotiateResponseFormat(Request $req)
{
return $this->getFormatNegotiator()->getBestFormat($req->headers->get('Accept'), $this->formatPriorities);
}
/**
* Return preferred language, derived from Requests Accept-Language header, and configured priorities.
*
* @param Request $req
* @return string
*/
public function negotiateResponseLanguage(Request $req)
{
return $this->getLanguageNegotiator()->getBest($req->headers->get('Accept-Language'), $this->langPriorities)->getValue();
}
/**
* Derive response charset/encoding from incoming requests Accept-Charset header and configured priorities.
*
* @param Request $req
* @return string
*/
public function negotiateResponseCharset(Request $req)
{
return $this->getBasicNegotiator()->getBest($req->headers->get('Accept-Charset'), $this->charsetPriorities)->getValue();
}
public function negotiateResponseEncoding(Request $req)
{
return $this->getBasicNegotiator()->getBest($req->headers->get('Accept-Encoding', $this->encodingPriorities))->getValue();
}
public function setInputFormatForType($format, $type)
{
$this->inputFormatMap[$format] = $type;
return $this;
}
public function setLanguagePriorities(array $arr = null)
{
$this->langPriorities = $arr;
return $this;
}
public function setFormatPriorities(array $arr = null)
{
$this->formatPriorities = $arr;
return $this;
}
public function setCharsetPriorities(array $arr = null)
{
$this->charsetPriorities = $arr;
return $this;
}
public function setEncodingPriorities(array $arr = null)
{
$this->encodingPriorities = $arr;
return $this;
}
public function getLanguageNegotiator()
{
if (!$this->langNegotiator) {
$this->langNegotiator = new LanguageNegotiator();
}
return $this->langNegotiator;
}
public function getFormatNegotiator()
{
if (!$this->formatNegotiator) {
$this->formatNegotiator = new FormatNegotiator();
foreach ($this->additionalResponseNegotiationFormats as $format => $mimeTypes) {
$this->formatNegotiator->registerFormat($format, $mimeTypes);
}
}
return $this->formatNegotiator;
}
public function getBasicNegotiator()
{
if (!$this->basicNegotiator) {
$this->basicNegotiator = new BasicNegotiator();
}
return $this->basicNegotiator;
}
}