Skip to content

Commit

Permalink
Merge branch 'hotfix/http-message-0.4.0'
Browse files Browse the repository at this point in the history
Close #13
  • Loading branch information
weierophinney committed Oct 17, 2014
2 parents 2ad4c0e + 34d5380 commit 33f478e
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 306 deletions.
18 changes: 14 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

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

## TBD - TBD
## 0.6.0 - 2014-10.17

Updated component to psr/http-message 0.4.0. That release contains a number of backwards-incompatible changes.

### Added

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

### Deprecated

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

### Removed

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

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"require": {
"php": ">=5.4.8",
"psr/http-message": "~0.3.0"
"psr/http-message": "~0.4.0"
},
"require-dev": {
"phpunit/PHPUnit": "3.7.*",
Expand Down
155 changes: 49 additions & 106 deletions src/IncomingRequest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Phly\Http;

use ArrayAccess;
use InvalidArgumentException;
use Psr\Http\Message\IncomingRequestInterface;

Expand All @@ -25,54 +24,55 @@
class IncomingRequest extends Request implements IncomingRequestInterface
{
/**
* @var array|object
* @var array
*/
private $bodyParams = [];
private $attributes = [];

/**
* @var array|ArrayAccess
* @var array
*/
private $cookieParams;
private $bodyParams = [];

/**
* @var array|ArrayAccess
* @var array
*/
private $fileParams;
private $cookieParams;

/**
* @var array|ArrayAccess
* @var array
*/
private $pathParams = [];
private $fileParams;

/**
* @var array|ArrayAccess
* @var array
*/
private $queryParams;

/**
* @param string|\Psr\Http\Message\StreamableInterface|array $stream Stream representing message body.
* Alternately, this can be an array with keys for each of the possible arguments.
* @param array|ArrayAccess $cookieParams Deserialized cookies
* @param array|ArrayAccess $pathParams Variables matched from the URI path
* @param array|ArrayAccess $queryParams Deserialized query string arguments
* @param array|ArrayAccess $bodyParams Deserialized body parameters
* @param array|ArrayAccess $fileParams Upload file information; should be in PHP's $_FILES format
* @param string|\Psr\Http\Message\StreamableInterface|array $stream
* Stream representing message body. Alternately, this can be an
* array with keys for each of the possible arguments.
* @param array $cookieParams Deserialized cookies
* @param array $attributes Attributes derived from the request
* @param array $queryParams Deserialized query string arguments
* @param array $bodyParams Deserialized body parameters
* @param array $fileParams Upload file information; should be in PHP's $_FILES format
* @return void
*/
public function __construct(
$stream = 'php://input',
$cookieParams = [],
$pathParams = [],
$queryParams = [],
$bodyParams = [],
$fileParams = []
array $cookieParams = [],
array $attributes = [],
array $queryParams = [],
array $bodyParams = [],
array $fileParams = []
) {
if (is_array($stream)) {
if (isset($stream['cookieParams']) && empty($cookieParams)) {
$cookieParams = $stream['cookieParams'];
}
if (isset($stream['pathParams']) && empty($pathParams)) {
$pathParams = $stream['pathParams'];
if (isset($stream['attributes']) && empty($attributes)) {
$attributes = $stream['attributes'];
}
if (isset($stream['queryParams']) && empty($queryParams)) {
$queryParams = $stream['queryParams'];
Expand All @@ -89,7 +89,7 @@ public function __construct(

parent::__construct($stream);
$this->setCookieParams($cookieParams);
$this->setPathParams($pathParams);
$this->setAttributes($attributes);
$this->setQueryParams($queryParams);
$this->setBodyParams($bodyParams);
$this->setFileParams($fileParams);
Expand All @@ -100,14 +100,7 @@ public function __construct(
*
* Retrieves cookies sent by the client to the server.
*
* The assumption is these are injected during instantiation, typically
* from PHP's $_COOKIE superglobal, and should remain immutable over the
* course of the incoming request.
*
* The return value can be either an array or an object that acts like
* an array (e.g., implements ArrayAccess, or an ArrayObject).
*
* @return array|ArrayAccess
* @return array
*/
public function getCookieParams()
{
Expand All @@ -123,21 +116,10 @@ public function getCookieParams()
* the original value, filter them, and re-inject into the incoming
* request..
*
* The value provided should be an array or array-like object
* (e.g., implements ArrayAccess, or an ArrayObject).
*
* @param array|ArrayAccess $cookies Cookie values/structs
*
* @return void
* @param array $cookies Cookie values/structs
*/
public function setCookieParams($cookies)
public function setCookieParams(array $cookies)
{
if (! is_array($cookies) && ! $cookies instanceof ArrayAccess) {
throw new InvalidArgumentException(
'Cookies must be provided as either an array or ArrayAccess'
);
}

$this->cookieParams = $cookies;
}

Expand All @@ -150,10 +132,7 @@ public function setCookieParams($cookies)
* from PHP's $_GET superglobal, and should remain immutable over the
* course of the incoming request.
*
* The return value can be either an array or an object that acts like
* an array (e.g., implements ArrayAccess, or an ArrayObject).
*
* @return array|ArrayAccess
* @return array
*/
public function getQueryParams()
{
Expand All @@ -165,16 +144,10 @@ public function getQueryParams()
*
* Internal method only.
*
* @param array|ArrayAccess $queryParams
* @param array $queryParams
*/
private function setQueryParams($queryParams)
private function setQueryParams(array $queryParams)
{
if (! is_array($queryParams) && ! $queryParams instanceof ArrayAccess) {
throw new InvalidArgumentException(
'Query string arguments must be provided as either an array or ArrayAccess'
);
}

$this->queryParams = $queryParams;
}

Expand All @@ -188,10 +161,7 @@ private function setQueryParams($queryParams)
* from PHP's $_FILES superglobal, and should remain immutable over the
* course of the incoming request.
*
* The return value can be either an array or an object that acts like
* an array (e.g., implements ArrayAccess, or an ArrayObject).
*
* @return array|ArrayAccess Upload file(s) metadata, if any.
* @return array Upload file(s) metadata, if any.
*/
public function getFileParams()
{
Expand All @@ -203,32 +173,24 @@ public function getFileParams()
*
* Internal method only.
*
* @param array|ArrayAccess $fileParams
* @param array $fileParams
*/
private function setFileParams($fileParams)
private function setFileParams(array $fileParams)
{
if (! is_array($fileParams) && ! $fileParams instanceof ArrayAccess) {
throw new InvalidArgumentException(
'Files must be provided as either an array or ArrayAccess'
);
}

$this->fileParams = $fileParams;
}

/**
* Retrieve any parameters provided in the request body.
*
* If the request body can be deserialized, and if the deserialized values
* can be represented as an array or object, this method can be used to
* can be represented as an array, this method can be used to
* retrieve them.
*
* In other cases, the parent getBody() method should be used to retrieve
* the body content.
*
* @return array|object The deserialized body parameters, if any. These may
* be either an array or an object, though an array or
* array-like object is recommended.
* @return array The deserialized body parameters, if any.
*/
public function getBodyParams()
{
Expand All @@ -238,61 +200,42 @@ public function getBodyParams()
/**
* Set the request body parameters.
*
* If the body content can be deserialized, the values obtained may then
* If the body content can be deserialized as an array, the values obtained may then
* be injected into the response using this method. This method will
* typically be invoked by a factory marshaling request parameters.
*
* @param array|object $values The deserialized body parameters, if any.
* These may be either an array or an object,
* though an array or array-like object is
* recommended.
*
* @return void
* @param array $values The deserialized body parameters, if any.
*/
public function setBodyParams($values)
public function setBodyParams(array $values)
{
if (! is_array($values) && ! is_object($values)) {
throw new InvalidArgumentException(
'Body parameters must be provided as either an array or an object'
);
}

$this->bodyParams = $values;
}

/**
* Retrieve parameters matched during routing.
* Retrieve attributes derived from the request
*
* If a router or similar is used to match against the path and/or request,
* this method can be used to retrieve the results, so long as those
* results can be represented as an array or array-like object.
* results can be represented as an array.
*
* @return array|ArrayAccess Path parameters matched by routing
* @return array Path parameters matched by routing
*/
public function getPathParams()
public function getAttributes()
{
return $this->pathParams;
return $this->attributes;
}

/**
* Set parameters discovered by matching that path
*
* If a router or similar is used to match against the path and/or request,
* this method can be used to inject the request with the results, so long
* as those results can be represented as an array or array-like object.
* this method can be used to inject them, so long as those
* results can be represented as an array.
*
* @param array|ArrayAccess $values Path parameters matched by routing
*
* @return void
* @param array $values Path parameters matched by routing
*/
public function setPathParams(array $values)
public function setAttributes(array $values)
{
if (! is_array($values) && ! $values instanceof ArrayAccess) {
throw new InvalidArgumentException(
'Path parameters must be provided as either an array or ArrayAccess'
);
}

$this->pathParams = $values;
$this->attributes = $values;
}
}
18 changes: 17 additions & 1 deletion src/IncomingRequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Phly\Http;

use Psr\Http\Message\IncomingRequestInterface;
use Psr\Http\Message\RequestInterface;
use stdClass;

/**
Expand Down Expand Up @@ -72,7 +73,7 @@ public static function fromServer(array $server, IncomingRequestInterface $reque
}

$request->setMethod(self::get('REQUEST_METHOD', $server, 'GET'));
$request->setHeaders(self::marshalHeaders($server));
self::setHeaders($request, self::marshalHeaders($server));
$request->setUrl(self::marshalUri($server, $request));
return $request;
}
Expand Down Expand Up @@ -157,6 +158,21 @@ public static function marshalHeaders(array $server)
return $headers;
}

/**
* Set the headers for a request.
*
* Injects the given request instance with the headers provided.
*
* @param RequestInterface $request
* @param array $headers
*/
public static function setHeaders(RequestInterface $request, array $headers)
{
foreach ($headers as $header => $values) {
$request->setHeader($header, $values);
}
}

/**
* Marshal the URI from the $_SERVER array and headers
*
Expand Down
Loading

0 comments on commit 33f478e

Please sign in to comment.