Skip to content
This repository was archived by the owner on Aug 17, 2025. It is now read-only.

Commit 33f478e

Browse files
committed
Merge branch 'hotfix/http-message-0.4.0'
Close #13
2 parents 2ad4c0e + 34d5380 commit 33f478e

File tree

10 files changed

+144
-306
lines changed

10 files changed

+144
-306
lines changed

CHANGELOG.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,29 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release..
44

5-
## TBD - TBD
5+
## 0.6.0 - 2014-10.17
6+
7+
Updated component to psr/http-message 0.4.0. That release contains a number of backwards-incompatible changes.
68

79
### Added
810

9-
- Nothing.
11+
- Added IncomingRequestFactory::setHeaders() for simplifying setting
12+
(overwriting) many headers at once from an array.
13+
- Updated MessageTrait::addHeader() to allow array values
14+
- Modified IncomingRequest to `s/PathParams/Attributes/g`
1015

1116
### Deprecated
1217

13-
- Nothing.
18+
- IncomingRequest now only allows arrays for either input or return values; Array-like objects are no longer accepted.
19+
- Removed ability to pass objects to MessageTrait::addHeader()/setHeader()
20+
- Removed setHeaders()/addHeaders() from MessageTrait
21+
- Modified IncomingRequest to `s/PathParams/Attributes/g`
1422

1523
### Removed
1624

17-
- Nothing.
25+
- Removed ability to pass objects to MessageTrait::addHeader()/setHeader()
26+
- Removed setHeaders()/addHeaders() from MessageTrait
27+
- Modified IncomingRequest to `s/PathParams/Attributes/g`
1828

