forked from consolidation/annotated-command
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvUtils.php
More file actions
49 lines (46 loc) · 1.1 KB
/
CsvUtils.php
File metadata and controls
49 lines (46 loc) · 1.1 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
<?php
namespace Consolidation\AnnotatedCommand\Parser\Internal;
/**
* Methods to convert to / from a csv string.
*/
class CsvUtils
{
/**
* Ensure that the provided data is a string.
*
* @param string|array $data The data to convert to a string.
* @return string
*/
public static function toString($data)
{
if (is_array($data)) {
return static::csvEscape($data);
}
return $data;
}
/**
* Convert a string to a csv.
*/
public static function csvEscape(array $data, $delimiter = ',')
{
$buffer = fopen('php://temp', 'r+');
fputcsv($buffer, $data, $delimiter, '"', "\\");
rewind($buffer);
$csv = fgets($buffer);
fclose($buffer);
return rtrim($csv);
}
/**
* Return a specific named annotation for this command.
*
* @param string|array $data The data to convert to an array.
* @return array
*/
public static function toList($data)
{
if (!is_array($data)) {
return str_getcsv($data);
}
return $data;
}
}