Skip to content

Instantly share code, notes, and snippets.

@rafa-acioly
Last active April 13, 2016 17:12
Show Gist options
  • Save rafa-acioly/759342dd663e6d6f2f8f to your computer and use it in GitHub Desktop.
Save rafa-acioly/759342dd663e6d6f2f8f to your computer and use it in GitHub Desktop.
<?php
/**
* Gera arquivos com dados para download
* @author Rafael Acioly
*/
class FileGenerator {
private static $controller;
private $file;
private $data = array();
private function __construct() {}
public static function getInstance() {
if (!isset(self::$controller)) {
$c = __CLASS__;
self::$controller = new $c;
}
return self::$controller;
}
/**
* Define o nome do arquivo
* @param String $filename
* @return NULL
*/
public function setNewFile($fileName) {
$this->file = new SplFileObject($fileName, 'w');
}
/**
* Define as colunas/indices do arquivo
* @param Array $columns
* @param String $delimiter
*/
public function setColumns(Array $columns, $delimiter = ";") {
if (!empty($delimiter) || count($delimiter) == 1) {
$this->file->fputcsv($columns, $delimiter);
} else {
Log::setLog("Ocorreu um erro ao definir as colunas do arquivo: ". $error->getMessage(), Log::ERROR);
return FALSE;
}
return TRUE;
}
/**
* Verifica se o arquivo esta vazio
* @param SplFileObject $file
* @return Bool
*/
private function validateFile($file) {
if (empty($file) or sizeof($file) == 0) {
return FALSE;
}
return TRUE;
}
/**
* Salva os dados em um array para serem inseridos no arquivo
* @param Array $data
* @param String $delimiter
*/
public function insertData(array $data, $delimiter = ';') {
if ($this->validateFile($data) || !empty($delimiter) || count($delimiter) == 1) {
array_push($this->data, $data);
} else {
Log::setLog("Ocorreu um problema ao tentar inserir os dados no arquivo: ". $error->getMessage(), Log::ERROR);
return FALSE;
}
return TRUE;
}
/**
* Realiza o download do arquivo
* @return String
*/
public function makeDownload() {
if (!$this->validateDownload($this->file)) {
Log::setLog("Ocorreu um problema ao tentar efetuar o download do arquivo: ". $error->getMessage(), Log::ERROR);
return FALSE;
}
header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename="'.$this->file->getFilename().'"');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($this->file->getFilename()));
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Expires: 0');
readfile($this->file->getFilename());
unlink($this->file->getFilename());
return TRUE;
}
/**
*
* @param SplFileObject $file
* @return bool
*/
private function validateDownload($file) {
if (!empty($file) and file_exists($file->getPathname()) and sizeof($file) >= 1) {
return TRUE;
}
return FALSE;
}
/**
* Salva os dados no arquivo para realizar o download
* @param String $delimiter
*/
public function saveFile($delimiter = ";") {
foreach($this->data as $key) {
$this->file->fputcsv($key, $delimiter);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment