ソースを参照

read tab data

euromark 12 年 前
コミット
c119d7eb92

+ 32 - 0
Lib/Utility/TextLib.php

@@ -17,6 +17,38 @@ class TextLib extends String {
 	}
 	}
 
 
 	/**
 	/**
+	 * Read tab data (tab-separated data).
+	 *
+	 * @return array
+	 */
+	public function readTab() {
+		$pieces = explode("\n", $this->text);
+		$result = array();
+		foreach ($pieces as $piece) {
+			$tmp = explode("\t", trim($piece, "\r\n"));
+			$result[] = $tmp;
+		}
+		return $result;
+	}
+
+	/**
+	 * Read with a specific pattern.
+	 *
+	 * E.g.: '%s,%s,%s'
+	 *
+	 * @param string $pattern
+	 * @return array
+	 */
+	public function readWithPattern($pattern) {
+		$pieces = explode("\n", $this->text);
+		$result = array();
+		foreach ($pieces as $piece) {
+			$result[] = sscanf(trim($piece, "\r\n"), $pattern);
+		}
+		return $result;
+	}
+
+	/**
 	 * Count words in a text.
 	 * Count words in a text.
 	 *
 	 *
 	 * //TODO use str_word_count() instead!!!
 	 * //TODO use str_word_count() instead!!!

+ 25 - 0
Test/Case/Lib/Utility/TextLibTest.php

@@ -14,6 +14,31 @@ class TextLibTest extends CakeTestCase {
 		$this->TextLib = new TextLib();
 		$this->TextLib = new TextLib();
 	}
 	}
 
 
+	public function testReadTab() {
+		$data = <<<TXT
+some	tabbed	data
+and	another	line
+TXT;
+		$this->TextLib = new TextLib($data);
+		$result = $this->TextLib->readTab();
+
+		$this->assertSame(2, count($result));
+		$this->assertSame(array('and', 'another', 'line'), $result[1]);
+	}
+
+	public function testReadWithPattern() {
+		$data = <<<TXT
+some random data
+and another line
+and a   third
+TXT;
+		$this->TextLib = new TextLib($data);
+		$result = $this->TextLib->readWithPattern("%s %s %s");
+
+		$this->assertSame(3, count($result));
+		$this->assertSame(array('and', 'a', 'third'), $result[2]);
+	}
+
 	public function testConvertToOrd() {
 	public function testConvertToOrd() {
 		$this->TextLib = new TextLib('h H');
 		$this->TextLib = new TextLib('h H');
 		$is = $this->TextLib->convertToOrd();
 		$is = $this->TextLib->convertToOrd();

+ 1 - 1
View/Helper/FormExtHelper.php

@@ -738,7 +738,7 @@ class FormExtHelper extends FormHelper {
 		# temp fix
 		# temp fix
 		if (!is_array($options)) {
 		if (!is_array($options)) {
 			if ($options === null) {
 			if ($options === null) {
-				$options = 'DMY';
+				//$options = 'DMY';
 			}
 			}
 			return parent::dateTime($field, $options, $tf, $a);
 			return parent::dateTime($field, $options, $tf, $a);
 		}
 		}