csv_reader.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Read cvs data from a stream - string/FileReader.
  4. *
  5. * Returns data as associative arrays (headers are the keys of the array).
  6. * Skip blank lines ?? is it such a good idea?
  7. *
  8. * Usage:
  9. *
  10. * $reader = CsvReader::create('path');
  11. * foreach($reader as $items){
  12. * foreach($items as $key=>$value){
  13. * echo "$key : $value";
  14. * }
  15. * }
  16. *
  17. *
  18. *
  19. * @copyright (c) 2012 University of Geneva
  20. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  21. * @author Laurent Opprecht <laurent@opprecht.info>
  22. */
  23. class CsvReader implements Iterator
  24. {
  25. /**
  26. *
  27. * @param string|FileReader $stream
  28. * @param string $delimiter
  29. * @param string $enclosure
  30. * @return CsvReader
  31. */
  32. static function create($stream, $delimiter = ';', $enclosure = '"')
  33. {
  34. return new self($stream, $delimiter, $enclosure);
  35. }
  36. protected $stream = null;
  37. protected $headers = array();
  38. protected $delimiter = '';
  39. protected $enclosure = '';
  40. protected $current = false;
  41. protected $index = -1;
  42. function __construct($stream, $delimiter = ';', $enclosure = '"')
  43. {
  44. $this->stream = $stream;
  45. $this->delimiter = $delimiter ? substr($delimiter, 0, 1) : ';';
  46. $this->enclosure = $enclosure ? substr($enclosure, 0, 1) : '"';
  47. }
  48. function get_delimiter()
  49. {
  50. return $this->delimiter;
  51. }
  52. function get_enclosure()
  53. {
  54. return $this->enclosure;
  55. }
  56. function headers()
  57. {
  58. return $this->headers;
  59. }
  60. /**
  61. * @return FileReader
  62. */
  63. function stream()
  64. {
  65. if (is_string($this->stream)) {
  66. $this->stream = new FileReader($this->stream);
  67. }
  68. return $this->stream;
  69. }
  70. protected function decode($line)
  71. {
  72. if (empty($line)) {
  73. return array();
  74. }
  75. $data = Text::api_str_getcsv($line, $this->get_delimiter(), $this->get_enclosure());
  76. if ($this->headers) {
  77. $result = array();
  78. foreach ($data as $index => $value) {
  79. $key = isset($this->headers[$index]) ? $this->headers[$index] : false;
  80. if ($key) {
  81. $result[$key] = $value;
  82. } else {
  83. $result[] = $value;
  84. }
  85. }
  86. } else {
  87. $result = $data;
  88. }
  89. return $result;
  90. }
  91. /**
  92. * Returns the next non empty line
  93. *
  94. * @return boolean|string
  95. */
  96. protected function next_line()
  97. {
  98. while (true) {
  99. $line = $this->stream()->next();
  100. if ($line === false) {
  101. return false;
  102. } else if ($line) {
  103. return $line;
  104. }
  105. }
  106. return false;
  107. }
  108. public function current()
  109. {
  110. return $this->current;
  111. }
  112. public function key()
  113. {
  114. return $this->index;
  115. }
  116. public function next()
  117. {
  118. if (empty($this->headers)) {
  119. $line = $this->next_line();
  120. $this->headers = $this->decode($line);
  121. }
  122. $line = $this->next_line();
  123. if ($line) {
  124. $this->current = $this->decode($line);
  125. $this->index++;
  126. } else {
  127. $this->current = false;
  128. }
  129. return $this->current;
  130. }
  131. public function rewind()
  132. {
  133. $this->stream()->rewind();
  134. $line = $this->stream()->current();
  135. if (empty($line)) {
  136. $line = $this->next_line();
  137. }
  138. $this->headers = $this->decode($line);
  139. $this->index = -1;
  140. $this->next();
  141. }
  142. public function valid()
  143. {
  144. return $this->current !== false;
  145. }
  146. function __clone()
  147. {
  148. $this->stream()->rewind();
  149. $this->current = false;
  150. $this->index = -1;
  151. $this->headers = array();
  152. }
  153. }