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