-
Notifications
You must be signed in to change notification settings - Fork 14
/
NetteCSVResponse.php
225 lines (184 loc) · 5.18 KB
/
NetteCSVResponse.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php
namespace Nette\Application\Responses;
use Nette;
/**
* CSV download response.
* Under New BSD license.
*
* @package Nette\Application\Responses
*/
class CsvResponse extends Nette\Object implements Nette\Application\IResponse
{
/** standard glues */
const COMMA = ',',
SEMICOLON = ';',
TAB = ' ';
/** @var bool */
protected $addHeading;
/** @var string */
protected $glue = self::COMMA;
/** @var string */
protected $outputCharset = 'utf-8';
/** @var string */
protected $contentType = 'text/csv';
/** @var callable */
protected $headingFormatter = 'self::firstUpperNoUnderscoresFormatter';
/** @var callable */
protected $dataFormatter;
/** @var array */
protected $data;
/** @var string */
protected $filename;
/**
* In accordance with Nette Framework accepts only UTF-8 input. For output @see setOutputCharset().
* @param array[]|\Traversable $data
* @param string $filename
* @param bool $addHeading whether add first row from data array keys (keys are taken from first row)
* @throws \InvalidArgumentException
*/
public function __construct($data, $filename = 'output.csv', $addHeading = TRUE)
{
if ($data instanceof \Traversable) {
$data = iterator_to_array($data);
}
if (!is_array($data)) {
throw new \InvalidArgumentException(__CLASS__.": data must be two dimensional array or instance of Traversable.");
}
$this->data = array_values($data);
$this->filename = $filename;
$this->addHeading = $addHeading;
}
/**
* Value separator.
* @param string $glue
* @return self
* @throws \InvalidArgumentException
*/
public function setGlue($glue)
{
if (empty($glue) || preg_match('/[\n\r"]/s', $glue)) {
throw new \InvalidArgumentException(__CLASS__.": glue cannot be an empty or reserved character.");
}
$this->glue = $glue;
return $this;
}
/**
* @param string $charset
* @return self
*/
public function setOutputCharset($charset)
{
$this->outputCharset = $charset;
return $this;
}
/**
* @param string $contentType
* @return self
*/
public function setContentType($contentType)
{
$this->contentType = $contentType;
return $this;
}
/**
* When heading added, it is formatted by given callback.
* Default @see firstUpperNoUnderscoresFormatter(); erase it by calling setHeadingFormatter(NULL).
* @param callable $formatter
* @return self
* @throws \InvalidArgumentException
*/
public function setHeadingFormatter($formatter)
{
if ($formatter !== NULL && !is_callable($formatter)) {
throw new \InvalidArgumentException(__CLASS__.": heading formatter must be callable.");
}
$this->headingFormatter = $formatter;
return $this;
}
/**
* If given, every value is formatted by given callback.
* @param callable $formatter
* @return self
* @throws \InvalidArgumentException
*/
public function setDataFormatter($formatter)
{
if ($formatter !== NULL && !is_callable($formatter)) {
throw new \InvalidArgumentException(__CLASS__.": data formatter must be callable.");
}
$this->dataFormatter = $formatter;
return $this;
}
/**
* @param string $heading
* @return string
*/
public static function firstUpperNoUnderscoresFormatter($heading)
{
$heading = str_replace("_", ' ', $heading);
$heading = mb_strtoupper(mb_substr($heading, 0, 1)) . mb_substr($heading, 1);
return $heading;
}
/**
* Sends response to output.
* @param Nette\Http\IRequest $httpRequest
* @param Nette\Http\IResponse $httpResponse
*/
public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
{
$httpResponse->setContentType($this->contentType, $this->outputCharset);
$attachment = 'attachment';
if (!empty($this->filename)) {
$attachment .= '; filename="' . $this->filename . '"';
}
$httpResponse->setHeader('Content-Disposition', $attachment);
$data = $this->formatCsv();
$httpResponse->setHeader('Content-Length', strlen($data));
print $data;
}
protected function formatCsv()
{
if (empty($this->data)) {
return '';
}
ob_start();
$buffer = fopen("php://output", 'w');
// if output charset is not UTF-8
$recode = strcasecmp($this->outputCharset, 'utf-8');
foreach ($this->data as $n => $row) {
if ($row instanceof \Traversable) {
$row = iterator_to_array($row);
}
if (!is_array($row)) {
throw new \InvalidArgumentException(__CLASS__.": row $n must be array or instance of Traversable, " . gettype($row) . ' given.');
}
if ($n === 0 && $this->addHeading) {
$labels = array_keys($row);
if ($this->headingFormatter || $recode) {
foreach ($labels as &$label) {
if ($this->headingFormatter) {
$label = call_user_func($this->headingFormatter, $label);
}
if ($recode) {
$label = iconv('utf-8', "$this->outputCharset//TRANSLIT", $label);
}
}
}
fputcsv($buffer, $labels, $this->glue);
}
if ($this->dataFormatter || $recode) {
foreach ($row as &$value) {
if ($this->dataFormatter) {
$value = call_user_func($this->dataFormatter, $value);
}
if ($recode) {
$value = iconv('utf-8', "$this->outputCharset//TRANSLIT", $value);
}
}
}
fputcsv($buffer, $row, $this->glue);
}
fclose($buffer);
return ob_get_clean();
}
}