powered by nequal
Home » CSV_Iterator » Timeline » 1038

Diffs

CSV_Iterator/trunk/tests/src/CSV/CSV_IteratorTest.php

@@ -0,0 +1,116 @@
+<?php
+ini_set("include_path", "../src/CSV".PATH_SEPARATOR."../../../src/CSV".PATH_SEPARATOR.ini_get("include_path"));
+require_once 'PHPUnit/Framework.php';
+
+require_once 'Iterator.php';
+
+/**
+ * Test class for CSV_Iterator.
+ * Generated by PHPUnit on 2009-07-31 at 18:25:12.
+ */
+class CSV_IteratorTest extends PHPUnit_Framework_TestCase
+{
+/**
+ * Tears down the fixture, for example, closes a network connection.
+ * This method is called after a test is executed.
+ *
+ * @access protected
+ */
+    protected function tearDown()
+    {
+    }
+
+    /**
+     * @todo Implement testRewind().
+     */
+    public function testRewind() {
+    // Remove the following lines when you implement this test.
+        $this->markTestIncomplete(
+            'This test has not been implemented yet.'
+        );
+    }
+
+    /**
+     * @todo Implement testNext().
+     */
+    public function testNext() {
+    // Remove the following lines when you implement this test.
+        $this->markTestIncomplete(
+            'This test has not been implemented yet.'
+        );
+    }
+
+    /**
+     * @todo Implement testCurrent().
+     */
+    public function testCurrent() {
+    // Remove the following lines when you implement this test.
+        $this->markTestIncomplete(
+            'This test has not been implemented yet.'
+        );
+    }
+
+    /**
+     * @todo Implement testKey().
+     */
+    public function testKey() {
+    // Remove the following lines when you implement this test.
+        $this->markTestIncomplete(
+            'This test has not been implemented yet.'
+        );
+    }
+
+    /**
+     * @todo Implement testValid().
+     */
+    public function testValid() {
+    // Remove the following lines when you implement this test.
+        $this->markTestIncomplete(
+            'This test has not been implemented yet.'
+        );
+    }
+
+    /**
+     * @todo Implement testSetRowLength().
+     */
+    public function testSetRowLength() {
+    // Remove the following lines when you implement this test.
+        $this->markTestIncomplete(
+            'This test has not been implemented yet.'
+        );
+    }
+
+    /**
+     * @dataProvider foreachData
+     */
+    public function testForeach($file, $encoding, $expected)
+    {
+        reset($expected);
+        foreach (new CSV_Iterator(realpath(dirname(__FILE__) . '/../../fixtures/' . $file), $encoding) AS $key=>$row) {
+            $this->assertEquals(current($expected), $row, 'at ' . $key);
+            next($expected);
+        }
+    }
+
+    public function foreachData()
+    {
+        return array(
+            array('withHeader.csv', 'utf-8', array(
+                    array('header1'=>'1-1', 'header 2'=>'1-2', 'header3'=>'1-3'),
+                    array('header1'=>'2 1', 'header 2'=>'2 2', 'header3'=>'2 3'),
+                    array('header1'=>'3
+1', 'header 2'=>'
+3
+
+2
+', 'header3'=>'
+3 3'),
+                )),
+            array('withHeader.ja.utf-8.csv', 'utf-8', array(
+                    array("あいうえお"=>'一の一',"漢字ヘッダ"=>'一の弐',"漢字 ヘッダ"=>'壱の参'),
+                    array("あいうえお"=>'弐 壱',"漢字ヘッダ"=>'弐 弐',"漢字 ヘッダ"=>'弐 参'),
+                ))
+        );
+    }
+}
+?>
属性に変更があったパス: CSV_Iterator/trunk/tests/src/CSV/CSV_IteratorTest.php
___________________________________________________________________
名前: svn:keywords
+ id
名前: svn:eol-style
+ native

CSV_Iterator/trunk/tests/fixtures/withHeader.ja.shift_jis.csv

@@ -0,0 +1,3 @@
+"",wb_,"@wb_"
+̈,"̓","̎Q"
+" "," "," Q"

CSV_Iterator/trunk/tests/fixtures/withHeader.ja.utf-8.csv

@@ -0,0 +1,3 @@
+"あいうえお",漢字ヘッダ,"漢字 ヘッダ"
+一の一,"一の弐","壱の参"
+"弐 壱","弐 弐","弐 参"

CSV_Iterator/trunk/tests/fixtures/withHeader.csv

@@ -0,0 +1,10 @@
+header1,"header 2",header3
+1-1,1-2,1-3
+"2 1","2 2","2 3"
+"3
+1","
+3
+
+2
+","
+3 3"

CSV_Iterator/trunk/src/CSV/Iterator.php

@@ -0,0 +1,118 @@
+<?php
+class CSV_Iterator implements Iterator
+{
+    private $filePointer;
+    private $delimiter;
+    private $enclosure;
+    private $encoding;
+    private $rowCounter;
+    private $currentRow;
+    private $rowLength;
+    private $header;
+    private $offset;
+
+    /**
+     * construct
+     *
+     * @param resource $file
+     * @param string $encoding
+     * @param string $delimiter
+     * @param string $enclosure
+     * @param array $header
+     * @param integer $rowlength
+     */
+    public function __construct($file, $encoding = 'utf-8', $delimiter = ',', $enclosure = '"', array $header = array(), $rowlength = null)
+    {
+        $this->filePointer = fopen($file, 'r');
+        $this->delimiter = $delimiter;
+        $this->enclosure = $enclosure;
+        $this->encoding = $encoding;
+        $this->setRowLength($rowlength);
+
+        $this->header = empty($header) ? $this->readRow() : $header;
+        $this->offset = ftell($this->filePointer);
+        $this->next();
+        $this->rowCounter = 0;
+    }
+
+    public function rewind()
+    {
+        fseek($this->filePointer, $this->offset);
+        $this->next();
+        $this->rowCounter = 0;
+    }
+
+    public function next()
+    {
+        $data = $this->readRow();
+        $this->currentRow = $data ? array_combine($this->header, $data) : null;
+        ++$this->rowCounter;
+    }
+
+    public function current()
+    {
+        return $this->currentRow;
+    }
+
+    public function key()
+    {
+        return $this->rowCounter;
+    }
+
+    public function valid()
+    {
+        return is_array($this->currentRow);
+    }
+
+    /**
+     * read 1 row of csv.
+     *
+     * this is a port of the original code written by yossy.
+     *
+     * @author yossy
+     * @author MugeSo
+     *
+     * @see http://yossy.iimp.jp/wp/?p=56
+     * @return array
+     */
+    private function readRow()
+    {
+        $d = preg_quote($this->delimiter);
+        $e = preg_quote($this->enclosure);
+        $line = "";
+        $arg = $this->rowLength ? array($this->filePointer, $this->rowLength) : array($this->filePointer);
+
+        // 囲い込み記号内で改行できるようにするための処理
+        // また、マルチバイト関係で安全に処理するために、文字エンコーディングを一旦UTF-8にする
+        while (!feof($this->filePointer)) {
+            $line .= mb_convert_encoding(call_user_func_array('fgets', $arg), 'utf-8', $this->encoding);
+            $itemcnt = preg_match_all('/'.$e.'/u', $line, $dummy);
+            if ($itemcnt % 2 == 0) break;
+        }
+
+        $csv_line = preg_replace('/(?:\r\n|[\r\n])?$/u', $d, trim($line));
+        $csv_pattern = '/('.$e.'[^'.$e.']*(?:'.$e.$e.'[^'.$e.']*)*'.$e.'|[^'.$d.']*)'.$d.'/u';
+        preg_match_all($csv_pattern, $csv_line, $csv_matches);
+        $csv_data = $csv_matches[1];
+
+        foreach($csv_data AS &$column){
+            $column = mb_convert_encoding(str_replace($e.$e, $e, preg_replace('/^'.$e.'(.*)'.$e.'$/us','$1',$column)), $this->encoding, 'utf-8');
+        }
+
+        return empty($line) ? null : $csv_data;
+    }
+
+    /**
+     * set row length.
+     *
+     * don't use usaly.
+     *
+     * @param <type> $length
+     */
+    public function setRowLength($length)
+    {
+        if(!is_int($length) && $length!==null) throw new UnexpectedValueException('argument #1 should be integer or null.');
+        $this->rowLength = $length;
+    }
+}
+?>
属性に変更があったパス: CSV_Iterator/trunk/src/CSV/Iterator.php
___________________________________________________________________
名前: svn:keywords
+ id
名前: svn:eol-style
+ native
属性に変更があったパス: CSV_Iterator/trunk
___________________________________________________________________
名前: svn:ignore
+ nbproject