Completed
Push — master ( 193118...0801ff )
by Greg
8s
created

CsvFormatter::isMultiLine()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
namespace Consolidation\OutputFormatters\Formatters;
3
4
use Consolidation\OutputFormatters\FormatterInterface;
5
use Consolidation\OutputFormatters\ValidationInterface;
6
use Consolidation\OutputFormatters\Transformations\TableTransformation;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class CsvFormatter implements FormatterInterface, ValidationInterface
10
{
11
    public function validate($structuredData)
12
    {
13
        // If the provided data was of class RowsOfFields
14
        // or AssociativeList, it will be converted into
15
        // a TableTransformation object.
16
        if (!is_array($structuredData) && (!$structuredData instanceof TableTransformation)) {
17
            throw new IncompatibleDataException(
18
                $this,
19
                $structuredData,
20
                [
21
                    new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'),
22
                    new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList'),
23
                    [],
24
                ]
25
            );
26
        }
27
        return $structuredData;
28
    }
29
30
    /**
31
     * @inheritdoc
32
     */
33
    public function write(OutputInterface $output, $data, $options = [])
34
    {
35
        if ($this->isMultiLine($data)) {
36
            $this->writeCsvLines($output, $data, $options);
37
            return;
38
        }
39
        $this->writeCsvLine($output, $data, $options);
40
    }
41
42
    protected function isMultiLine($data)
43
    {
44
        return
45
            ($data instanceof TableTransformation) ||
46
            (
47
                !empty($data) &&
48
                is_array($data[0])
49
            );
50
    }
51
52
    protected function writeCsvLines(OutputInterface $output, $data, $options)
53
    {
54
        $options += [
55
            'include-field-labels' => true,
56
        ];
57
58
        if ($options['include-field-labels'] && ($data instanceof TableTransformation)) {
59
            $headers = $data->getHeaders();
60
            $this->writeCsvLine($output, $headers, $options);
61
        }
62
63
        foreach ($data as $line) {
64
            $this->writeCsvLine($output, $line, $options);
65
        }
66
    }
67
68
    protected function writeCsvLine(OutputInterface $output, $data, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
69
    {
70
        $output->write($this->csvEscape($data));
71
    }
72
73
    protected function csvEscape($data, $delimiter = ',')
74
    {
75
        $buffer = fopen('php://temp', 'r+');
76
        fputcsv($buffer, $data, $delimiter);
77
        rewind($buffer);
78
        $csv = fgets($buffer);
79
        fclose($buffer);
80
        return $csv;
81
    }
82
}
83