Skip to content

Commit

Permalink
Merge branch 'feature/http-request-accessors'
Browse files Browse the repository at this point in the history
  • Loading branch information
weierophinney committed Jul 25, 2012
2 parents d509b52 + 73e4bf6 commit 0ed7fc9
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 38 deletions.
28 changes: 20 additions & 8 deletions library/Zend/Http/PhpEnvironment/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,24 @@ public function setServer(ParametersInterface $server)
}

/**
* Return the parameter container responsible for server parameters
* Return the parameter container responsible for server parameters or a single parameter value.
*
* @param string|null $name Parameter name to retrieve, or null to get the whole container.
* @param mixed|null $default Default value to use when the parameter is missing.
* @see http://www.faqs.org/rfcs/rfc3875.html
* @return ParametersInterface
* @return \Zend\Stdlib\ParametersInterface|mixed
*/
public function getServer()
public function getServer($name = null, $default = null)
{
if ($this->serverParams === null) {
$this->serverParams = new Parameters();
}

return $this->serverParams;
if($name === null){
return $this->serverParams;
}

return $this->serverParams->get($name, $default);
}

/**
Expand All @@ -322,17 +328,23 @@ public function setEnv(ParametersInterface $env)
}

/**
* Return the parameter container responsible for env parameters
* Return the parameter container responsible for env parameters or a single parameter value.
*
* @return ParametersInterface
* @param string|null $name Parameter name to retrieve, or null to get the whole container.
* @param mixed|null $default Default value to use when the parameter is missing. * @return \Zend\Stdlib\ParametersInterface
* @return \Zend\Stdlib\ParametersInterface|mixed
*/
public function getEnv()
public function getEnv($name = null, $default = null)
{
if ($this->envParams === null) {
$this->envParams = new Parameters();
}

return $this->envParams;
if($name === null){
return $this->envParams;
}

return $this->envParams->get($name, $default);
}

/**
Expand Down
83 changes: 70 additions & 13 deletions library/Zend/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,23 @@ public function setQuery(ParametersInterface $query)
}

/**
* Return the parameter container responsible for query parameters
* Return the parameter container responsible for query parameters or a single query parameter
*
* @return \Zend\Stdlib\ParametersInterface
* @param string|null $name Parameter name to retrieve, or null to get the whole container.
* @param mixed|null $default Default value to use when the parameter is missing.
* @return \Zend\Stdlib\ParametersInterface|mixed
*/
public function getQuery()
public function getQuery($name = null, $default = null)
{
if ($this->queryParams === null) {
$this->queryParams = new Parameters();
}

return $this->queryParams;
if($name === null){
return $this->queryParams;
}

return $this->queryParams->get($name, $default);
}

/**
Expand All @@ -252,17 +258,23 @@ public function setPost(ParametersInterface $post)
}

/**
* Return the parameter container responsible for post parameters
* Return the parameter container responsible for post parameters or a single post parameter.
*
* @return \Zend\Stdlib\ParametersInterface
* @param string|null $name Parameter name to retrieve, or null to get the whole container.
* @param mixed|null $default Default value to use when the parameter is missing.
* @return \Zend\Stdlib\ParametersInterface|mixed
*/
public function getPost()
public function getPost($name = null, $default = null)
{
if ($this->postParams === null) {
$this->postParams = new Parameters();
}

return $this->postParams;
if($name === null){
return $this->postParams;
}

return $this->postParams->get($name, $default);
}

/**
Expand Down Expand Up @@ -290,17 +302,62 @@ public function setFiles(ParametersInterface $files)
}

/**
* Return the parameter container responsible for file parameters
* Return the parameter container responsible for file parameters or a single file.
*
* @return ParametersInterface
* @param string|null $name Parameter name to retrieve, or null to get the whole container.
* @param mixed|null $default Default value to use when the parameter is missing.
* @return ParametersInterface|mixed
*/
public function getFiles()
public function getFiles($name = null, $default = null)
{
if ($this->fileParams === null) {
$this->fileParams = new Parameters();
}

return $this->fileParams;
if($name === null){
return $this->fileParams;
}

return $this->fileParams->get($name, $default);
}

