<?php
namespace App\Utils\CSV\Writer; class BufferedWriter extends Writer { protected $headerRows = []; protected $rows = []; public function fromArray($rows) { $this->rows = $rows; return $this; } public function appendRow($row) { $this->rows[] = $row; return $this; } public function appendHeaderRow($row) { $this->headerRows[] = $row; return $this; } public function resetHeader() { $this->headerRows = []; return $this; } public function save($file) { $fp = fopen($file, "w"); foreach ($this->headerRows as $row) { $this->fputcsv($fp, $row, $this->options['delimiter'], $this->options['enclosure'], $this->options['escape_char']); } foreach ($this->rows as $row) { $this->fputcsv($fp, $row, $this->options['delimiter'], $this->options['enclosure'], $this->options['escape_char']); } fclose($fp); } public function saveAsStr() { $memoryStream = fopen('php://temp', 'r+'); foreach ($this->headerRows as $row) { $this->fputcsv($memoryStream, $row, $this->options['delimiter'], $this->options['enclosure'], $this->options['escape_char']); } foreach ($this->rows as $row) { $this->fputcsv($memoryStream, $row, $this->options['delimiter'], $this->options['enclosure'], $this->options['escape_char']); } rewind($memoryStream); $content = stream_get_contents($memoryStream); fclose($memoryStream); return $content; } } 