ListFormatter::overrideRestructure()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
namespace Consolidation\OutputFormatters\Formatters;
3
4
use Consolidation\OutputFormatters\Options\FormatterOptions;
5
use Consolidation\OutputFormatters\StructuredData\ListDataInterface;
6
use Consolidation\OutputFormatters\StructuredData\RenderCellInterface;
7
use Consolidation\OutputFormatters\Transformations\OverrideRestructureInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Display the data in a simple list.
12
 *
13
 * This formatter prints a plain, unadorned list of data,
14
 * with each data item appearing on a separate line.  If you
15
 * wish your list to contain headers, then use the table
16
 * formatter, and wrap your data in an PropertyList.
17
 */
18
class ListFormatter implements FormatterInterface, OverrideRestructureInterface, RenderDataInterface
19
{
20
    /**
21
     * @inheritdoc
22
     */
23 5
    public function write(OutputInterface $output, $data, FormatterOptions $options)
24
    {
25 5
        $output->writeln(implode("\n", $data));
26 5
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31 5
    public function overrideRestructure($structuredOutput, FormatterOptions $options)
32
    {
33
        // If the structured data implements ListDataInterface,
34
        // then we will render whatever data its 'getListData'
35
        // method provides.
36 5
        if ($structuredOutput instanceof ListDataInterface) {
37 4
            return $this->renderData($structuredOutput, $structuredOutput->getListData($options), $options);
38
        }
39 1
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 5
    public function renderData($originalData, $restructuredData, FormatterOptions $options)
45
    {
46 5
        if ($originalData instanceof RenderCellInterface) {
47 2
            return $this->renderEachCell($originalData, $restructuredData, $options);
48
        }
49 3
        return $restructuredData;
50
    }
51
52 2
    protected function renderEachCell($originalData, $restructuredData, FormatterOptions $options)
53
    {
54 2
        foreach ($restructuredData as $key => $cellData) {
55 2
            $restructuredData[$key] = $originalData->renderCell($key, $cellData, $options, $restructuredData);
56 2
        }
57 2
        return $restructuredData;
58
    }
59
}
60