/**
* Return the header container responsible for headers or all headers of a certain name/type
*
* @see \Zend\Http\Headers::get()
* @param string|null $name Header name to retrieve, or null to get the whole container.
* @param mixed|null $default Default value to use when the requested header is missing.
* @return \Zend\Http\Headers|bool|\Zend\Http\Header\HeaderInterface|\ArrayIterator
*/
public function getHeaders($name = null, $default = false)
{
if ($this->headers === null || is_string($this->headers)) {
// this is only here for fromString lazy loading
$this->headers = (is_string($this->headers)) ? Headers::fromString($this->headers) : new Headers();
}

if($name === null){
return $this->headers;
}

if($this->headers->has($name)){
return $this->headers->get($name);
}

return $default;
}

/**
* Get all headers of a certain name/type.
*
* @see Request::getHeaders()
* @param string|null $name Header name to retrieve, or null to get the whole container.
* @param mixed|null $default Default value to use when the requested header is missing.
* @return \Zend\Http\Headers|bool|\Zend\Http\Header\HeaderInterface|\ArrayIterator
*/
public function getHeader($name, $default = false)
{
return $this->getHeaders($name, $default);
}

/**
Expand Down Expand Up @@ -440,4 +497,4 @@ public function toString()
$str .= $this->getContent();
return $str;
}
}
}
58 changes: 41 additions & 17 deletions library/Zend/Mvc/Controller/Plugin/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,27 @@ public function __invoke($param = NULL, $default = null)
}

/**
* Get a param from the route match.
*
* @param string $param
* @param mixed $default
* @return mixed
* @throws RuntimeException
* Retrieve a named $_FILES value
*
* @param string $name
* @param mixed $default
* @return array|\ArrayAccess|null
*/
public function fromRoute($param, $default = null)
public function fromFiles($name, $default = null)
{
$controller = $this->getController();

if (!$controller instanceof InjectApplicationEventInterface) {
throw new RuntimeException(
'Controllers must implement Zend\Mvc\InjectApplicationEventInterface to use this plugin.'
);
}
return $this->getController()->getRequest()->getFiles($name, $default);
}

return $controller->getEvent()->getRouteMatch()->getParam($param, $default);
/**
* Get a header
*
* @param string $header
* @param mixed $default
* @return null|\Zend\Http\Header\HeaderInterface
*/
public function fromHeader($header, $default = null)
{
return $this->getController()->getRequest()->getHeaders($header, $default);
}

/**
Expand All @@ -66,7 +69,7 @@ public function fromRoute($param, $default = null)
*/
public function fromPost($param, $default = null)
{
return $this->getController()->getRequest()->getPost()->get($param, $default);
return $this->getController()->getRequest()->getPost($param, $default);
}

/**
Expand All @@ -78,6 +81,27 @@ public function fromPost($param, $default = null)
*/
public function fromQuery($param, $default = null)
{
return $this->getController()->getRequest()->getQuery()->get($param, $default);
return $this->getController()->getRequest()->getQuery($param, $default);
}

/**
* Get a param from the route match.
*
* @param string $param
* @param mixed $default
* @return mixed
* @throws RuntimeException
*/
public function fromRoute($param, $default = null)
{
$controller = $this->getController();

if (!$controller instanceof InjectApplicationEventInterface) {
throw new RuntimeException(
'Controllers must implement Zend\Mvc\InjectApplicationEventInterface to use this plugin.'
);
}

return $controller->getEvent()->getRouteMatch()->getParam($param, $default);
}
}
52 changes: 52 additions & 0 deletions tests/Zend/Http/PhpEnvironment/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace ZendTest\Http\PhpEnvironment;

use PHPUnit_Framework_TestCase as TestCase;
use Zend\Http\Headers;
use Zend\Http\Header\GenericHeader;
use Zend\Http\PhpEnvironment\Request;

