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

Commit ae63bcf

Browse files
committed
Refactor DownloadResponseTrait to be a new class, DownloadResponse
This takes advice from weierophinney that I should create a class called DownloadResponse which builds on DownloadResponseTrait. This is the first part of the refactor that changes enough of the functionality to keep the code functional. I'm planning to implement the follow-up advice as well, but one step at a time.
1 parent dd3ff9c commit ae63bcf

File tree

4 files changed

+310
-257
lines changed

4 files changed

+310
-257
lines changed

src/Response/CsvResponse.php

Lines changed: 84 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,105 +1,84 @@
1-
<?php
2-
/**
3-
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4-
* @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (https://www.zend.com)
5-
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6-
*/
7-
8-
declare(strict_types=1);
9-
10-
namespace Zend\Diactoros\Response;
11-
12-
use Psr\Http\Message\StreamInterface;
13-
use Zend\Diactoros\Exception;
14-
use Zend\Diactoros\Response;
15-
use Zend\Diactoros\Stream;
16-
17-
use function get_class;
18-
use function gettype;
19-
use function is_object;
20-
use function is_string;
21-
use function sprintf;
22-
23-
/**
24-
* CSV response.
25-
*
26-
* Allows creating a CSV response by passing a string to the constructor;
27-
* by default, sets a status code of 200 and sets the Content-Type header to
28-
* text/csv.
29-
*/
30-
class CsvResponse extends Response
31-
{
32-
use DownloadResponseTrait;
33-
use InjectContentTypeTrait;
34-
35-
/**
36-
* Create a CSV response.
37-
*
38-
* Produces a CSV response with a Content-Type of text/csv and a default
39-
* status of 200.
40-
*
41-
* @param string|StreamInterface $text String or stream for the message body.
42-
* @param int $status Integer status code for the response; 200 by default.
43-
* @param string $filename
44-
* @param array $headers Array of headers to use at initialization.
45-
*/
46-
public function __construct($text, int $status = 200, string $filename = '', array $headers = [])
47-
{
48-
if (is_string($filename) && $filename !== '') {
49-
$headers = $this->prepareDownloadHeaders($filename, $headers);
50-
}
51-
52-
parent::__construct(
53-
$this->createBody($text),
54-
$status,
55-
$this->injectContentType('text/csv; charset=utf-8', $headers)
56-
);
57-
}
58-
59-
/**
60-
* Create the CSV message body.
61-
*
62-
* @param string|StreamInterface $text
63-
* @return StreamInterface
64-
* @throws Exception\InvalidArgumentException if $text is neither a string or stream.
65-
*/
66-
private function createBody($text) : StreamInterface
67-
{
68-
if ($text instanceof StreamInterface) {
69-
return $text;
70-
}
71-
72-
if (! is_string($text)) {
73-
throw new Exception\InvalidArgumentException(sprintf(
74-
'Invalid CSV content (%s) provided to %s',
75-
(is_object($text) ? get_class($text) : gettype($text)),
76-
__CLASS__
77-
));
78-
}
79-
80-
$body = new Stream('php://temp', 'wb+');
81-
$body->write($text);
82-
$body->rewind();
83-
return $body;
84-
}
85-
86-
/**
87-
* Get download headers
88-
*
89-
* @param string $filename
90-
* @return array
91-
*/
92-
private function getDownloadHeaders(string $filename): array
93-
{
94-
$headers = [];
95-
$headers['cache-control'] = ['must-revalidate'];
96-
$headers['content-description'] = ['File Transfer'];
97-
$headers['content-disposition'] = [sprintf('attachment; filename=%s', $filename)];
98-
$headers['content-transfer-encoding'] = ['Binary'];
99-
$headers['content-type'] = ['text/csv; charset=utf-8'];
100-
$headers['expires'] = ['0'];
101-
$headers['pragma'] = ['Public'];
102-
103-
return $headers;
104-
}
105-
}
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4+
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Zend\Diactoros\Response;
11+
12+
use Psr\Http\Message\StreamInterface;
13+
use Zend\Diactoros\Exception;
14+
use Zend\Diactoros\Response;
15+
use Zend\Diactoros\Stream;
16+
17+
use function get_class;
18+
use function gettype;
19+
use function is_object;
20+
use function is_string;
21+
use function sprintf;
22+
23+
/**
24+
* CSV response.
25+
*
26+
* Allows creating a CSV response by passing a string to the constructor;
27+
* by default, sets a status code of 200 and sets the Content-Type header to
28+
* text/csv.
29+
*/
30+
class CsvResponse extends DownloadResponse
31+
{
32+
use InjectContentTypeTrait;
33+
34+
/**
35+
* Create a CSV response.
36+
*
37+
* Produces a CSV response with a Content-Type of text/csv and a default
38+
* status of 200.
39+
*
40+
* @param string|StreamInterface $text String or stream for the message body.
41+
* @param int $status Integer status code for the response; 200 by default.
42+
* @param string $filename
43+
* @param array $headers Array of headers to use at initialization.
44+
*/
45+
public function __construct($text, int $status = 200, string $filename = '', array $headers = [])
46+
{
47+
if ($filename !== '') {
48+
$headers = $this->prepareDownloadHeaders($filename, $headers);
49+
}
50+
51+
parent::__construct(
52+
$this->createBody($text),
53+
$status,
54+
$this->injectContentType('text/csv; charset=utf-8', $headers)
55+
);
56+
}
57+
58+
/**
59+
* Create the CSV message body.
60+
*
61+
* @param string|StreamInterface $text
62+
* @return StreamInterface
63+
* @throws Exception\InvalidArgumentException if $text is neither a string or stream.
64+
*/
65+
private function createBody($text) : StreamInterface
66+
{
67+
if ($text instanceof StreamInterface) {
68+
return $text;
69+
}
70+
71+
if (! is_string($text)) {
72+
throw new Exception\InvalidArgumentException(sprintf(
73+
'Invalid CSV content (%s) provided to %s',
74+
(is_object($text) ? get_class($text) : gettype($text)),
75+
__CLASS__
76+
));
77+
}
78+
79+
$body = new Stream('php://temp', 'wb+');
80+
$body->write($text);
81+
$body->rewind();
82+
return $body;
83+
}
84+
}

