Nessuna descrizione

testDataFileIterator.php 3.7KB

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