Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 7ad6914

Browse files
committed
Merge pull request #58 from franzliedke/patch-2
Add shortcut method for empty responses
2 parents 3224057 + efa0fa7 commit 7ad6914

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

src/Response/EmptyResponse.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace Zend\Diactoros\Response;
11+
12+
use Zend\Diactoros\Response;
13+
use Zend\Diactoros\Stream;
14+
15+
/**
16+
* A class representing empty HTTP responses.
17+
*/
18+
class EmptyResponse extends Response
19+
{
20+
/**
21+
* Create an empty response with the given status code.
22+
*
23+
* @param int $status Status code for the response, if any.
24+
* @param array $headers Headers for the response, if any.
25+
*/
26+
public function __construct($status = 204, array $headers = [])
27+
{
28+
$body = new Stream('php://temp', 'r');
29+
parent::__construct($body, $status, $headers);
30+
}
31+
32+
/**
33+
* Create an empty response with the given headers.
34+
*
35+
* @param array $headers Headers for the response.
36+
* @return EmptyResponse
37+
*/
38+
public static function withHeaders(array $headers)
39+
{
40+
return new static(204, $headers);
41+
}
42+
}

src/Response/StringResponse.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private function __construct()
7171
* Create a Response from the provided information.
7272
*
7373
* Creates a Response using a php://temp stream, and writes the provided
74-
* body to the stream; if non content-type header was provided, the given
74+
* body to the stream; if no content-type header was provided, the given
7575
* $contentType is injected for it.
7676
*
7777
* @param string $body
@@ -85,7 +85,7 @@ private static function createResponse($body, $status, array $headers, $contentT
8585
$response = new Response('php://temp', $status, $headers);
8686
$response->getBody()->write($body);
8787

88-
if ($response->hasHeader('content-type')) {
88+
if (empty($contentType) || $response->hasHeader('content-type')) {
8989
return $response;
9090
}
9191

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Zend Framework (http://framework.zend.com/)
4+
*
5+
* @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6+
* @copyright Copyright (c) 2015 Zend Technologies USA Inc. (http://www.zend.com)
7+
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
8+
*/
9+
10+
namespace ZendTest\Diactoros\Response;
11+
12+
use PHPUnit_Framework_TestCase as TestCase;
13+
use Zend\Diactoros\Response\EmptyResponse;
14+
15+
class EmptyResponseTest extends TestCase
16+
{
17+
public function testConstructor()
18+
{
19+
$response = new EmptyResponse(201);
20+
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
21+
$this->assertEquals('', (string) $response->getBody());
22+
$this->assertEquals(201, $response->getStatusCode());
23+
}
24+
25+
public function testHeaderConstructor()
26+
{
27+
$response = EmptyResponse::withHeaders(['x-empty' => ['true']]);
28+
$this->assertInstanceOf('Zend\Diactoros\Response', $response);
29+
$this->assertEquals('', (string) $response->getBody());
30+
$this->assertEquals(204, $response->getStatusCode());
31+
$this->assertEquals('true', $response->getHeaderLine('x-empty'));
32+
}
33+
}

0 commit comments

Comments
 (0)