Mwalker has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/90283


Change subject: A Streaming CSV Reader for SmashPig
......................................................................

A Streaming CSV Reader for SmashPig

These classes allow the stream processing of CSV files in a 'nicely'
wrapped way.

These were needed so that I could process Adyen reports.

Change-Id: I6cf6656bac25c557f724ea6b296a3adab0243e52
---
A Core/DataFiles/HeadedCsvReader.php
1 file changed, 46 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/wikimedia/fundraising/SmashPig 
refs/changes/83/90283/1

diff --git a/Core/DataFiles/HeadedCsvReader.php 
b/Core/DataFiles/HeadedCsvReader.php
new file mode 100644
index 0000000..1e84730
--- /dev/null
+++ b/Core/DataFiles/HeadedCsvReader.php
@@ -0,0 +1,46 @@
+<?php namespace SmashPig\Core\DataFiles;
+
+/**
+ * Iteratively reads a CSV file that contains a data header
+ *
+ * @package SmashPig\Core\DataFiles
+ */
+class HeadedCsvReader extends CsvReader {
+       /** @var string[] */
+       protected $colNames;
+
+       public function __construct( $file, $delimiter = ',', $maxRowLength = 
4098 ) {
+               parent::__construct( $file, $delimiter, $maxRowLength );
+
+               // Extract the header information
+               $this->colNames = parent::current();
+               parent::next();
+       }
+
+       /**
+        * Extract the contents of the given column name.
+        *
+        * This is slightly backwards because it did not seem worth the effort
+        * to create a fully functional ArrayObject class to return from 
current()
+        *
+        * @param $colName string Name of the column to extract
+        * @param $row string[] A row returned from current() **FROM THIS FILE**
+        *
+        * @throws DataFileException if the column name does not exist.
+        * @return string Contents of the column
+        */
+       public function extractCol( $colName, &$row ) {
+               $col = array_search( $colName, $this->colNames );
+               if ( $col === false ) {
+                       throw new DataFileException( "Column name 
{$this->colNames} not found!" );
+               }
+               return $row[$col];
+       }
+
+       /**
+        * @return string[] CSV file headers in order of columns
+        */
+       public function headers() {
+               return $this->colNames;
+       }
+}
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/90283
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6cf6656bac25c557f724ea6b296a3adab0243e52
Gerrit-PatchSet: 1
Gerrit-Project: wikimedia/fundraising/SmashPig
Gerrit-Branch: master
Gerrit-Owner: Mwalker <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to