src/Response/DownloadResponseTrait.php renamed to src/Response/DownloadResponse.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-diactoros for the canonical source repository
4-
* @copyright Copyright (c) 2015-2018 Zend Technologies USA Inc. (https://www.zend.com)
4+
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
55
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
66
*/
77

@@ -11,17 +11,18 @@
1111

1212
use Zend\Diactoros\Exception\InvalidArgumentException;
1313

14+
use Zend\Diactoros\Response;
1415
use function array_keys;
1516
use function array_merge;
1617
use function implode;
1718
use function in_array;
1819
use function sprintf;
1920

2021
/**
21-
* Trait DownloadResponseTrait
22+
* Class DownloadResponse
2223
* @package Zend\Diactoros\Response
2324
*/
24-
trait DownloadResponseTrait
25+
class DownloadResponse extends Response
2526
{
2627
/**
2728
* A list of header keys required to be sent with a download response
@@ -51,7 +52,7 @@ public function overridesDownloadHeaders(array $downloadHeaders, array $headers
5152
$overridesDownloadHeaders = false;
5253

5354
foreach (array_keys($headers) as $header) {
54-
if (in_array($header, $downloadHeaders)) {
55+
if (in_array($header, $downloadHeaders, TRUE)) {
5556
$overridesDownloadHeaders = true;
5657
break;
5758
}
@@ -80,4 +81,23 @@ private function prepareDownloadHeaders(string $filename, array $headers = []) :
8081

8182
return array_merge($headers, $this->getDownloadHeaders($filename));
8283
}
84+
85+
/**
86+
* Get download headers
87+
*
88+
* @param string $filename
89+
* @return array
90+
*/
91+
private function getDownloadHeaders(string $filename): array
92+
{
93+
return [
94+
'cache-control' => 'must-revalidate',
95+
'content-description' => 'File Transfer',
96+
'content-disposition' => sprintf('attachment; filename=%s', $filename),
97+
'content-transfer-encoding' => 'Binary',
98+
'content-type' => 'text/csv; charset=utf-8',
99+
'expires' => '0',
100+
'pragma' => 'Public',
101+
];
102+
}
83103
}

0 commit comments

Comments
 (0)