|
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 Consolidation\OutputFormatters\Exception\IncompatibleDataException; |
|
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Comma-separated value formatters |
|
12
|
|
|
* |
|
13
|
|
|
* Display the provided structured data in a comma-separated list. If |
|
14
|
|
|
* there are multiple records provided, then they will be printed |
|
15
|
|
|
* one per line. The primary data types accepted are RowsOfFields and |
|
16
|
|
|
* AssociativeList. The later behaves exactly like the former, save for |
|
17
|
|
|
* the fact that it contains but a single row. This formmatter can also |
|
18
|
|
|
* accept a PHP array; this is also interpreted as a single-row of data |
|
19
|
|
|
* with no header. |
|
20
|
|
|
*/ |
|
21
|
|
|
class CsvFormatter implements FormatterInterface, ValidationInterface, RenderDataInterface |
|
22
|
|
|
{ |
|
23
|
|
|
use RenderTableDataTrait; |
|
24
|
|
|
|
|
25
|
12 |
|
public function validate($structuredData) |
|
26
|
|
|
{ |
|
27
|
|
|
// If the provided data was of class RowsOfFields |
|
28
|
|
|
// or AssociativeList, it will be converted into |
|
29
|
|
|
// a TableTransformation object. |
|
30
|
12 |
|
if (!is_array($structuredData) && (!$structuredData instanceof TableTransformation)) { |
|
31
|
1 |
|
throw new IncompatibleDataException( |
|
32
|
1 |
|
$this, |
|
33
|
1 |
|
$structuredData, |
|
34
|
|
|
[ |
|
35
|
1 |
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'), |
|
36
|
1 |
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList'), |
|
37
|
1 |
|
[], |
|
38
|
|
|
] |
|
39
|
1 |
|
); |
|
40
|
|
|
} |
|
41
|
|
|
// If the data was provided to us as a single array, then |
|
42
|
|
|
// convert it to a single row. |
|
43
|
11 |
|
if (is_array($structuredData) && !empty($structuredData)) { |
|
44
|
6 |
|
$firstRow = reset($structuredData); |
|
45
|
6 |
|
if (!is_array($firstRow)) { |
|
46
|
5 |
|
return [$structuredData]; |
|
47
|
|
|
} |
|
48
|
1 |
|
} |
|
49
|
6 |
|
return $structuredData; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
11 |
|
public function write(OutputInterface $output, $data, $options = []) |
|
56
|
|
|
{ |
|
57
|
|
|
$options += [ |
|
58
|
11 |
|
'include-field-labels' => true, |
|
59
|
|
|
]; |
|
60
|
|
|
|
|
61
|
11 |
|
if ($options['include-field-labels'] && ($data instanceof TableTransformation)) { |
|
62
|
5 |
|
$headers = $data->getHeaders(); |
|
63
|
5 |
|
$this->writeCsvLine($output, $headers, $options); |
|
64
|
5 |
|
} |
|
65
|
|
|
|
|
66
|
11 |
|
foreach ($data as $line) { |
|
67
|
11 |
|
$this->writeCsvLine($output, $line, $options); |
|
68
|
11 |
|
} |
|
69
|
11 |
|
} |
|
70
|
|
|
|
|
71
|
11 |
|
protected function writeCsvLine(OutputInterface $output, $data, $options) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
11 |
|
$output->write($this->csvEscape($data)); |
|
74
|
11 |
|
} |
|
75
|
|
|
|
|
76
|
11 |
|
protected function csvEscape($data, $delimiter = ',') |
|
77
|
|
|
{ |
|
78
|
11 |
|
$buffer = fopen('php://temp', 'r+'); |
|
79
|
11 |
|
fputcsv($buffer, $data, $delimiter); |
|
80
|
11 |
|
rewind($buffer); |
|
81
|
11 |
|
$csv = fgets($buffer); |
|
82
|
11 |
|
fclose($buffer); |
|
83
|
11 |
|
return $csv; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.