<?php
namespace lib\excel;
/**
 * excelXml
 * @author HzqGhost admin@whiledo.com QQ:313143468
 * @version 1.2.0
 *
public function excel()
{
    $fp = fopen('php://output','w');
    $data=['a','b','c'];
    $excel = object(\lib\excel\excel::class,'xml');
    $excel->setStream($fp,'test.xlsx');
    $excel->header();
    $excel->bookBegin();
    $excel->sheetBegin('aaa',$data);
    $excel->append($data);
    $excel->sheetEnd();
    $excel->bookEnd();
}
 */
class excelXml extends excelBase implements excelInterface
{
	public function __construct()
    {
        $this->br = "\r\n";
    }

	public function bookBegin():excelInterface
    {
        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,'w');
        }
        fwrite($this->fp, '<?xml version="1.0"?>'.$this->br);
        fwrite($this->fp, '<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet">'.$this->br);
        fwrite($this->fp, '<ss:Styles>'.$this->br);
        fwrite($this->fp,   '<ss:Style ss:ID="s1">'.$this->br);
        fwrite($this->fp,       '<ss:Font ss:Color="#FFFFFF" ss:Bold="1"/>'.$this->br);
        fwrite($this->fp,       '<ss:Interior ss:Color="#002F5F" ss:Pattern="Solid"/>'.$this->br);
        fwrite($this->fp,   '</ss:Style>'.$this->br);
        fwrite($this->fp, '</ss:Styles>'.$this->br);
        return $this;
    }
	public function bookEnd()
    {
        fwrite($this->fp, '</ss:Workbook>');
        fclose($this->fp);
        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
    {
        fwrite($this->fp, '<ss:Worksheet ss:Name="'.$sheetName.'">'.$this->br);
        fwrite($this->fp, '<ss:Table>'.$this->br);
        if(!empty($columnStyle)){
            foreach($columnStyle as $item){
                fwrite($this->fp, '<ss:Column ss:Width="'.$item['width'].'"/>'.$this->br);
            }
        }else{
            foreach($headerRow as $cell){
                fwrite($this->fp, '<ss:Column ss:Width="125"/>'.$this->br);
            }
        }
        if(!empty($headerRow)){
            fwrite($this->fp, '<ss:Row ss:StyleID="s1">'.$this->br);
            foreach($headerRow as $cell){
                fwrite($this->fp, '<ss:Cell><ss:Data ss:Type="String">'.$cell.'</ss:Data></ss:Cell>'.$this->br);
            }
            fwrite($this->fp, '</ss:Row>'.$this->br);
        }
        return $this;
    }

	public function sheetEnd():excelInterface
    {
        fwrite($this->fp, '</ss:Table>'.$this->br);
        fwrite($this->fp, '</ss:Worksheet>'.$this->br);
        return $this;
    }

	public function append(array $row=[]):excelInterface
    {
        fwrite($this->fp, '<ss:Row>'.$this->br);
        if($row){
            foreach($row as $cell){
                fwrite($this->fp, '<ss:Cell><ss:Data ss:Type="String">'.$this->specialchars($cell).'</ss:Data></ss:Cell>'.$this->br);
            }
        }
        fwrite($this->fp, '</ss:Row>'.$this->br);
        if($this->isOutput && empty($this->filename)){
            ob_flush();
            flush();
        }
        return $this;
    }

    protected function specialchars($value){
        if(is_null($value)) $value = '';
        return str_replace('&amp;','&',htmlspecialchars($value));
    }
}