Conditions | 4 |
Paths | 5 |
Total Lines | 26 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
21 | public function getCsvData(array $data, bool $addHeader = true) |
||
22 | { |
||
23 | $csvData = null; |
||
24 | |||
25 | if (empty($data)) { |
||
26 | return false; |
||
27 | } |
||
28 | |||
29 | // temporary memory wrapper; if bigger than 5MB will be written to temp file. |
||
30 | $handle = fopen('php://temp/maxmemory: ' . (5*1024*1024), 'r+'); |
||
31 | |||
32 | if ($addHeader) { |
||
33 | fputcsv($handle, array_keys(current($data)), $this->delimiter, $this->enclosure); |
||
34 | } |
||
35 | |||
36 | foreach ($data as $item) { |
||
37 | fputcsv($handle, $item, $this->delimiter, $this->enclosure); |
||
38 | } |
||
39 | |||
40 | rewind($handle); |
||
41 | |||
42 | $csvData = stream_get_contents($handle); |
||
43 | |||
44 | fclose($handle); |
||
45 | |||
46 | return $csvData; |
||
47 | } |
||
49 |