From aaf5ca234cecc45f4068bc98a29c8905affafd01 Mon Sep 17 00:00:00 2001 From: Filippo Tessarotto Date: Sat, 24 Nov 2012 09:42:09 +0100 Subject: [PATCH] Get str_getcsv working on PHP 5.2 --- unitTests/testDataFileIterator.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/unitTests/testDataFileIterator.php b/unitTests/testDataFileIterator.php index 0e6aad8c..9eabe09d 100644 --- a/unitTests/testDataFileIterator.php +++ b/unitTests/testDataFileIterator.php @@ -57,7 +57,7 @@ class testDataFileIterator implements Iterator list($testData) = explode('//',$testDataRow); // Split data into an array of individual values and a result - $dataSet = str_getcsv($testData,',',"'"); + $dataSet = $this->_getcsv($testData, ',', "'"); foreach($dataSet as &$dataValue) { $dataValue = $this->_parseDataValue($dataValue); } @@ -66,6 +66,25 @@ class testDataFileIterator implements Iterator return $dataSet; } + private function _getcsv($input, $delimiter, $enclosure) + { + if (function_exists('str_getcsv')) { + return str_getcsv($input, $delimiter, $enclosure); + } + + $temp = fopen('php://memory', 'rw'); + fwrite($temp, $input); + rewind($temp); + $data = fgetcsv($temp, strlen($input), $delimiter, $enclosure); + fclose($temp); + + if ($data === false) { + $data = array(null); + } + + return $data; + } + private function _parseDataValue($dataValue) { // discard any white space $dataValue = trim($dataValue);