CsvFormat.inc 3.79 KB
<?php
/**
 * CsvFormat
 * データをCSVとして出力する時のクラスです。
 * $Id: CsvFormat.inc,v 1.1 2015/10/08 11:18:56 wanggb Exp $
 * @author iimuro
 * @access public
 * @package jp.aimslib2.io
 */

/* 
 * CSV出力の基本処理
 * ,を含む値→""で囲む
 * "を含む値→""に変換
 */
class CsvFormat{
	
	
	/**
	 * @var array 出力するデータがはいった、2次元配列。
	 */
	public $data = null;
	public $file_path = null;
	public $file_name = null;
	public $delimiter = ",";
	
	/**
	 * @public boolean SJISへの文字コード変換を行うか?trueの場合、行う。
	 */
	public $convert_encoding = true;
	
	/**
	 * @var String 出力時の文字コード。デフォルトで、Shift_JIS。
	 */
	public $output_encoding = "Shift_JIS";
	
	/**
	 * @var String 入力されたデータの文字コード。autoの場合、自動判別を行う。
	 */
	public $data_encoding = "auto";
	
	/**
	 * データをCSV形式で出力したり、保存したりしたいときに利用するクラスです。<br />
	 * 2次元配列でデータをセットします。<br />
	 */
	public function __construct($data = null) {
		if ($data != null) {
			if (is_array($data)) {
				$this->data = $data;
			}
		}
	}
	
	
	/**
	 * 出力時のファイル名をセットします。
	 */
	public function setFileName($name) {
		$this->file_name = $name;
	}
	
	
	/**
	 * ファイルとして保存する場合のディレクトリをセットします。
	 */
	public function setFilePath($path) {
		$this->file_path = $path;
	}
	
	
	/**
	 * 出力時に文字コード変換を行うかを設定します。
	 * @param boolean $convert 文字コード変換を行うか?true:する。
	 * @param String $output_encoding 出力するときの文字コード。
	 * @param String $data_encoding 入力されたデータの文字コード。autoを指定した場合、自動判別。
	 */
	public function setEncodingConvert($convert, $output_encoding = null, $data_encoding = null) {
		
		$this->convert_encoding = $convert;
		
		if ($output_encoding != null) {
			$this->output_encoding = $output_encoding;
		}
		if ($data_encoding != null) {
			$this->data_encoding = $data_encoding;
		}
		
		
	}
	
	/**
	 * 文字列データとして返します。
	 */
	public function toString() {
		$result = "";
		foreach ($this->data as $row) {
			$row_data = "";
			foreach ($row as $cell) {
				if ($row_data != "") {
					$row_data .= $this->delimiter;
				}
				$row_data .= CsvFormat::doCsvEscape($cell);
			}
			$result .= trim($row_data) . "\n";
		}
		
		
		// 文字コード指定がある場合は変換してやる。
		if ($this->convert_encoding) {
			
			if ($this->data_encoding == "auto") {
				$result = mb_convert_encoding($result, $this->output_encoding);
			} else {
				$result = mb_convert_encoding($result, $this->output_encoding, $this->data_encoding);
			}
		}
		
		return $result;
		
	}
	
	
	/**
	 * HTML出力します。
	 */
	public function printOut() {
		if ($this->file_name != null) {
			header("Pragma: cache");
			header("Content-type: application/octet-stream; name=" . $this->file_name);
			header("Content-disposition: attachment; filename=" . $this->file_name);
		} else {
			header("Content-type: application/octet-stream;");
		}
		header("Content-Transfer-Encoding: binary");
		
		print $this->toString();
	}
	
	
	/**
	 * データをCSV出力用にエスケープします。
	 * ,を含む値→""で囲む
	 * "を含む値→""に変換
	 */
	public function doCsvEscape($str) {
		if (strpos($str, "\"") !== false) {
			$str = str_replace("\"", "\"\"", $str); 
		}
		$do_escape = false;
		if (strpos($str, ",") !== false) {
			$do_escape = true;
		}
		if (strpos($str, "\n") !== false) {
			$do_escape = true;
		}
		
		if ($do_escape) {
			$str = "\"" . $str . "\""; 
		}
		
		return $str;
	}
}

?>