| 1 |
<?php |
| 2 |
|
| 3 |
class testDataFileIterator implements Iterator |
| 4 |
{ |
| 5 |
|
| 6 |
protected $file; |
| 7 |
protected $key = 0; |
| 8 |
protected $current; |
| 9 |
|
| 10 |
public function __construct($file) |
| 11 |
{ |
| 12 |
$this->file = fopen($file, 'r'); |
| 13 |
} |
| 14 |
|
| 15 |
public function __destruct() |
| 16 |
{ |
| 17 |
fclose($this->file); |
| 18 |
} |
| 19 |
|
| 20 |
public function rewind() |
| 21 |
{ |
| 22 |
rewind($this->file); |
| 23 |
$this->current = $this->_parseNextDataset(); |
| 24 |
$this->key = 0; |
| 25 |
} |
| 26 |
|
| 27 |
public function valid() |
| 28 |
{ |
| 29 |
return !feof($this->file); |
| 30 |
} |
| 31 |
|
| 32 |
public function key() |
| 33 |
{ |
| 34 |
return $this->key; |
| 35 |
} |
| 36 |
|
| 37 |
public function current() |
| 38 |
{ |
| 39 |
return $this->current; |
| 40 |
} |
| 41 |
|
| 42 |
public function next() |
| 43 |
{ |
| 44 |
$this->current = $this->_parseNextDataset(); |
| 45 |
$this->key++; |
| 46 |
} |
| 47 |
|
| 48 |
private function _parseNextDataset() |
| 49 |
{ |
| 50 |
// Read a line of test data from the file |
| 51 |
do { |
| 52 |
// Only take lines that contain test data and that aren't commented out |
| 53 |
$testDataRow = trim(fgets($this->file)); |
| 54 |
} while (($testDataRow > '') && ($testDataRow{0} === '#')); |
| 55 |
|
| 56 |
// Discard any comments at the end of the line |
| 57 |
list($testData) = explode('//',$testDataRow); |
| 58 |
|
| 59 |
// Split data into an array of individual values and a result |
| 60 |
$dataSet = $this->_getcsv($testData, ',', "'"); |
| 61 |
foreach($dataSet as &$dataValue) { |
| 62 |
$dataValue = $this->_parseDataValue($dataValue); |
| 63 |
} |
| 64 |
unset($dataValue); |
| 65 |
|
| 66 |
return $dataSet; |
| 67 |
} |
| 68 |
|
| 69 |
private function _getcsv($input, $delimiter, $enclosure) |
| 70 |
{ |
| 71 |
if (function_exists('str_getcsv')) { |
| 72 |
return str_getcsv($input, $delimiter, $enclosure); |
| 73 |
} |
| 74 |
|
| 75 |
$temp = fopen('php://memory', 'rw'); |
| 76 |
fwrite($temp, $input); |
| 77 |
rewind($temp); |
| 78 |
$data = fgetcsv($temp, strlen($input), $delimiter, $enclosure); |
| 79 |
fclose($temp); |
| 80 |
|
| 81 |
if ($data === false) { |
| 82 |
$data = array(null); |
| 83 |
} |
| 84 |
|
| 85 |
return $data; |
| 86 |
} |
| 87 |
|
| 88 |
private function _parseDataValue($dataValue) { |
| 89 |
// discard any white space |
| 90 |
$dataValue = trim($dataValue); |
| 91 |
// test for the required datatype and convert accordingly |
| 92 |
if (!is_numeric($dataValue)) { |
| 93 |
if($dataValue == '') { |
| 94 |
$dataValue = NULL; |
| 95 |
} elseif($dataValue == '""') { |
| 96 |
$dataValue = ''; |
| 97 |
} elseif(($dataValue[0] == '"') && ($dataValue[strlen($dataValue)-1] == '"')) { |
| 98 |
$dataValue = substr($dataValue,1,-1); |
| 99 |
} elseif(($dataValue[0] == '{') && ($dataValue[strlen($dataValue)-1] == '}')) { |
| 100 |
$dataValue = explode(';',substr($dataValue,1,-1)); |
| 101 |
foreach($dataValue as &$dataRow) { |
| 102 |
if (strpos($dataRow,'|') !== FALSE) { |
| 103 |
$dataRow = explode('|',$dataRow); |
| 104 |
foreach($dataRow as &$dataCell) { |
| 105 |
$dataCell = $this->_parseDataValue($dataCell); |
| 106 |
} |
| 107 |
unset($dataCell); |
| 108 |
} else { |
| 109 |
$dataRow = $this->_parseDataValue($dataRow); |
| 110 |
} |
| 111 |
} |
| 112 |
unset($dataRow); |
| 113 |
} else { |
| 114 |
switch (strtoupper($dataValue)) { |
| 115 |
case 'NULL' : $dataValue = NULL; break; |
| 116 |
case 'TRUE' : $dataValue = TRUE; break; |
| 117 |
case 'FALSE' : $dataValue = FALSE; break; |
| 118 |
} |
| 119 |
} |
| 120 |
} else { |
| 121 |
if (strpos($dataValue,'.') !== FALSE) { |
| 122 |
$dataValue = (float) $dataValue; |
| 123 |
} else { |
| 124 |
$dataValue = (int) $dataValue; |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
return $dataValue; |
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
|