class RequestTest extends TestCase
Expand Down Expand Up @@ -581,4 +583,54 @@ public function testRequestMapsPhpFies(array $files, array $expectedFiles)
$request = new Request();
$this->assertEquals($expectedFiles, $request->getFiles()->toArray());
}

public function testParameterRetrievalDefaultValue()
{
$request = new Request();
$p = new \Zend\Stdlib\Parameters(array(
'foo' => 'bar'
));
$request->setQuery($p);
$request->setPost($p);
$request->setFiles($p);
$request->setServer($p);
$request->setEnv($p);

$default = 15;
$this->assertSame($default, $request->getQuery('baz', $default));
$this->assertSame($default, $request->getPost('baz', $default));
$this->assertSame($default, $request->getFiles('baz', $default));
$this->assertSame($default, $request->getServer('baz', $default));
$this->assertSame($default, $request->getEnv('baz', $default));
$this->assertSame($default, $request->getHeaders('baz', $default));
$this->assertSame($default, $request->getHeader('baz', $default));
}

public function testRetrievingASingleValueForParameters()
{
$request = new Request();
$p = new \Zend\Stdlib\Parameters(array(
'foo' => 'bar'
));
$request->setQuery($p);
$request->setPost($p);
$request->setFiles($p);
$request->setServer($p);
$request->setEnv($p);

$this->assertSame('bar', $request->getQuery('foo'));
$this->assertSame('bar', $request->getPost('foo'));
$this->assertSame('bar', $request->getFiles('foo'));
$this->assertSame('bar', $request->getServer('foo'));
$this->assertSame('bar', $request->getEnv('foo'));

$headers = new Headers();
$h = new GenericHeader('foo','bar');
$headers->addHeader($h);

$request->setHeaders($headers);
$this->assertSame($headers, $request->getHeaders());
$this->assertSame($h, $request->getHeaders()->get('foo'));
$this->assertSame($h, $request->getHeader('foo'));
}
}
48 changes: 48 additions & 0 deletions tests/Zend/Http/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace ZendTest\Http;

use Zend\Http\Request;
use Zend\Http\Headers;
use Zend\Http\Header\GenericHeader;

class RequestTest extends \PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -45,6 +47,52 @@ public function testRequestAllowsSettingOfParameterContainer()
$this->assertSame($p, $request->getQuery());
$this->assertSame($p, $request->getPost());
$this->assertSame($p, $request->getFiles());

$headers = new Headers();
$request->setHeaders($headers);
$this->assertSame($headers, $request->getHeaders());
}

public function testRetrievingASingleValueForParameters()
{
$request = new Request();
$p = new \Zend\Stdlib\Parameters(array(
'foo' => 'bar'
));
$request->setQuery($p);
$request->setPost($p);
$request->setFiles($p);

$this->assertSame('bar', $request->getQuery('foo'));
$this->assertSame('bar', $request->getPost('foo'));
$this->assertSame('bar', $request->getFiles('foo'));

$headers = new Headers();
$h = new GenericHeader('foo','bar');
$headers->addHeader($h);

$request->setHeaders($headers);
$this->assertSame($headers, $request->getHeaders());
$this->assertSame($h, $request->getHeaders()->get('foo'));
$this->assertSame($h, $request->getHeader('foo'));
}

public function testParameterRetrievalDefaultValue()
{
$request = new Request();
$p = new \Zend\Stdlib\Parameters(array(
'foo' => 'bar'
));
$request->setQuery($p);
$request->setPost($p);
$request->setFiles($p);

$default = 15;
$this->assertSame($default, $request->getQuery('baz', $default));
$this->assertSame($default, $request->getPost('baz', $default));
$this->assertSame($default, $request->getFiles('baz', $default));
$this->assertSame($default, $request->getHeaders('baz', $default));
$this->assertSame($default, $request->getHeader('baz', $default));
}

public function testRequestPersistsRawBody()
Expand Down
Loading

0 comments on commit 0ed7fc9

Please sign in to comment.