PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/spout/src/Spout/Writer/CSV/Writer.php

https://github.com/pauln/moodle
PHP | 101 lines | 42 code | 13 blank | 46 comment | 2 complexity | 234cbc4d77bde69682019c09df502913 MD5 | raw file
  1. <?php
  2. namespace Box\Spout\Writer\CSV;
  3. use Box\Spout\Writer\AbstractWriter;
  4. use Box\Spout\Common\Exception\IOException;
  5. use Box\Spout\Common\Helper\EncodingHelper;
  6. /**
  7. * Class Writer
  8. * This class provides support to write data to CSV files
  9. *
  10. * @package Box\Spout\Writer\CSV
  11. */
  12. class Writer extends AbstractWriter
  13. {
  14. /** Number of rows to write before flushing */
  15. const FLUSH_THRESHOLD = 500;
  16. /** @var string Content-Type value for the header */
  17. protected static $headerContentType = 'text/csv; charset=UTF-8';
  18. /** @var string Defines the character used to delimit fields (one character only) */
  19. protected $fieldDelimiter = ',';
  20. /** @var string Defines the character used to enclose fields (one character only) */
  21. protected $fieldEnclosure = '"';
  22. /** @var int */
  23. protected $lastWrittenRowIndex = 0;
  24. /**
  25. * Sets the field delimiter for the CSV
  26. *
  27. * @api
  28. * @param string $fieldDelimiter Character that delimits fields
  29. * @return Writer
  30. */
  31. public function setFieldDelimiter($fieldDelimiter)
  32. {
  33. $this->fieldDelimiter = $fieldDelimiter;
  34. return $this;
  35. }
  36. /**
  37. * Sets the field enclosure for the CSV
  38. *
  39. * @api
  40. * @param string $fieldEnclosure Character that enclose fields
  41. * @return Writer
  42. */
  43. public function setFieldEnclosure($fieldEnclosure)
  44. {
  45. $this->fieldEnclosure = $fieldEnclosure;
  46. return $this;
  47. }
  48. /**
  49. * Opens the CSV streamer and makes it ready to accept data.
  50. *
  51. * @return void
  52. */
  53. protected function openWriter()
  54. {
  55. // Adds UTF-8 BOM for Unicode compatibility
  56. $this->globalFunctionsHelper->fputs($this->filePointer, EncodingHelper::BOM_UTF8);
  57. }
  58. /**
  59. * Adds data to the currently opened writer.
  60. *
  61. * @param array $dataRow Array containing data to be written.
  62. * Example $dataRow = ['data1', 1234, null, '', 'data5'];
  63. * @param \Box\Spout\Writer\Style\Style $style Ignored here since CSV does not support styling.
  64. * @return void
  65. * @throws \Box\Spout\Common\Exception\IOException If unable to write data
  66. */
  67. protected function addRowToWriter(array $dataRow, $style)
  68. {
  69. $wasWriteSuccessful = $this->globalFunctionsHelper->fputcsv($this->filePointer, $dataRow, $this->fieldDelimiter, $this->fieldEnclosure);
  70. if ($wasWriteSuccessful === false) {
  71. throw new IOException('Unable to write data');
  72. }
  73. $this->lastWrittenRowIndex++;
  74. if ($this->lastWrittenRowIndex % self::FLUSH_THRESHOLD === 0) {
  75. $this->globalFunctionsHelper->fflush($this->filePointer);
  76. }
  77. }
  78. /**
  79. * Closes the CSV streamer, preventing any additional writing.
  80. * If set, sets the headers and redirects output to the browser.
  81. *
  82. * @return void
  83. */
  84. protected function closeWriter()
  85. {
  86. $this->lastWrittenRowIndex = 0;
  87. }
  88. }