Skip to content

Commit

Permalink
Implement parsed body methods.
Browse files Browse the repository at this point in the history
Add more of the PSR7 interface in.
  • Loading branch information
markstory committed Sep 4, 2016
1 parent 7aafb67 commit f795822
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Network/Request.php
Expand Up @@ -1403,6 +1403,36 @@ public function withCookieParams(array $cookies)
return $new;
}

/**
* Get the parsed request body data.
*
* If the request Content-Type is either application/x-www-form-urlencoded
* or multipart/form-data, nd the request method is POST, this will be the
* post data. For other content types, it may be the deserialized request
* body.
*
* @return null|array|object The deserialized body parameters, if any.
* These will typically be an array or object.
*/
public function getParsedBody()
{
return $this->data;
}

/**
* Update the parsed body and get a new instance.
*
* @param null|array|object $data The deserialized body data. This will
* typically be in an array or object.
* @return static
*/
public function withParsedBody($data)
{
$new = clone $this;
$new->data = $data;
return $new;
}

/**
* Get/Set value from the request's environment data.
* Fallback to using env() if key not set in $environment property.
Expand Down
31 changes: 31 additions & 0 deletions tests/TestCase/Network/RequestTest.php
Expand Up @@ -2704,6 +2704,37 @@ public function testWithParam()
$this->assertSame('Admin', $result->param('prefix'));
}

/**
* Test getting the parsed body parameters.
*
* @return void
*/
public function testGetParsedBody()
{
$data = ['title' => 'First', 'body' => 'Best Article!'];
$request = new Request(['post' => $data]);
$this->assertSame($data, $request->getParsedBody());

$request = new Request();
$this->assertSame([], $request->getParsedBody());
}

/**
* Test replacing the parsed body parameters.
*
* @return void
*/
public function testWithParsedBody()
{
$data = ['title' => 'First', 'body' => 'Best Article!'];
$request = new Request([]);
$new = $request->withParsedBody($data);

$this->assertNotSame($request, $new);
$this->assertSame([], $request->getParsedBody());
$this->assertSame($data, $new->getParsedBody());
}

/**
* Test updating POST data in a psr7 fashion.
*
Expand Down

0 comments on commit f795822

Please sign in to comment.