source: branches/feature-module-paygent/data/downloads/module/mdl_paygent/jp/co/ks/merchanttool/connectmodule/util/CSVWriter.php @ 15162

Revision 15162, 5.0 KB checked in by naka, 17 years ago (diff)

ペイジェント決済モジュール

Line 
1<?php
2/**
3 * PAYGENT B2B MODULE
4 * CSVWriter.php
5 *
6 * Copyright (C) 2007 by PAYGENT Co., Ltd.
7 * All rights reserved.
8 */
9
10/**
11 * CSVWriter CSVŒ`Ž®‚Ńtƒ@ƒCƒ‹‚ðo—Í‚·‚éB
12 * Žg—p•û–@F<br />
13 * <pre><code>
14 * // writerƒIƒuƒWƒFƒNƒg‚ðì‚éBƒfƒtƒHƒ‹ƒg‚ÍShift_JISƒGƒ“ƒR[ƒh
15 * CSVWriter writer = null;
16 * try {
17 *     writer = new CSVWriter(
18 *         "c:\\temp\\test.txt", CSVWriter.ENCODING_SJIS);
19 *     writer.open();
20 *     List list = new ArrayList();
21 *     list.add("1");
22 *     list.add("abc");
23 *     list.add("");
24 *     list.add(",");
25 *     list.add("‚É‚Ù‚ñ‚²");
26 *     writer.writeOneLine(list);
27 *     list.remove(0);
28 *     list.add(0, "2");
29 *     writer.writeOneLine(list);
30 * } finally {
31 *     writer.close();
32 * }
33 * </code></pre>
34 * @version $Revision: 1.4 $
35 * @author $Author: t-mori $
36 */
37
38    /** ƒtƒ@ƒCƒ‹o—Í—pEncoding Shift_JIS */
39    define("CSVWriter__ENCODING_SJIS", "Shift_JIS");
40    /** ƒtƒ@ƒCƒ‹o—Í—pEncoding EUC-JP */
41    define("CSVWriter__ENCODING_EUC", "EUC_JP");
42    /** ƒtƒ@ƒCƒ‹o—Í—pEncoding MS932 */
43    define("CSVWriter__ENCODING_MS932", "SJIS-win");    //"Windows-31J";
44        /** ƒtƒ@ƒCƒ‹o—ÍŽž‚̉üsƒR[ƒh \r\n */
45    define("CSVWriter__WINDOWS_NEWLINE", "\r\n");
46        /** ƒtƒ@ƒCƒ‹o—ÍŽž‚̉üsƒR[ƒh \n */
47    define("CSVWriter__UNIX_NEWLINE", "\n");
48        /** ƒtƒ@ƒCƒ‹o—ÍŽž‚̉üsƒR[ƒh \r */
49    define("CSVWriter__MAC_NEWLINE", "\r");
50
51class CSVWriter {
52
53    var $csvFile;
54    var $filePath;
55    var $encoding;
56    var $envelop;
57    var $newLine = CSVWriter__WINDOWS_NEWLINE;
58
59    /**
60     * ƒRƒ“ƒXƒgƒ‰ƒNƒ^BƒGƒ“ƒR[ƒh‹y‚э€–Úƒf[ƒ^ˆÍ‚Ý•¶Žš‚ÌŽw’è‚ðs‚¢Writer‚ðì¬‚·‚éB
61     * @param filePath ƒtƒ@ƒCƒ‹ƒpƒX
62     * @param encoding ƒtƒ@ƒCƒ‹‚̃Gƒ“ƒR[ƒh
63     * @param envelop €–Úƒf[ƒ^ˆÍ‚Ý•¶Žš
64     */
65    function CSVWriter($filePath, $encoding = CSVWriter__ENCODING_MS932, $envelop = CSVTokenizer__DEF_ITEM_ENVELOPE) {
66        $this->filePath = $filePath;
67        $this->encoding = $encoding;
68        $this->envelop = $envelop;
69    }
70
71    /**
72     * o—̓tƒ@ƒCƒ‹‚ðŠJ‚­B
73     * ƒtƒ@ƒCƒ‹o—Í‚ª‰Â”\‚ȏó‘Ô‚É‚·‚éB
74      * @return boolean TRUE:¬Œ÷AFALSEFŽ¸”s
75     */
76    function open() {
77
78        $this->csvFile = fopen($this->filePath, "w");
79        if ($this->csvFile == false) {
80            $this->csvFile = null;
81            trigger_error("cannot open file " . $this->filePath . " to write", E_USER_NOTICE);
82            return false;           
83        }
84       
85        // ƒ`ƒFƒbƒNƒGƒ“ƒR[ƒfƒBƒ“ƒO
86        if (mb_convert_encoding("ƒGƒ“ƒR[ƒh", $this->encoding) === false){
87            trigger_error("Unsupported Encoding " . $this->encoding . ".", E_USER_NOTICE);
88            return false;           
89        }
90        return true;
91    }
92
93    /**
94     * o—̓tƒ@ƒCƒ‹‚ð•Â‚¶‚éB
95     * Ä“xƒtƒ@ƒCƒ‹‚ðì¬‚·‚éê‡‚ÍOpen‚©‚çs‚¤‚±‚ƁB
96     */
97    function close() {
98        if ($this->csvFile != null) {
99            fclose($this->csvFile);
100            $this->csvFile = null;
101        }
102    }
103
104    /**
105     * ‰üsƒR[ƒh‚ðÝ’è‚·‚éB–¢Ý’è‚̏ꍇA\n‚ŏo—Í‚·‚éB
106     * @param newLine ‰üsƒR[ƒh‚Ì•¶Žš—ñ
107     */
108    function setNewLine($newLine) {
109        $this->newLine = $newLine;
110    }
111
112    /**
113     * ƒtƒ@ƒCƒ‹‚ðˆês•ª‘‚«ž‚ށB––”ö‚ɉüsƒR[ƒh‚ð’ljÁ‚·‚éB
114     * List‚̏ꍇAList‚Ì’†g‚ðCSVŒ`Ž®‚̈ês‚É•ÏŠ·‚µAo—Í‚ðs‚¤B
115     * @param line ˆês•ª‚Ì•¶Žš—ñ(String)ˆ½‚¢‚Í”z—ñ(array)
116     * @return ‘‚«ž‚ß‚½‚çtrueB
117     */
118    function writeOneLine($line) {
119
120        if (is_string($line)) {
121            if ($this->csvFile == null) {
122                trigger_error("File not open.", E_USER_NOTICE);
123                return false;
124            }
125            $encLine = $line;
126       
127            if (fwrite($this->csvFile, $line) === false) {
128                trigger_error("File can not write.", E_USER_NOTICE);
129                return false;
130            }
131            fwrite($this->csvFile, $this->newLine);
132            flush($this->csvFile);
133            return true;
134        }
135        else if (is_array($line)) {
136            $strLine = "";
137   
138            // List to CSVString
139            $bFirstLine = true;
140            foreach($line as $i => $data) {
141                if ($bFirstLine) {
142                    $bFirstLine = false;
143                } else {
144                    $strLine .= ",";
145                }
146   
147                if ($this->envelop != CSVTokenizer__NO_ITEM_ENVELOPE) {
148                    $strLine .= $this->envelop;
149                }
150                $strLine .= $this->cnvKnmString($data);
151                if ($this->envelop != CSVTokenizer__NO_ITEM_ENVELOPE) {
152                    $strLine .= $this->envelop;
153                }
154            }
155   
156            return $this->writeOneLine($strLine);
157        }
158    }
159
160    /**
161     * •¶Žš—ñ’†‚ɃJƒ“ƒ}‚ª‘¶Ý‚·‚éê‡‚Í""‚ň͂ށB
162     * •¶Žš—ñ’†‚Ƀ_ƒuƒ‹ƒNƒH[ƒe[ƒVƒ‡ƒ“‚ª‘¶Ý‚·‚éê‡‚̓_ƒuƒ‹ƒNƒH[ƒe[ƒVƒ‡ƒ“‚ŃGƒXƒP[ƒv‚µA
163     * ƒ_ƒuƒ‹ƒNƒH[ƒe[ƒVƒ‡ƒ“‚ň͂ށB
164     * @param str •ÏŠ·‘Ώە¶Žš—ñ
165     * @return •ÏŠ·Œ‹‰Ê•¶Žš—ñ
166     */
167    function cnvKnmString($str) {
168        if ($str == null) {
169            return null;
170        }
171        $flg = false;
172        $buf = "";
173        for ($i = 0; $i < strlen($str); $i++) {
174            if ($str{$i} == $this->envelop) {
175                $buf .= $this->envelop;
176                $flg = true;
177            }
178            if ($str{$i} == CSVTokenizer__DEF_SEPARATOR) {
179                $flg = true;
180            }
181            $buf .= $str{$i};
182        }
183        return $buf;
184    }
185}
186
187?>
Note: See TracBrowser for help on using the repository browser.