1929
### Fixed
2030

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"require": {
2828
"php": ">=5.4.8",
29-
"psr/http-message": "~0.3.0"
29+
"psr/http-message": "~0.4.0"
3030
},
3131
"require-dev": {
3232
"phpunit/PHPUnit": "3.7.*",

src/IncomingRequest.php

Lines changed: 49 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace Phly\Http;
33

4-
use ArrayAccess;
54
use InvalidArgumentException;
65
use Psr\Http\Message\IncomingRequestInterface;
76

@@ -25,54 +24,55 @@
2524
class IncomingRequest extends Request implements IncomingRequestInterface
2625
{
2726
/**
28-
* @var array|object
27+
* @var array
2928
*/
30-
private $bodyParams = [];
29+
private $attributes = [];
3130

3231
/**
33-
* @var array|ArrayAccess
32+
* @var array
3433
*/
35-
private $cookieParams;
34+
private $bodyParams = [];
3635

3736
/**
38-
* @var array|ArrayAccess
37+
* @var array
3938
*/
40-
private $fileParams;
39+
private $cookieParams;
4140

4241
/**
43-
* @var array|ArrayAccess
42+
* @var array
4443
*/
45-
private $pathParams = [];
44+
private $fileParams;
4645

4746
/**
48-
* @var array|ArrayAccess
47+
* @var array
4948
*/
5049
private $queryParams;
5150

5251
/**
53-
* @param string|\Psr\Http\Message\StreamableInterface|array $stream Stream representing message body.
54-
* Alternately, this can be an array with keys for each of the possible arguments.
55-
* @param array|ArrayAccess $cookieParams Deserialized cookies
56-
* @param array|ArrayAccess $pathParams Variables matched from the URI path
57-
* @param array|ArrayAccess $queryParams Deserialized query string arguments
58-
* @param array|ArrayAccess $bodyParams Deserialized body parameters
59-
* @param array|ArrayAccess $fileParams Upload file information; should be in PHP's $_FILES format
52+
* @param string|\Psr\Http\Message\StreamableInterface|array $stream
53+
* Stream representing message body. Alternately, this can be an
54+
* array with keys for each of the possible arguments.
55+
* @param array $cookieParams Deserialized cookies
56+
* @param array $attributes Attributes derived from the request
57+
* @param array $queryParams Deserialized query string arguments
58+
* @param array $bodyParams Deserialized body parameters
59+
* @param array $fileParams Upload file information; should be in PHP's $_FILES format
6060
* @return void
6161
*/
6262
public function __construct(
6363
$stream = 'php://input',
64-
$cookieParams = [],
65-
$pathParams = [],
66-
$queryParams = [],
67-
$bodyParams = [],
68-
$fileParams = []
64+
array $cookieParams = [],
65+
array $attributes = [],
66+
array $queryParams = [],
67+
array $bodyParams = [],
68+
array $fileParams = []
6969
) {
7070
if (is_array($stream)) {
7171
if (isset($stream['cookieParams']) && empty($cookieParams)) {
7272
$cookieParams = $stream['cookieParams'];
7373
}
74-
if (isset($stream['pathParams']) && empty($pathParams)) {
75-
$pathParams = $stream['pathParams'];
74+
if (isset($stream['attributes']) && empty($attributes)) {
75+
$attributes = $stream['attributes'];
7676
}
7777
if (isset($stream['queryParams']) && empty($queryParams)) {
7878
$queryParams = $stream['queryParams'];
@@ -89,7 +89,7 @@ public function __construct(
8989

9090
parent::__construct($stream);
9191
$this->setCookieParams($cookieParams);
92-
$this->setPathParams($pathParams);
92+
$this->setAttributes($attributes);
9393
$this->setQueryParams($queryParams);
9494
$this->setBodyParams($bodyParams);
9595
$this->setFileParams($fileParams);
@@ -100,14 +100,7 @@ public function __construct(
100100
*
101101
* Retrieves cookies sent by the client to the server.
102102
*
103-
* The assumption is these are injected during instantiation, typically
104-
* from PHP's $_COOKIE superglobal, and should remain immutable over the
105-
* course of the incoming request.
106-
*
107-
* The return value can be either an array or an object that acts like
108-
* an array (e.g., implements ArrayAccess, or an ArrayObject).
109-
*
110-
* @return array|ArrayAccess
103+
* @return array
111104
*/
112105
public function getCookieParams()
113106
{
@@ -123,21 +116,10 @@ public function getCookieParams()
123116
* the original value, filter them, and re-inject into the incoming
124117
* request..
125118
*
126-
* The value provided should be an array or array-like object
127-
* (e.g., implements ArrayAccess, or an ArrayObject).
128-
*
129-
* @param array|ArrayAccess $cookies Cookie values/structs
130-
*
131-
* @return void
119+
* @param array $cookies Cookie values/structs
132120
*/
133-
public function setCookieParams($cookies)
121+
public function setCookieParams(array $cookies)
134122
{
135-
if (! is_array($cookies) && ! $cookies instanceof ArrayAccess) {
136-
throw new InvalidArgumentException(
137-
'Cookies must be provided as either an array or ArrayAccess'
138-
);
139-
}
140-
141123
$this->cookieParams = $cookies;
142124
}
143125

@@ -150,10 +132,7 @@ public function setCookieParams($cookies)
150132
* from PHP's $_GET superglobal, and should remain immutable over the
151133
* course of the incoming request.
152134
*
153-
* The return value can be either an array or an object that acts like
154-
* an array (e.g., implements ArrayAccess, or an ArrayObject).
155-
*
156-
* @return array|ArrayAccess
135+
* @return array
157136
*/
158137
public function getQueryParams()
159138
{
@@ -165,16 +144,10 @@ public function getQueryParams()
165144
*
166145
* Internal method only.
167146
*
168-
* @param array|ArrayAccess $queryParams
147+
* @param array $queryParams
169148
*/
170-
private function setQueryParams($queryParams)
149+
private function setQueryParams(array $queryParams)
171150
{
172-
if (! is_array($queryParams) && ! $queryParams instanceof ArrayAccess) {
173-
throw new InvalidArgumentException(
174-
'Query string arguments must be provided as either an array or ArrayAccess'
175-
);
176-
}
177-
178151
$this->queryParams = $queryParams;
179152
}
180153

@@ -188,10 +161,7 @@ private function setQueryParams($queryParams)
188161
* from PHP's $_FILES superglobal, and should remain immutable over the
189162
* course of the incoming request.
190163
*
191-
* The return value can be either an array or an object that acts like
192-
* an array (e.g., implements ArrayAccess, or an ArrayObject).
193-
*
194-
* @return array|ArrayAccess Upload file(s) metadata, if any.
164+
* @return array Upload file(s) metadata, if any.
195165
*/
196166
public function getFileParams()
197167
{
@@ -203,32 +173,24 @@ public function getFileParams()
203173
*
204174
* Internal method only.
205175
*
206-
* @param array|ArrayAccess $fileParams
176+
* @param array $fileParams
207177
*/
208-
private function setFileParams($fileParams)
178+
private function setFileParams(array $fileParams)
209179
{
210-
if (! is_array($fileParams) && ! $fileParams instanceof ArrayAccess) {
211-
throw new InvalidArgumentException(
212-
'Files must be provided as either an array or ArrayAccess'
213-
);
214-
}
215-
216180
$this->fileParams = $fileParams;
217181
}
218182

219183
/**
220184
* Retrieve any parameters provided in the request body.
221185
*
222186
* If the request body can be deserialized, and if the deserialized values
223-
* can be represented as an array or object, this method can be used to
187+
* can be represented as an array, this method can be used to
224188
* retrieve them.
225189
*
226190
* In other cases, the parent getBody() method should be used to retrieve
227191
* the body content.
228192
*
229-
* @return array|object The deserialized body parameters, if any. These may
230-
* be either an array or an object, though an array or
231-
* array-like object is recommended.
193+
* @return array The deserialized body parameters, if any.
232194
*/
233195
public function getBodyParams()
234196
{
@@ -238,61 +200,42 @@ public function getBodyParams()
238200
/**
239201
* Set the request body parameters.
240202
*
241-
* If the body content can be deserialized, the values obtained may then
203+
* If the body content can be deserialized as an array, the values obtained may then
242204
* be injected into the response using this method. This method will
243205
* typically be invoked by a factory marshaling request parameters.
244206
*
245-
* @param array|object $values The deserialized body parameters, if any.
246-
* These may be either an array or an object,
247-
* though an array or array-like object is
248-
* recommended.
249-
*
250-
* @return void
207+
* @param array $values The deserialized body parameters, if any.
251208
*/
252-
public function setBodyParams($values)
209+
public function setBodyParams(array $values)
253210
{
254-
if (! is_array($values) && ! is_object($values)) {
255-
throw new InvalidArgumentException(
256-
'Body parameters must be provided as either an array or an object'
257-
);
258-
}
259-
260211
$this->bodyParams = $values;
261212
}
262213

263214
/**
264-
* Retrieve parameters matched during routing.
215+
* Retrieve attributes derived from the request
265216
*
266217
* If a router or similar is used to match against the path and/or request,
267218
* this method can be used to retrieve the results, so long as those
268-
* results can be represented as an array or array-like object.
219+
* results can be represented as an array.
269220
*
270-
* @return array|ArrayAccess Path parameters matched by routing
221+
* @return array Path parameters matched by routing
271222
*/
272-
public function getPathParams()
223+
public function getAttributes()
273224
{
274-
return $this->pathParams;
225+
return $this->attributes;
275226
}
276227

277228
/**
278229
* Set parameters discovered by matching that path
279230
*
280231
* If a router or similar is used to match against the path and/or request,
281-
* this method can be used to inject the request with the results, so long
282-
* as those results can be represented as an array or array-like object.
232+
* this method can be used to inject them, so long as those
233+
* results can be represented as an array.
283234
*
284-
* @param array|ArrayAccess $values Path parameters matched by routing
285-
*
286-
* @return void
235+
* @param array $values Path parameters matched by routing
287236
*/
288-
public function setPathParams(array $values)
237+
public function setAttributes(array $values)
289238
{
290-
if (! is_array($values) && ! $values instanceof ArrayAccess) {
291-
throw new InvalidArgumentException(
292-
'Path parameters must be provided as either an array or ArrayAccess'
293-
);
294-
}
295-
296-
$this->pathParams = $values;
239+
$this->attributes = $values;
297240
}
298241
}

src/IncomingRequestFactory.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Phly\Http;
33

44
use Psr\Http\Message\IncomingRequestInterface;
5+
use Psr\Http\Message\RequestInterface;
56
use stdClass;
67

78
/**
@@ -72,7 +73,7 @@ public static function fromServer(array $server, IncomingRequestInterface $reque
7273
}
7374

7475
$request->setMethod(self::get('REQUEST_METHOD', $server, 'GET'));
75-
$request->setHeaders(self::marshalHeaders($server));
76+
self::setHeaders($request, self::marshalHeaders($server));
7677
$request->setUrl(self::marshalUri($server, $request));
7778
return $request;
7879
}
@@ -157,6 +158,21 @@ public static function marshalHeaders(array $server)
157158
return $headers;
158159
}
159160

161+
/**
162+
* Set the headers for a request.
163+
*
164+
* Injects the given request instance with the headers provided.
165+
*
166+
* @param RequestInterface $request
167+
* @param array $headers
168+
*/
169+
public static function setHeaders(RequestInterface $request, array $headers)
170+
{
171+
foreach ($headers as $header => $values) {
172+
$request->setHeader($header, $values);
173+
}
174+
}
175+
160176
/**
161177
* Marshal the URI from the $_SERVER array and headers
162178
*

0 commit comments

Comments
 (0)