|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the csv-table-generator package. |
| 5 | + * |
| 6 | + * (c) E-commit <contact@e-commit.fr> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Ecommit\CsvTableGenerator; |
| 13 | + |
| 14 | +class Csv |
| 15 | +{ |
| 16 | + const EOL_LF = 'LF'; |
| 17 | + const EOL_CRLF = 'CR+LF'; |
| 18 | + |
| 19 | + protected $handle; |
| 20 | + protected $pathDir; |
| 21 | + protected $filename; |
| 22 | + protected $currentFile; |
| 23 | + protected $header; |
| 24 | + protected $maxLines; |
| 25 | + protected $delimiter; |
| 26 | + protected $enclosure; |
| 27 | + protected $fileNumber = 0; |
| 28 | + protected $lines; |
| 29 | + protected $unixToDos = false; |
| 30 | + protected $unixToDosPath; |
| 31 | + protected $addUtf8Bom = false; |
| 32 | + |
| 33 | + /** |
| 34 | + * Constructor |
| 35 | + * |
| 36 | + * @param string $pathDir Path folder |
| 37 | + * @param string $filename Filename (without path folder and extension) |
| 38 | + * @param array $options See README.md |
| 39 | + */ |
| 40 | + public function __construct($pathDir, $filename, $options = array()) { |
| 41 | + $defaultOptions = array( |
| 42 | + 'header' => array(), |
| 43 | + 'max_lines' => null, |
| 44 | + 'delimiter' => ',', |
| 45 | + 'enclosure' => '"', |
| 46 | + 'eol' => self::EOL_LF, |
| 47 | + 'unix2dos_path' => '/usr/bin/unix2dos', |
| 48 | + 'add_utf8_bom' => false, |
| 49 | + ); |
| 50 | + $options = array_merge($defaultOptions, $options); |
| 51 | + |
| 52 | + //Test folder |
| 53 | + $realPath = \realpath($pathDir); |
| 54 | + if (false === $realPath || !\is_writable($realPath)) { |
| 55 | + throw new \Exception(sprintf('Folder %s does not exist or is not writable', $pathDir)); |
| 56 | + } |
| 57 | + |
| 58 | + $this->pathDir = $realPath; |
| 59 | + $this->filename = $filename; |
| 60 | + $this->header = $options['header']; |
| 61 | + if (empty($options['max_lines'])) { |
| 62 | + $this->maxLines = null; |
| 63 | + } else { |
| 64 | + $this->maxLines = \intval($options['max_lines']); |
| 65 | + } |
| 66 | + $this->delimiter = $options['delimiter']; |
| 67 | + $this->enclosure = $options['enclosure']; |
| 68 | + |
| 69 | + if (self::EOL_CRLF === $options['eol']) { |
| 70 | + $this->unixToDos = true; |
| 71 | + } |
| 72 | + $this->unixToDosPath = $options['unix2dos_path']; |
| 73 | + |
| 74 | + $this->addUtf8Bom = $options['add_utf8_bom']; |
| 75 | + |
| 76 | + $this->open(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Open CSV file |
| 81 | + */ |
| 82 | + protected function open() |
| 83 | + { |
| 84 | + if ($this->handle) { |
| 85 | + throw new \Exception(sprintf('The file %s is already open', $this->filename)); |
| 86 | + } |
| 87 | + $this->fileNumber++; |
| 88 | + $this->lines = 0; |
| 89 | + $filename = $this->filename; |
| 90 | + if ($this->fileNumber > 1) { |
| 91 | + $filename .= '-' . $this->fileNumber; |
| 92 | + } |
| 93 | + $this->currentFile = $this->pathDir . '/' . $filename . '.csv'; |
| 94 | + $this->handle = \fopen($this->currentFile, 'wb'); //Binary is forced. EOL = "\n" |
| 95 | + if (false === $this->handle) { |
| 96 | + throw new \Exception(sprintf('Error during the opening of the %s file', $this->filename)); |
| 97 | + } |
| 98 | + if ($this->addUtf8Bom) { |
| 99 | + if (!\fputs($this->handle, chr(0xEF).chr(0xBB).chr(0xBF))) { |
| 100 | + throw new \Exception(sprintf('Error during the UTF8-BOM writing in %s file', $this->filename)); |
| 101 | + } |
| 102 | + } |
| 103 | + if (!empty($this->header)) { |
| 104 | + $this->write($this->header); |
| 105 | + $this->lines = 0; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Close CSV file |
| 111 | + */ |
| 112 | + public function close() |
| 113 | + { |
| 114 | + if ($this->handle) { |
| 115 | + \fclose($this->handle); |
| 116 | + $this->handle = null; |
| 117 | + |
| 118 | + if ($this->unixToDos) { |
| 119 | + passthru(sprintf('%s %s', $this->unixToDosPath, $this->currentFile), $returnVar); |
| 120 | + if (0 !== $returnVar) { |
| 121 | + throw new \Exception(sprintf('Unix2dos error (%s file)', $this->filename)); |
| 122 | + } |
| 123 | + } |
| 124 | + $this->currentFile = null; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Create new CSV file |
| 130 | + */ |
| 131 | + protected function newFile() |
| 132 | + { |
| 133 | + $this->close(); |
| 134 | + $this->open(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Add line in CSV file |
| 139 | + * @param array $data |
| 140 | + */ |
| 141 | + public function write($data) |
| 142 | + { |
| 143 | + if (!$this->handle) { |
| 144 | + throw new \Exception(sprintf('Handle does not exist. File %s', $this->filename)); |
| 145 | + } |
| 146 | + |
| 147 | + //New file |
| 148 | + if ($this->maxLines && $this->maxLines == $this->lines) { |
| 149 | + $this->newFile(); |
| 150 | + } |
| 151 | + |
| 152 | + //Write |
| 153 | + if (false === \fputcsv($this->handle, $data, $this->delimiter, $this->enclosure)) { |
| 154 | + throw new \Exception(sprintf('Error during the writing in %s file', $this->filename)); |
| 155 | + } |
| 156 | + |
| 157 | + $this->lines++; |
| 158 | + } |
| 159 | +} |
0 commit comments