PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/PHPExcel/unitTests/testDataFileIterator.php

https://gitlab.com/pra34/excel-parse
PHP | 136 lines | 115 code | 15 blank | 6 comment | 20 complexity | 0c6414e2b92ce073ae7a55707171b88d MD5 | raw file
  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. {
  72. // discard any white space
  73. $dataValue = trim($dataValue);
  74. // test for the required datatype and convert accordingly
  75. if (!is_numeric($dataValue)) {
  76. if ($dataValue == '') {
  77. $dataValue = null;
  78. } elseif ($dataValue == '""') {
  79. $dataValue = '';
  80. } elseif (($dataValue[0] == '"') && ($dataValue[strlen($dataValue)-1] == '"')) {
  81. $dataValue = substr($dataValue, 1, -1);
  82. } elseif (($dataValue[0] == '{') && ($dataValue[strlen($dataValue)-1] == '}')) {
  83. $dataValue = explode(';', substr($dataValue, 1, -1));
  84. foreach ($dataValue as &$dataRow) {
  85. if (strpos($dataRow, '|') !== false) {
  86. $dataRow = explode('|', $dataRow);
  87. foreach ($dataRow as &$dataCell) {
  88. $dataCell = $this->_parseDataValue($dataCell);
  89. }
  90. unset($dataCell);
  91. } else {
  92. $dataRow = $this->_parseDataValue($dataRow);
  93. }
  94. }
  95. unset($dataRow);
  96. } else {
  97. switch (strtoupper($dataValue)) {
  98. case 'NULL':
  99. $dataValue = null;
  100. break;
  101. case 'TRUE':
  102. $dataValue = true;
  103. break;
  104. case 'FALSE':
  105. $dataValue = false;
  106. break;
  107. }
  108. }
  109. } else {
  110. if (strpos($dataValue, '.') !== false) {
  111. $dataValue = (float) $dataValue;
  112. } else {
  113. $dataValue = (int) $dataValue;
  114. }
  115. }
  116. return $dataValue;
  117. }
  118. }