Completed
Push — master ( 2f66fb...4eb816 )
by Greg
02:33
created

TsvFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 24
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultFormatterOptions() 0 6 1
A writeOneLine() 0 4 1
A tsvEscape() 0 9 1
1
<?php
2
namespace Consolidation\OutputFormatters\Formatters;
3
4
use Consolidation\OutputFormatters\FormatterInterface;
5
use Consolidation\OutputFormatters\ValidationInterface;
6
use Consolidation\OutputFormatters\FormatterOptions;
7
use Consolidation\OutputFormatters\Transformations\TableTransformation;
8
use Consolidation\OutputFormatters\Exception\IncompatibleDataException;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Tab-separated value formatters
13
 *
14
 * Display the provided structured data in a tab-separated list.  Output
15
 * escaping is much lighter, since there is no allowance for altering
16
 * the delimiter.
17
 */
18
class TsvFormatter extends CsvFormatter
19
{
20
    protected function getDefaultFormatterOptions()
21
    {
22
        return [
23
            FormatterOptions::INCLUDE_FIELD_LABELS => false,
24
        ];
25
    }
26
27
    protected function writeOneLine(OutputInterface $output, $data, $options)
28
    {
29
        $output->writeln($this->tsvEscape($data));
30
    }
31
32
    protected function tsvEscape($data)
33
    {
34
        return implode("\t", array_map(
35
            function ($item) {
36
                return str_replace(["\t", "\n"], ['\t', '\n'], $item);
37
            },
38
            $data
39
        ));
40
    }
41
}
42