From 4aef5be253706626478d4e0212a855b3b371e58c Mon Sep 17 00:00:00 2001
From: Alex Barth <alex_b@53995.no-reply.drupal.org>
Date: Thu, 16 Sep 2010 19:03:27 +0000
Subject: [PATCH] #913672 andrewlevine: Break out CSV Parser into submethods so
 it is more easily overridable.

---
 CHANGELOG.txt              |  2 ++
 plugins/FeedsCSVParser.inc | 40 ++++++++++++++++++++++++++++++++------
 2 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/CHANGELOG.txt b/CHANGELOG.txt
index c14ff44e..1926c9c6 100644
--- a/CHANGELOG.txt
+++ b/CHANGELOG.txt
@@ -3,6 +3,8 @@
 Feeds 6.x xxxxxxxxxxxxxxxxxxxxxx
 --------------------------------
 
+- #913672 andrewlevine: Break out CSV Parser into submethods so it is more
+  easily overridable
 - #853974 snoldak924, alex_b: Fix XSS vulnerabilities in module.
 - #887846 ekes: Make FeedsSimplePieEnclosure (un)serialization safe.
 - #908582 XiaN Vizjereij, alex_b: Fix "Cannot use object of type stdClass as
diff --git a/plugins/FeedsCSVParser.inc b/plugins/FeedsCSVParser.inc
index 7780d701..ab3961bc 100644
--- a/plugins/FeedsCSVParser.inc
+++ b/plugins/FeedsCSVParser.inc
@@ -19,25 +19,53 @@ class FeedsCSVParser extends FeedsParser {
     $delimiter = $source_config['delimiter'] == 'TAB' ? "\t" : $source_config['delimiter'];
     $parser->setDelimiter($delimiter);
 
-    // Get first line and use it for column names, convert them to lower case.
+    $header = $this->parseHeader($parser, $iterator);
+    if (!header) {
+      return;
+    }
+    $parser->setColumnNames($header);
+
+    // Populate batch.
+    $batch->setItems($this->parseItems($parser, $iterator));
+  }
+
+  /**
+   * Get first line and use it for column names, convert them to lower case.
+   * Be aware that the $parser and iterator objects can be modified in this
+   * function since they are passed in by reference
+   * 
+   * @param ParserCSV $parser
+   * @param ParserCSVIterator $iterator
+   * @return
+   *   An array of lower-cased column names to use as keys for the parsed items.
+   */
+  protected function parseHeader(ParserCSV $parser, ParserCSVIterator $iterator) {
     $parser->setLineLimit(1);
     $rows = $parser->parse($iterator);
     if (!count($rows)) {
-      return;
+      return FALSE;
     }
     $header = array_shift($rows);
     foreach ($header as $i => $title) {
       $header[$i] = trim(drupal_strtolower($title));
     }
-    $parser->setColumnNames($header);
+    return $header;
+  }
 
+  /**
+   * Parse all of the items from the CSV.
+   *
+   * @param ParserCSV $parser
+   * @param ParserCSVIterator $iterator
+   * @return
+   *   An array of rows of the CSV keyed by the column names previously set
+   */
+  protected function parseItems(ParserCSV $parser, ParserCSVIterator $iterator) {
     // Set line limit to 0 and start byte to last position and parse rest.
     $parser->setLineLimit(0);
     $parser->setStartByte($parser->lastLinePos());
     $rows = $parser->parse($iterator);
-
-    // Populate batch.
-    $batch->setItems($rows);
+    return $rows;
   }
 
   /**
-- 
GitLab