<?php
namespace lib\excel;
/**
 * excelCsv
 * @author HzqGhost admin@whiledo.com QQ:313143468
 * @version 1.0.0
 *
public function excel()
{
    $fp = fopen('php://output','w');
    $data=['a','b','c'];
    $excel = object(\lib\excel\excel::class,'csv');
    $excel->setStream($fp,'test.csv');
    $excel->header();
    $excel->bookBegin();
    $excel->sheetBegin('aaa',$data);
    $excel->append($data);
    $excel->sheetEnd();
    $excel->bookEnd();
}
 */
class excelCsv extends excelBase implements excelInterface
{
	public function __construct()
    {
        
    }

	public function open($type='r'){
        if(is_null($this->fp)){
            if(empty($this->dirname)){
                trigger_error( slg('未定义excel输出目录,请先使用 $excel->setDir($dirname)'), E_USER_ERROR);
            }
            if(empty($this->filename)){
                trigger_error( slg('未定义excel输出文件,请先使用 $excel->setFile($filename)'), E_USER_ERROR);
            }
            $this->fp = fopen($this->dirname.'/'.$this->filename,$type);
        }
        if(!is_resource($this->fp)){
            trigger_error( slg('打开文件出错').$this->dirname.'/'.$this->filename, E_USER_ERROR);
        }
        return $this->fp;
    }

	public function bookBegin():excelInterface
    {
        $this->open('w');
        //UTF8 csv文件头前需添加BOM，不然会是乱码
        fwrite($this->fp, chr(0xEF).chr(0xBB).chr(0xBF));
        return $this;
    }
	public function bookEnd()
    {
        fclose($this->fp);
        $this->fp = null;
        if($this->isOutput){
            if(!empty($this->filename)){
                $filename = $this->dirname.'/'.$this->filename;
                header('Content-Length: '.filesize($filename));
                readfile($filename);
                ob_end_flush();
                unlink($filename);
            }
            exit();
        }
   }

	public function sheetBegin(string $sheetName,array $headerRow=[],array $columnStyle=[]):excelInterface
    {
        if(!empty($headerRow)){
            fputcsv($this->fp, $headerRow);
        }
        return $this;
    }

	public function sheetEnd():excelInterface
    {
        return $this;
    }

	public function append(array $row=[]):excelInterface
    {
        if($row){
            foreach($row as $k=>$cell){
                $row[$k] = $this->specialchars($cell);
            }
            fputcsv($this->fp, $row);
        }
        if($this->isOutput && empty($this->filename)){
            ob_flush();
            flush();
        }
        return $this;
    }

    protected function specialchars(string $value){
        return str_replace(['"'],['\"'],htmlspecialchars($value));
    }

    public function toArray($types=[]):array
    {
        $fp = $this->open('r');
        ini_set('auto_detect_line_endings',TRUE);
        $data = [];
        while ( ($row = fgetcsv($fp) ) !== FALSE ) {
            $data[] = $row;
        }
        ini_set('auto_detect_line_endings',FALSE);
        return $data;
    }
}