Completed
Pull Request — master (#16)
by Greg
02:48
created

ListFormatter::renderData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 4
crap 2
1
<?php
2
namespace Consolidation\OutputFormatters\Formatters;
3
4
use Symfony\Component\Console\Output\OutputInterface;
5
use Consolidation\OutputFormatters\FormatterInterface;
6
use Consolidation\OutputFormatters\OverrideRestructureInterface;
7
use Consolidation\OutputFormatters\StructuredData\ListDataInterface;
8
use Consolidation\OutputFormatters\StructuredData\RenderCellInterface;
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 AssociativeList.
17
 */
18
class ListFormatter implements FormatterInterface, OverrideRestructureInterface, RenderDataInterface
19
{
20
    /**
21
     * @inheritdoc
22
     */
23 5
    public function write(OutputInterface $output, $data, $options = [])
24
    {
25 5
        $output->writeln(implode("\n", $data));
26 5
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31 5
    public function overrideRestructure($structuredOutput, $configurationData, $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(), $configurationData, $options);
38
        }
39 1
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44 5
    public function renderData($originalData, $restructuredData, $configurationData, $options)
45
    {
46 5
        if ($originalData instanceof RenderCellInterface) {
47 2
            return $this->renderEachCell($originalData, $restructuredData, $configurationData, $options);
48
        }
49 3
        return $restructuredData;
50
    }
51
52 2
    protected function renderEachCell($originalData, $restructuredData, $configurationData, $options)
53
    {
54 2
        foreach ($restructuredData as $key => $cellData) {
55 2
            $restructuredData[$key] = $originalData->renderCell($key, $cellData, $configurationData, $options);
56 2
        }
57 2
        return $restructuredData;
58
    }
59
}
60