|
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
|
|
|
class CsvFormatter implements FormatterInterface, ValidationInterface |
|
11
|
|
|
{ |
|
12
|
10 |
View Code Duplication |
public function validate($structuredData) |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
// If the provided data was of class RowsOfFields |
|
15
|
|
|
// or AssociativeList, it will be converted into |
|
16
|
|
|
// a TableTransformation object. |
|
17
|
10 |
|
if (!is_array($structuredData) && (!$structuredData instanceof TableTransformation)) { |
|
18
|
1 |
|
throw new IncompatibleDataException( |
|
19
|
1 |
|
$this, |
|
20
|
1 |
|
$structuredData, |
|
21
|
|
|
[ |
|
22
|
1 |
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'), |
|
23
|
1 |
|
new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList'), |
|
24
|
1 |
|
[], |
|
25
|
|
|
] |
|
26
|
1 |
|
); |
|
27
|
|
|
} |
|
28
|
9 |
|
return $structuredData; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @inheritdoc |
|
33
|
|
|
*/ |
|
34
|
9 |
|
public function write(OutputInterface $output, $data, $options = []) |
|
35
|
|
|
{ |
|
36
|
9 |
|
if ($this->isMultiLine($data)) { |
|
37
|
4 |
|
$this->writeCsvLines($output, $data, $options); |
|
38
|
4 |
|
return; |
|
39
|
|
|
} |
|
40
|
5 |
|
$this->writeCsvLine($output, $data, $options); |
|
41
|
5 |
|
} |
|
42
|
|
|
|
|
43
|
9 |
|
protected function isMultiLine($data) |
|
44
|
|
|
{ |
|
45
|
|
|
return |
|
46
|
9 |
|
($data instanceof TableTransformation) || |
|
47
|
|
|
( |
|
48
|
6 |
|
!empty($data) && |
|
49
|
6 |
|
is_array($data[0]) |
|
50
|
9 |
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
4 |
|
protected function writeCsvLines(OutputInterface $output, $data, $options) |
|
54
|
|
|
{ |
|
55
|
|
|
$options += [ |
|
56
|
4 |
|
'include-field-labels' => true, |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
4 |
|
if ($options['include-field-labels'] && ($data instanceof TableTransformation)) { |
|
60
|
3 |
|
$headers = $data->getHeaders(); |
|
61
|
3 |
|
$this->writeCsvLine($output, $headers, $options); |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
4 |
|
foreach ($data as $line) { |
|
65
|
4 |
|
$this->writeCsvLine($output, $line, $options); |
|
66
|
4 |
|
} |
|
67
|
4 |
|
} |
|
68
|
|
|
|
|
69
|
9 |
|
protected function writeCsvLine(OutputInterface $output, $data, $options) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
9 |
|
$output->write($this->csvEscape($data)); |
|
72
|
9 |
|
} |
|
73
|
|
|
|
|
74
|
9 |
|
protected function csvEscape($data, $delimiter = ',') |
|
75
|
|
|
{ |
|
76
|
9 |
|
$buffer = fopen('php://temp', 'r+'); |
|
77
|
9 |
|
fputcsv($buffer, $data, $delimiter); |
|
78
|
9 |
|
rewind($buffer); |
|
79
|
9 |
|
$csv = fgets($buffer); |
|
80
|
9 |
|
fclose($buffer); |
|
81
|
9 |
|
return $csv; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.