| Conditions | 5 |
| Paths | 5 |
| Total Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 32 | public function generate(array $data): string |
||
| 33 | { |
||
| 34 | $fileHandle = fopen('php://temp', 'w'); |
||
| 35 | |||
| 36 | if (!$fileHandle) { |
||
| 37 | throw new \RuntimeException('Cannot open temp file handle (php://temp)'); |
||
| 38 | } |
||
| 39 | |||
| 40 | if (!is_array($data[0])) { |
||
| 41 | $data = [$data]; |
||
| 42 | } |
||
| 43 | |||
| 44 | $tmpPlaceholder = 'MJASCHEN_COLLMEX_WORKAROUND_PHP_BUG_43225_' . time(); |
||
| 45 | foreach ($data as $line) { |
||
| 46 | // workaround for PHP bug 43225: temporarily insert a placeholder |
||
| 47 | // between a backslash directly followed by a double-quote (for |
||
| 48 | // string field values only) |
||
| 49 | array_walk( |
||
| 50 | $line, |
||
| 51 | function (&$item) use ($tmpPlaceholder): void { |
||
| 52 | if (!is_string($item)) { |
||
| 53 | return; |
||
| 54 | } |
||
| 55 | $item = preg_replace('/(\\\\+)"/m', '$1' . $tmpPlaceholder . '"', $item); |
||
| 56 | } |
||
| 57 | ); |
||
| 58 | |||
| 59 | fputcsv($fileHandle, $line, self::DELIMITER, self::ENCLOSURE); |
||
| 60 | } |
||
| 61 | |||
| 62 | rewind($fileHandle); |
||
| 63 | $csv = stream_get_contents($fileHandle); |
||
| 64 | fclose($fileHandle); |
||
| 65 | |||
| 66 | // remove the temporary placeholder from the final CSV string |
||
| 67 | $csv = str_replace($tmpPlaceholder, '', $csv); |
||
| 68 | |||
| 69 | return $csv; |
||
| 70 | } |
||
| 71 | } |
||
| 72 |