@@ -29,6 +29,8 @@ class Request implements RequestInterface
29
29
protected $ requestParameters ;
30
30
protected $ queryParameters ;
31
31
protected $ serverParameters ;
32
+ protected $ filesParameters ;
33
+ protected $ cookiesParameters ;
32
34
protected $ languages ;
33
35
protected $ charsets ;
34
36
protected $ acceptableContentTypes ;
@@ -48,11 +50,13 @@ class Request implements RequestInterface
48
50
* @param array $query The GET parameters
49
51
* @param array $request The POST parameters
50
52
* @param array $path The parameters parsed from the PATH_INFO (see Router)
53
+ * @param array $cookies The COOKIE parameters
54
+ * @param array $files The FILES parameters
51
55
* @param array $server The SERVER parameters
52
56
*/
53
- public function __construct (array $ query = null , array $ request = null , array $ path = null , array $ server = null )
57
+ public function __construct (array $ query = null , array $ request = null , array $ path = null , array $ cookies = null , array $ files = null , array $ server = null )
54
58
{
55
- $ this ->setParameters ($ request , $ query , $ path , $ server );
59
+ $ this ->setParameters ($ request , $ query , $ path , $ cookies , $ files , $ server );
56
60
}
57
61
58
62
/**
@@ -63,13 +67,17 @@ public function __construct(array $query = null, array $request = null, array $p
63
67
* @param array $query The GET parameters
64
68
* @param array $request The POST parameters
65
69
* @param array $path The parameters parsed from the PATH_INFO
70
+ * @param array $cookies The COOKIE parameters
71
+ * @param array $files The FILES parameters
66
72
* @param array $server The SERVER parameters
67
73
*/
68
- public function setParameters (array $ query = null , array $ request = null , array $ path = null , array $ server = null )
74
+ public function setParameters (array $ query = null , array $ request = null , array $ path = null , array $ cookies = null , array $ files = null , array $ server = null )
69
75
{
70
76
$ this ->requestParameters = null !== $ request ? $ request : $ _POST ;
71
77
$ this ->queryParameters = null !== $ query ? $ query : $ _GET ;
72
78
$ this ->pathParameters = null !== $ path ? $ path : array ();
79
+ $ this ->cookiesParameters = null !== $ cookies ? $ cookies : $ _COOKIE ;
80
+ $ this ->filesParameters = self ::convertFileInformation (null !== $ files ? $ files : $ _FILES );
73
81
$ this ->serverParameters = null !== $ server ? $ server : $ _SERVER ;
74
82
75
83
$ this ->languages = null ;
@@ -90,34 +98,25 @@ public function setParameters(array $query = null, array $request = null, array
90
98
* @param array $query The GET parameters
91
99
* @param array $request The POST parameters
92
100
* @param array $path The parameters parsed from the PATH_INFO
101
+ * @param array $cookies The COOKIE parameters
102
+ * @param array $files The FILES parameters
93
103
* @param array $server The SERVER parameters
94
104
*/
95
- public function duplicate (array $ query = null , array $ request = null , array $ path = null , array $ server = null )
105
+ public function duplicate (array $ query = null , array $ request = null , array $ path = null , array $ cookies = null , array $ files = null , array $ server = null )
96
106
{
97
107
$ dup = clone $ this ;
98
108
$ dup ->setParameters (
99
109
null !== $ query ? $ query : $ this ->queryParameters ,
100
110
null !== $ request ? $ request : $ this ->requestParameters ,
101
111
null !== $ path ? $ path : $ this ->pathParameters ,
112
+ null !== $ cookies ? $ cookies : $ this ->cookiesParameters ,
113
+ null !== $ files ? $ files : $ this ->filesParameters ,
102
114
null !== $ server ? $ server : $ this ->serverParameters
103
115
);
104
116
105
117
return $ dup ;
106
118
}
107
119
108
- /**
109
- * Gets a cookie value.
110
- *
111
- * @param string $name Cookie name
112
- * @param string $defaultValue Default value returned when no cookie with given name is found
113
- *
114
- * @return mixed The cookie value
115
- */
116
- public function getCookie ($ name , $ default = null )
117
- {
118
- return isset ($ _COOKIE [$ name ]) ? $ _COOKIE [$ name ] : $ default ;
119
- }
120
-
121
120
// Order of precedence: GET, PATH, POST, COOKIE
122
121
// Avoid using this method in controllers:
123
122
// * slow
@@ -128,51 +127,145 @@ public function getParameter($key, $default = null)
128
127
return $ this ->getQueryParameter ($ key , $ this ->getPathParameter ($ key , $ this ->getRequestParameter ($ key , $ default )));
129
128
}
130
129
130
+ /**
131
+ * Returns the server parameters ($_SERVER).
132
+ *
133
+ * @return array An array of server parameters
134
+ */
131
135
public function getServerParameters ()
132
136
{
133
137
return $ this ->serverParameters ;
134
138
}
135
139
136
- public function getServerParameter ($ name , $ default = null )
140
+ /**
141
+ * Returns a server parameter ($_SERVER).
142
+ *
143
+ * @param string $key The key
144
+ * @param mixed $default The default value
145
+ */
146
+ public function getServerParameter ($ key , $ default = null )
137
147
{
138
- return isset ($ this ->serverParameters [$ name ]) ? $ this ->serverParameters [$ name ] : $ default ;
148
+ return isset ($ this ->serverParameters [$ key ]) ? $ this ->serverParameters [$ key ] : $ default ;
139
149
}
140
150
151
+ /**
152
+ * Returns the path parameters.
153
+ *
154
+ * @return array An array of path parameters
155
+ */
141
156
public function getPathParameters ()
142
157
{
143
158
return $ this ->pathParameters ;
144
159
}
145
160
161
+ /**
162
+ * Sets the path parameters.
163
+ *
164
+ * @param array An array of path parameters
165
+ */
146
166
public function setPathParameters (array $ parameters )
147
167
{
148
168
$ this ->pathParameters = $ parameters ;
149
169
}
150
170
171
+ /**
172
+ * Returns a path parameter.
173
+ *
174
+ * @param string $key The key
175
+ * @param mixed $default The default value
176
+ */
151
177
public function getPathParameter ($ key , $ default = null )
152
178
{
153
179
return isset ($ this ->pathParameters [$ key ]) ? $ this ->pathParameters [$ key ] : $ default ;
154
180
}
155
181
182
+ /**
183
+ * Returns the request parameters ($_POST).
184
+ *
185
+ * @return array An array of request parameters
186
+ */
156
187
public function getRequestParameters ()
157
188
{
158
189
return $ this ->requestParameters ;
159
190
}
160
191
192
+ /**
193
+ * Returns a request parameter ($_POST).
194
+ *
195
+ * @param string $key The server key
196
+ * @param mixed $default The default value
197
+ */
161
198
public function getRequestParameter ($ key , $ default = null )
162
199
{
163
200
return isset ($ this ->requestParameters [$ key ]) ? $ this ->requestParameters [$ key ] : $ default ;
164
201
}
165
202
203
+ /**
204
+ * Returns the query parameters ($_GET).
205
+ *
206
+ * @return array An array of query parameters
207
+ */
166
208
public function getQueryParameters ()
167
209
{
168
210
return $ this ->queryParameters ;
169
211
}
170
212
213
+ /**
214
+ * Returns a query parameter ($_GET).
215
+ *
216
+ * @param string $key The server key
217
+ * @param mixed $default The default value
218
+ */
171
219
public function getQueryParameter ($ key , $ default = null )
172
220
{
173
221
return isset ($ this ->queryParameters [$ key ]) ? $ this ->queryParameters [$ key ] : $ default ;
174
222
}
175
223
224
+ /**
225
+ * Returns a cookie value ($_COOKIE).
226
+ *
227
+ * @param string $key The key
228
+ * @param mixed $default The default value
229
+ *
230
+ * @return mixed The cookie value
231
+ */
232
+ public function getCookie ($ key , $ default = null )
233
+ {
234
+ return isset ($ this ->cookiesParameters [$ key ]) ? $ cookiesParameters [$ key ] : $ default ;
235
+ }
236
+
237
+ /**
238
+ * Returns the array of cookies ($_COOKIE).
239
+ *
240
+ * @param array An array of cookies
241
+ */
242
+ public function getCookies ()
243
+ {
244
+ return $ this ->cookiesParameters ;
245
+ }
246
+
247
+ /**
248
+ * Returns a file from the request ($_FILES).
249
+ *
250
+ * @param string $key A key
251
+ *
252
+ * @return array The associated file
253
+ */
254
+ public function getFile ($ key )
255
+ {
256
+ return isset ($ this ->filesParameters [$ key ]) ? $ this ->filesParameters [$ key ] : array ();
257
+ }
258
+
259
+ /**
260
+ * Returns the array of files ($_FILES).
261
+ *
262
+ * @return array An associative array of files
263
+ */
264
+ public function getFiles ()
265
+ {
266
+ return $ this ->filesParameters ;
267
+ }
268
+
176
269
public function getHttpHeader ($ name , $ default = null )
177
270
{
178
271
return $ this ->getServerParameter ('HTTP_ ' .strtoupper (strtr ($ name , '- ' , '_ ' )), $ default );
@@ -735,6 +828,26 @@ protected function preparePathInfo()
735
828
return (string ) $ pathInfo ;
736
829
}
737
830
831
+ /**
832
+ * Converts uploaded file array to a format following the $_GET and $POST naming convention.
833
+ *
834
+ * It's safe to pass an already converted array, in which case this method just returns the original array unmodified.
835
+ *
836
+ * @param array $taintedFiles An array representing uploaded file information
837
+ *
838
+ * @return array An array of re-ordered uploaded file information
839
+ */
840
+ protected function convertFileInformation (array $ taintedFiles )
841
+ {
842
+ $ files = array ();
843
+ foreach ($ taintedFiles as $ key => $ data )
844
+ {
845
+ $ files [$ key ] = $ this ->fixPhpFilesArray ($ data );
846
+ }
847
+
848
+ return $ files ;
849
+ }
850
+
738
851
static protected function initializeFormats ()
739
852
{
740
853
static ::$ formats = array (
@@ -747,4 +860,34 @@ static protected function initializeFormats()
747
860
'atom ' => 'application/atom+xml ' ,
748
861
);
749
862
}
863
+
864
+ static protected function fixPhpFilesArray ($ data )
865
+ {
866
+ $ fileKeys = array ('error ' , 'name ' , 'size ' , 'tmp_name ' , 'type ' );
867
+ $ keys = array_keys ($ data );
868
+ sort ($ keys );
869
+
870
+ if ($ fileKeys != $ keys || !isset ($ data ['name ' ]) || !is_array ($ data ['name ' ]))
871
+ {
872
+ return $ data ;
873
+ }
874
+
875
+ $ files = $ data ;
876
+ foreach ($ fileKeys as $ k )
877
+ {
878
+ unset($ files [$ k ]);
879
+ }
880
+ foreach (array_keys ($ data ['name ' ]) as $ key )
881
+ {
882
+ $ files [$ key ] = self ::fixPhpFilesArray (array (
883
+ 'error ' => $ data ['error ' ][$ key ],
884
+ 'name ' => $ data ['name ' ][$ key ],
885
+ 'type ' => $ data ['type ' ][$ key ],
886
+ 'tmp_name ' => $ data ['tmp_name ' ][$ key ],
887
+ 'size ' => $ data ['size ' ][$ key ],
888
+ ));
889
+ }
890
+
891
+ return $ files ;
892
+ }
750
893
}
0 commit comments