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