Skip to content

Instantly share code, notes, and snippets.

@thekid
Created February 10, 2012 11:41
Show Gist options
  • Select an option

  • Save thekid/1788956 to your computer and use it in GitHub Desktop.

Select an option

Save thekid/1788956 to your computer and use it in GitHub Desktop.
XP Framework: Patch for Issue #123
diff --git a/core/src/main/php/text/csv/CsvWriter.class.php b/core/src/main/php/text/csv/CsvWriter.class.php
index 811b02d..70c396e 100644
--- a/core/src/main/php/text/csv/CsvWriter.class.php
+++ b/core/src/main/php/text/csv/CsvWriter.class.php
@@ -60,15 +60,16 @@
*/
protected function writeValues($values, $raw= FALSE) {
$line= '';
- foreach ($values as $v => $value) {
- if (!$raw && isset($this->processors[$v])) {
+ $i= 0;
+ foreach ($values as $value) {
+ if (!$raw && isset($this->processors[$i])) {
try {
- $value= $this->processors[$v]->process($value);
+ $value= $this->processors[$i]->process($value);
} catch (Throwable $e) {
$this->raise($e->getMessage());
}
}
-
+ $i++;
$line.= $this->format->format((string)$value);
}
$this->line++;
diff --git a/core/src/test/php/net/xp_framework/unittest/text/csv/CsvListWriterTest.class.php b/core/src/test/php/net/xp_framework
index 859e8f1..749bc19 100644
--- a/core/src/test/php/net/xp_framework/unittest/text/csv/CsvListWriterTest.class.php
+++ b/core/src/test/php/net/xp_framework/unittest/text/csv/CsvListWriterTest.class.php
@@ -7,6 +7,7 @@
uses(
'unittest.TestCase',
'text.csv.CsvListWriter',
+ 'text.csv.processors.FormatDate',
'io.streams.MemoryOutputStream'
);
@@ -151,5 +152,27 @@
$this->newWriter(create(new CsvFormat())->withQuote("'"))->write(array("''", 'Karlsruhe', '76137'));
$this->assertEquals("'''''';Karlsruhe;76137\n", $this->out->getBytes());
}
+
+ /**
+ * Test writing a single line
+ *
+ */
+ #[@test]
+ public function writeLineFromMap() {
+ $this->newWriter()->write(array('name' => 'Timm', 'city' => 'Karlsruhe', 'zip' => '76137'));
+ $this->assertEquals("Timm;Karlsruhe;76137\n", $this->out->getBytes());
+ }
+
+ /**
+ * Test writing a single line
+ *
+ */
+ #[@test]
+ public function writeLineFromMapWithProcessor() {
+ $writer= $this->newWriter();
+ $writer->setProcessor(1, new FormatDate('d.m.Y'));
+ $writer->write(array('id' => 1, 'date' => new Date('2012-02-10')));
+ $this->assertEquals("1;10.02.2012\n", $this->out->getBytes());
+ }
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment