1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the AMFWebServicesClientBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Amine Fattouch <http://github.com/fattouchsquall> |
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 AMF\WebServicesClientBundle\Rest\Component; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
15
|
|
|
use Symfony\Component\HttpFoundation\HeaderBag; |
16
|
|
|
use Symfony\Component\HttpFoundation\ServerBag; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This class represents a ReST Request. |
20
|
|
|
* |
21
|
|
|
* @author Mohamed Amine Fattouch <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class Request |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ParameterBag |
27
|
|
|
*/ |
28
|
|
|
protected $request; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ParameterBag |
32
|
|
|
*/ |
33
|
|
|
protected $query; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var ParameterBag |
37
|
|
|
*/ |
38
|
|
|
protected $server; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var HeaderBag |
42
|
|
|
*/ |
43
|
|
|
protected $headers; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
protected $format; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
protected static $formats; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The constructor class. |
57
|
|
|
* |
58
|
|
|
* @param array $request The request parameters (default empty). |
59
|
|
|
* @param array $query The query parameters (default empty). |
60
|
|
|
* @param array $server The server parameters (default empty). |
61
|
|
|
*/ |
62
|
|
|
public function __construct(array $request = [], array $query = [], array $server = []) |
63
|
|
|
{ |
64
|
|
|
$this->init($request, $query, $server); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Initializes current request. |
69
|
|
|
* |
70
|
|
|
* @param array $request The POST parameters (default empty). |
71
|
|
|
* @param array $query The GET parameters (default empty). |
72
|
|
|
* @param array $server The SERVER parameters (default empty). |
73
|
|
|
*/ |
74
|
|
|
public function init(array $request = [], array $query = [], array $server = []) |
75
|
|
|
{ |
76
|
|
|
$this->request = new ParameterBag($request); |
77
|
|
|
$this->query = new ParameterBag($query); |
78
|
|
|
$this->server = new ServerBag($server); |
79
|
|
|
$this->headers = new HeaderBag($this->server->getHeaders()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create a static instance of this class. |
84
|
|
|
* |
85
|
|
|
* @param string $uri The uri. |
86
|
|
|
* @param string $query The method of request. |
87
|
|
|
* @param array $request The parameters of request. |
88
|
|
|
* @param array $server The server parameters. |
89
|
|
|
* @param string $format The format of current request. |
90
|
|
|
* |
91
|
|
|
* @return \static |
92
|
|
|
*/ |
93
|
|
|
public static function create($uri = null, array $query = [], array $request = [], array $server = [], $format = null) |
94
|
|
|
{ |
95
|
|
|
if (!isset($server['HTTP_CONTENT_TYPE'])) { |
96
|
|
|
if (null === static::$formats) { |
97
|
|
|
static::initFormats(); |
98
|
|
|
} |
99
|
|
|
if (array_key_exists($format, static::$formats)) { |
100
|
|
|
$server['HTTP_CONTENT_TYPE'] = static::$formats[$format]; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($server['REQUEST_STRING'])) { |
105
|
|
|
$server['HTTP_CONTENT_LENGTH'] = strlen($server['REQUEST_STRING']); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (!isset($server['REQUEST_URI'])) { |
109
|
|
|
if (isset($uri)) { |
110
|
|
|
$server['REQUEST_URI'] = $uri; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return new static($request, $query, $server); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* The getter for request. |
119
|
|
|
* |
120
|
|
|
* @return ParameterBag |
121
|
|
|
*/ |
122
|
|
|
public function getRequest() |
123
|
|
|
{ |
124
|
|
|
return $this->request; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* The getter for query. |
129
|
|
|
* |
130
|
|
|
* @return ParameterBag |
131
|
|
|
*/ |
132
|
|
|
public function getQuery() |
133
|
|
|
{ |
134
|
|
|
return $this->query; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* The getter for server. |
139
|
|
|
* |
140
|
|
|
* @return ParameterBag |
141
|
|
|
*/ |
142
|
|
|
public function getServer() |
143
|
|
|
{ |
144
|
|
|
return $this->server; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* The getter for headers. |
149
|
|
|
* |
150
|
|
|
* @return HeaderBag |
151
|
|
|
*/ |
152
|
|
|
public function getHeaders() |
153
|
|
|
{ |
154
|
|
|
return $this->headers; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Builds http headers. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
public function buildHttpHeaders() |
163
|
|
|
{ |
164
|
|
|
$headers = $this->headers->all(); |
165
|
|
|
if (!isset($headers)) { |
166
|
|
|
return''; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$content = []; |
170
|
|
|
ksort($headers); |
171
|
|
|
foreach ($headers as $name => $values) { |
172
|
|
|
$name = implode('-', array_map('ucfirst', explode('-', $name))); |
173
|
|
|
foreach ($values as $value) { |
174
|
|
|
$content[] = sprintf("%s: %s", $name, $value); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $content; |
179
|
|
|
} |
180
|
|
|
/** |
181
|
|
|
* Initilizes the list of formats. |
182
|
|
|
*/ |
183
|
|
|
protected static function initFormats() |
184
|
|
|
{ |
185
|
|
|
static::$formats = array( |
186
|
|
|
'html' => array('text/html'), |
187
|
|
|
'json' => array('application/json'), |
188
|
|
|
'xml' => array('application/xml'), |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|