CsvUtils::toString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
namespace Consolidation\AnnotatedCommand\Parser\Internal;
3
4
/**
5
 * Methods to convert to / from a csv string.
6
 */
7
class CsvUtils
8
{
9
    /**
10
     * Ensure that the provided data is a string.
11
     *
12
     * @param string|array $data The data to convert to a string.
13
     * @return string
14
     */
15
    public static function toString($data)
16
    {
17
        if (is_array($data)) {
18
            return static::csvEscape($data);
19
        }
20
        return $data;
21
    }
22
23
    /**
24
     * Convert a string to a csv.
25
     */
26
    public static function csvEscape(array $data, $delimiter = ',')
27
    {
28
        $buffer = fopen('php://temp', 'r+');
29
        fputcsv($buffer, $data, $delimiter);
30
        rewind($buffer);
31
        $csv = fgets($buffer);
32
        fclose($buffer);
33
        return rtrim($csv);
34
    }
35
36
    /**
37
     * Return a specific named annotation for this command.
38
     *
39
     * @param string|array $data The data to convert to an array.
40
     * @return array
41
     */
42
    public static function toList($data)
43
    {
44
        if (!is_array($data)) {
45
            return str_getcsv($data);
46
        }
47
        return $data;
48
    }
49
}
50