CsvRcfWriter.php
8 years ago
CsvRfcUtils.php
8 years ago
CsvRfcWriteStreamFilter.php
8 years ago
CsvWriter.php
6 years ago
CsvRfcUtils.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * AJGL CSV RFC Component |
| 5 | * |
| 6 | * Copyright (C) Antonio J. García Lagar <aj@garcialagar.es> |
| 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 Wpae\Csv; |
| 13 | |
| 14 | /** |
| 15 | * @author Antonio J. García Lagar <aj@garcialagar.es> |
| 16 | */ |
| 17 | class CsvRfcUtils |
| 18 | { |
| 19 | const EOL_WRITE_DEFAULT = "\n"; |
| 20 | const EOL_WRITE_RFC = "\r\n"; |
| 21 | |
| 22 | private static $defaultEol = self::EOL_WRITE_DEFAULT; |
| 23 | |
| 24 | private function __construct() |
| 25 | { |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @see http://php.net/manual/en/function.fputcsv.php |
| 30 | * |
| 31 | * @param resource $handle |
| 32 | * @param array $fields |
| 33 | * @param string $delimiter |
| 34 | * @param string $enclosure |
| 35 | * @param string $escape |
| 36 | * @param string $eol |
| 37 | */ |
| 38 | public static function fPutCsv($handle, array $fields, $delimiter = ',', $enclosure = '"', $escape = '\\', $eol = null) |
| 39 | { |
| 40 | self::checkPutCsvEscape($escape); |
| 41 | |
| 42 | $eol = self::resolveEol($eol); |
| 43 | if ($eol !== self::EOL_WRITE_DEFAULT || self::hasAnyValueWithEscapeFollowedByEnclosure($fields, $enclosure)) { |
| 44 | \fwrite($handle, self::strPutCsv($fields, $delimiter, $enclosure, $eol)); |
| 45 | } else { |
| 46 | \fputcsv($handle, $fields, $delimiter, $enclosure); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @param array $fields |
| 52 | * @param string $enclosure |
| 53 | * |
| 54 | * @return bool |
| 55 | */ |
| 56 | private static function hasAnyValueWithEscapeFollowedByEnclosure(array $fields, $enclosure) |
| 57 | { |
| 58 | foreach ($fields as $value) { |
| 59 | if (strpos($value, '\\'.$enclosure) !== false) { |
| 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @param string | null $eol |
| 69 | * |
| 70 | * @return bool |
| 71 | */ |
| 72 | private static function resolveEol($eol) |
| 73 | { |
| 74 | return $eol === null ? self::$defaultEol : (string) $eol; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * @see http://php.net/manual/en/function.fgetcsv.php |
| 79 | * |
| 80 | * @param resource $handle |
| 81 | * @param int $length |
| 82 | * @param string $delimiter |
| 83 | * @param string $enclosure |
| 84 | * @param string $escape |
| 85 | * |
| 86 | * @return array|false|null |
| 87 | */ |
| 88 | public static function fGetCsv($handle, $length = 0, $delimiter = ',', $enclosure = '"', $escape = '"') |
| 89 | { |
| 90 | self::checkGetCsvEscape($enclosure, $escape); |
| 91 | |
| 92 | return \fgetcsv($handle, $length, $delimiter, $enclosure, $enclosure); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @see http://php.net/manual/en/function.str_getcsv.php |
| 97 | * |
| 98 | * @param string $input |
| 99 | * @param string $delimiter |
| 100 | * @param string $enclosure |
| 101 | * @param string $escape |
| 102 | * |
| 103 | * @return array |
| 104 | */ |
| 105 | public static function strGetCsv($input, $delimiter = ',', $enclosure = '"', $escape = '"') |
| 106 | { |
| 107 | self::checkGetCsvEscape($enclosure, $escape); |
| 108 | |
| 109 | return \str_getcsv($input, $delimiter, $enclosure, $enclosure); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * This code was borrowed from goodby/csv under MIT LICENSE. |
| 114 | * |
| 115 | * @author Hidehito Nozawa <suinyeze@gmail.com> |
| 116 | * |
| 117 | * @see https://github.com/goodby/csv |
| 118 | * @see https://github.com/goodby/csv/blob/c6677d9c68323ef734a67a34f3e5feabcafd5b4e/src/Goodby/CSV/Export/Standard/CsvFileObject.php#L46 |
| 119 | * |
| 120 | * @param array $fields |
| 121 | * @param string $delimiter |
| 122 | * @param string $enclosure |
| 123 | * @param string $eol |
| 124 | * |
| 125 | * @return string |
| 126 | */ |
| 127 | public static function strPutCsv(array $fields, $delimiter = ',', $enclosure = '"', $eol = self::EOL_WRITE_DEFAULT) |
| 128 | { |
| 129 | $file = new \SplTempFileObject(); |
| 130 | $file->fputcsv($fields, $delimiter, $enclosure); |
| 131 | $file->rewind(); |
| 132 | |
| 133 | $line = ''; |
| 134 | while (!$file->eof()) { |
| 135 | $line .= $file->fgets(); |
| 136 | } |
| 137 | |
| 138 | $line = self::fixEnclosureEscape($enclosure, $line); |
| 139 | |
| 140 | if ($eol !== self::EOL_WRITE_DEFAULT) { |
| 141 | $line = rtrim($line, "\n").$eol; |
| 142 | } |
| 143 | |
| 144 | return $line; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Fix the enclosure escape in the given CSV raw line. |
| 149 | * |
| 150 | * @param string $enclosure |
| 151 | * @param string $line |
| 152 | */ |
| 153 | public static function fixEnclosureEscape($enclosure, $line) |
| 154 | { |
| 155 | return \str_replace('\\'.$enclosure, '\\'.$enclosure.$enclosure, $line); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Emits a warning if the escape char is not the default backslash or null. |
| 160 | * |
| 161 | * @param string $escape |
| 162 | */ |
| 163 | public static function checkPutCsvEscape($escape) |
| 164 | { |
| 165 | if ($escape !== '\\' && $escape !== null) { |
| 166 | trigger_error( |
| 167 | sprintf( |
| 168 | "In writing mode, the escape char must be a backslash '\\'. " |
| 169 | ."The given escape char '%s' will be ignored.", |
| 170 | $escape |
| 171 | ), |
| 172 | E_USER_WARNING |
| 173 | ); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Emits a warning if the enclosure char and escape char are different. |
| 179 | * |
| 180 | * @param string $enclosure |
| 181 | * @param string $escape |
| 182 | */ |
| 183 | public static function checkGetCsvEscape($enclosure, $escape) |
| 184 | { |
| 185 | if ($enclosure !== $escape) { |
| 186 | trigger_error( |
| 187 | sprintf( |
| 188 | 'In reading mode, the escape and enclosure chars must be equals. ' |
| 189 | ."The given escape char '%s' will be ignored.", |
| 190 | $escape |
| 191 | ), |
| 192 | E_USER_WARNING |
| 193 | ); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * @param string $eol |
| 199 | */ |
| 200 | public static function setDefaultWriteEol($eol) |
| 201 | { |
| 202 | self::$defaultEol = $eol; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @return string $eol |
| 207 | */ |
| 208 | public static function getDefaultWriteEol() |
| 209 | { |
| 210 | return self::$defaultEol; |
| 211 | } |
| 212 | } |
| 213 |