| Conditions | 4 |
| Paths | 22 |
| Total Lines | 23 |
| Code Lines | 12 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 26 | public function getCsvData(array $data, bool $addHeader = true) |
||
| 27 | { |
||
| 28 | try { |
||
| 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 | } catch (\Exception $e) { |
||
| 48 | throw new ApplicationException($e->getMessage()); |
||
| 49 | } |
||
| 52 |